Android位置管理器 - 等待位置

时间:2013-05-11 12:25:10

标签: android location locationmanager

我在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);  
             }
});

当更改用户的位置时会发生这种情况,但我希望它发生一次。 我想等到收到新位置后移动到新的位置图,然后不将地图移动到用户

1 个答案:

答案 0 :(得分:1)

班级成员

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

        }

}