2013-05-11 60 views
0

我在OnCreate(代碼)Android的位置經理 - 等待位置

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

這發生在改變用戶的位置,但我希望它發生一次。 我想等到新的位置接收到移動到新位置的地圖,然後將地圖不能移動到用戶

+0

不能明白你的意思。你想爲用戶位置設置一次相機? – 2013-05-11 13:04:56

+0

是的。我只想移動相機而不使用方法locationManager.getLastKnownLocation(); – 2013-05-11 18:04:56

回答

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

     } 

}