使用Google位置API在android中的onMapReady中获取当前位置

我试图显示在我的应用程序内的谷歌地图上的用户的当前位置,但我不想随着用户移动不断更新的位置。 他的初始位置应该logging下来,直到他closures应用程序。 我已经为此写了下面的代码:

private GoogleMap mMap; protected GoogleApiClient mGoogleApiClient; Location mLastLocation; double lat =0, lng=0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_map); buildGoogleApiClient(); // Obtain the SupportMapFragment and get notified when the map is ready to be used. SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager() .findFragmentById(R.id.map); mapFragment.getMapAsync(this); } private void buildGoogleApiClient() { mGoogleApiClient = new GoogleApiClient.Builder(this) .addConnectionCallbacks(this) .addOnConnectionFailedListener(this) .addApi(LocationServices.API) .build(); } /** * Manipulates the map once available. * This callback is triggered when the map is ready to be used. * This is where we can add markers or lines, add listeners or move the camera. In this case, * we just add a marker near Sydney, Australia. * If Google Play services is not installed on the device, the user will be prompted to install * it inside the SupportMapFragment. This method will only be triggered once the user has * installed Google Play services and returned to the app. */ @Override public void onMapReady(GoogleMap googleMap) { mMap = googleMap; // Add a marker in Sydney and move the camera mMap.setMyLocationEnabled(true); LatLng loc = new LatLng(lat, lng); mMap.addMarker(new MarkerOptions().position(loc).title("New Marker")); mMap.moveCamera(CameraUpdateFactory.newLatLng(loc)); } @Override public void onConnected(Bundle bundle) { mLastLocation = LocationServices.FusedLocationApi.getLastLocation( mGoogleApiClient); if (mLastLocation != null) { lat = mLastLocation.getLatitude(); lng = mLastLocation.getLongitude(); } } @Override protected void onStart() { super.onStart(); mGoogleApiClient.connect(); } 

这个代码的问题是在onMapReady函数已经完成执行后,我得到了lat, lng 。 我认为解决这个问题的方法之一是创build一个AsyncTask来确保在地图之前检索位置数据。 但是,我试图以不会使用AsyncTask的方式来实现。

Solutions Collecting From Web of "使用Google位置API在android中的onMapReady中获取当前位置"

只需将创build标记的代码从onMapReady()移动到onConnected() ,并将调用buildGoogleApiClient()调用从onCreate()onMapReady()

 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_map); //buildGoogleApiClient(); // Obtain the SupportMapFragment and get notified when the map is ready to be used. SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager() .findFragmentById(R.id.map); mapFragment.getMapAsync(this); } @Override public void onMapReady(GoogleMap googleMap) { mMap = googleMap; // Add a marker in Sydney and move the camera mMap.setMyLocationEnabled(true); //add this here: buildGoogleApiClient(); //LatLng loc = new LatLng(lat, lng); //mMap.addMarker(new MarkerOptions().position(loc).title("New Marker")); //mMap.moveCamera(CameraUpdateFactory.newLatLng(loc)); } @Override public void onConnected(Bundle bundle) { mLastLocation = LocationServices.FusedLocationApi.getLastLocation( mGoogleApiClient); if (mLastLocation != null) { lat = mLastLocation.getLatitude(); lng = mLastLocation.getLongitude(); LatLng loc = new LatLng(lat, lng); mMap.addMarker(new MarkerOptions().position(loc).title("New Marker")); mMap.moveCamera(CameraUpdateFactory.newLatLng(loc)); } } 

虽然,请注意,您将经常从getLastLocation()的调用中得到null。 您可以使用requestLocationUpdates() ,并在第一个位置进来时调用removeLocationUpdates() 。请看这个答案 ,它有一个完全的例子,正是你想要做的。