Android位置管理器-等待位置



我有一个代码在onCreate()

LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(  
LocationManager.GPS_PROVIDER, 500, 1, new LocationListener (){
             @Override
             public void onLocationChanged(Location loc) {
                               CameraUpdate center = CameraUpdateFactory.newLatLng(new LatLng(loc.getLatitude(), loc.getLongitude()));
                               myMap.moveCamera(center);  
             }
});

当改变用户的位置时发生,但我希望它只发生一次。我想等到接收到新位置时再移动到新的位置地图,然后不将地图移动到用户

类成员

private LocationManager mLocationManager; 
MyLocationListener mLocationListener 
locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
MyLocationListener mLocationListener = new MyLocationListener();
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 500, 1, myLocationListener);
public MyLocationListener extends LocationListener
{
        @Override
        public void onLocationChanged(Location loc) {
              CameraUpdate center = CameraUpdateFactory.newLatLng(new LatLng(loc.getLatitude(), loc.getLongitude()));
              myMap.moveCamera(center); 
              mLocationManager.removeUpdates(mLocationListener): 
             }
        @Override
        public void onProviderDisabled(String provider) 
         {
         }
        @Override
        public void onProviderEnabled(String provider) 
        {
        }
        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) 
        {
        }
}

最新更新