地图活动中的位置未立即设置?

时间:2015-07-30 22:24:05

标签: android google-maps google-maps-api-2 onresume location-services

我想要的是我的地图活动在打开地图时缩放到用户的当前位置。如果我在运行应用程序之前启用位置服务,它可以正常工作。当我禁用位置服务并运行我的应用程序时,我会提示用户打开位置服务。它需要设置才能打开它,当它们回击时,地图应该缩放到它们的当前位置。我把我的zoomToLocation放在setUpMap()中,它在OnResume()中调用,但无论出于何种原因它似乎都不起作用。

代码:

位置服务检查:

private boolean checkLocationEnabled() {
    //credits: http://stackoverflow.com/questions/10311834/how-to-check-if-location-services-are-enabled
    LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    final boolean gpsEnabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
    boolean networkEnabled = lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
    if (!gpsEnabled) {
        AlertDialog.Builder gpsAlertBuilder = new AlertDialog.Builder(this);
        gpsAlertBuilder.setTitle("Location Services Must Be Turned On");
        gpsAlertBuilder.setMessage("Would you like to turn them on now? (Note: if not, you will be unable to use the map to find breweries. You will still be able to search for them.");
        gpsAlertBuilder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.cancel();
                Intent enableGPSIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                startActivity(enableGPSIntent);
            }
        });
        gpsAlertBuilder.setNegativeButton("No", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();

            }
        });
        AlertDialog gpsAlert = gpsAlertBuilder.create();
        gpsAlert.show();
    }

    return gpsEnabled;


}

Zoom和zoomToLocation()方法:

 private void zoom(Location location) {
    mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(
            new LatLng(location.getLatitude(), location.getLongitude()), 13));

    CameraPosition cameraPosition = new CameraPosition.Builder()
            .target(new LatLng(location.getLatitude(), location.getLongitude()))      // Sets the center of the map to location user
            .zoom(17)                   // Sets the zoom// Sets the orientation of the camera to east// Sets the tilt of the camera to 30 degrees
            .build();                   // Creates a CameraPosition from the builder
    mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
}

private void zoomToLocation() {
    //credits: http://stackoverflow.com/questions/18425141/android-google-maps-api-v2-zoom-to-current-location
    //http://stackoverflow.com/questions/14502102/zoom-on-current-user-location-displayed/14511032#14511032
    LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    Criteria criteria = new Criteria();

    Location location = locationManager.getLastKnownLocation(locationManager.getBestProvider(criteria, false));
    if (location != null) {

        zoom(location);

    } else {

       return;
    }

}

setUpMap方法:

private void setUpMap() {
    UiSettings settings = mMap.getUiSettings();
    settings.setZoomControlsEnabled(true);
    settings.setZoomGesturesEnabled(true);
    mMap.setMyLocationEnabled(true);
    settings.setMyLocationButtonEnabled(true);
    setUpActionBar();
    if(checkLocationEnabled()) {
        zoomToLocation();

    }
}

OnResume方法:

 protected void onResume() {
    super.onResume();
    setUpMapIfNeeded(); //setUpMap called in setUpMapIfNeeded
}

最后是mySetUpMapIfNeeded()方法:

  private void setUpMapIfNeeded() {
    // Do a null check to confirm that we have not already instantiated the map.
    if (mMap == null) {
        // Try to obtain the map from the SupportMapFragment.
        mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
                .getMap();
        // Check if we were successful in obtaining the map.
        if (mMap != null) {
            setUpMap();
            setUpActionBar();
        }
    }
}

1 个答案:

答案 0 :(得分:0)

您的setUpMapIfNeeded() onResume可能会被调用。

但是在您的setUpMapIfNeeded中,如果第一个setUpMap()为空,则只调用mMap。恢复应用程序时,该值不为空。您必须使用除setUpMap()内的其他功能设置缩放级别。

像这样。

private void setUpMapIfNeeded() {
    // Do a null check to confirm that we have not already instantiated the map.
    if (mMap == null) {
        // Try to obtain the map from the SupportMapFragment.
        mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
                .getMap();
        // Check if we were successful in obtaining the map.
        if (mMap != null) {
            setUpMap();
            setUpActionBar();
        }
    }
    else{
         //setup zoom level since your mMap isn't null.
    }
}