0

我想要的是我的地图活动在地图打开时缩放到用户当前位置。如果我在运行应用程序之前启用位置服务,它工作正常。当我禁用定位服务并运行我的应用程序时,我会提示用户打开定位服务。它会将他们带到设置以将其打开,当他们回击时,地图应该缩放到他们当前的位置。我已将 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;
    }

}

设置地图方法:

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

1 回答 1

0

setUpMapIfNeeded() onResume的可能被称为。

但是在您的情况下,您只有在 first为空setUpMapIfNeeded时才调用。当您恢复您的应用程序时,这不为空。除了 inside 之外,您还必须使用其他功能设置缩放级别。setUpMap()mMapsetUpMap()

像这样的东西。

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.
    }
}
于 2015-07-30T22:31:13.943 回答