地图 Activity 中的位置没有立即设置?

Location in Map Activity not set immediately?

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

代码:

位置服务检查:

private boolean checkLocationEnabled() {
    //credits: 
    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;


}

缩放和 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: 
    //
    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();
        }
    }
}

你的setUpMapIfNeeded()onResume大概叫

但是在您的 setUpMapIfNeeded 中,如果第一个 mMap 为空,您只会调用 setUpMap()。当您恢复您的申请时,这不是空的。除了 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.
    }
}