Android Location manager - wait location

I have a code in the 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);  
             }
});

This occurs when the changes the user's position, but I want it to happen once. I want to wait until the new location is received to move to a new location map, and then not move the map to the user

Upvotes: 0

Views: 439

Answers (1)

Hoan Nguyen
Hoan Nguyen

Reputation: 18151

Class member

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) 
        {

        }

}

Upvotes: 1

Related Questions