频繁位置更新期间的相机移动

CameraMovement during frequent location updates

我正在寻找有关 mapView 中的 cameraMovement 的最佳实践建议。 我现在每隔几秒检索一次位置更新,我想知道您如何最好地处理 cameraMovement。 我想让用户在地图上滚动并探索一些地方 + 阅读 infoWindows 中的文本,而不需要相机不断地围绕当前用户位置重新定位。 你能推荐我如何最好地处理这个问题吗?

public LocationRequest getLocationRequest() {
    LocationRequest locationRequest = new LocationRequest();
    locationRequest.setInterval(100000);
    locationRequest.setFastestInterval(20000);
    locationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
    return locationRequest;
}

这是在位置回调中调用的。

public void createLocationCallback() {
mLocationCallback =new LocationCallback() {
    @Override
    public void onLocationResult (LocationResult locationResult){
        for (Location location : locationResult.getLocations()) {
            Log.i(LOG_TAG, "Location: " + location.getLatitude() + " " + location.getLongitude() + " " + location.getTime());
            mLastKnownLocation = location;
            updateLocationUI(location);
            updateLocationOnFirebase();
            displayLocationData();
        }
    }
};

如您所见,对于每个新检索到的位置,都会调用 updateLocationUI 方法,使相机围绕当前位置重新居中。

private void updateLocationUI(Location mLastKnownLocation) {
    if (mGoogleMap == null) {
        return;
    }
    try {
        if (mLastKnownLocation != null) {
            mGoogleMap.setMyLocationEnabled(true);
            mGoogleMap.getUiSettings().setMyLocationButtonEnabled(true);
            mGoogleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(
                    new LatLng(mLastKnownLocation.getLatitude(),
                            mLastKnownLocation.getLongitude()), DEFAULT_ZOOM));
            Snackbar.make(getActivity().findViewById(android.R.id.content),
                    "Retrieving location update", Snackbar.LENGTH_SHORT).show();
        } else {
            mGoogleMap.setMyLocationEnabled(false);
            mGoogleMap.getUiSettings().setMyLocationButtonEnabled(false);
            Log.d(LOG_TAG, "Current location null. Use defaults here");
        }
    } catch (SecurityException e) {
        Log.e("Exception: %s", e.getMessage());
    }
}

所以我现在实际上用另一种方式解决了它。简单地设计了两种不同的方法(一种在该位置周围重新居中相机,一种不重新居中)并声明一个布尔值 firstRun,每次创建片段时该方法都为真。

如果为 true,则调用使相机围绕该位置重新居中的初始方法,并将布尔值设置为 false。

只要该值为 false,就会调用处理位置更新的常规方法,但不会使相机重新居中。

要重新居中,可以使用Google提供的位置按钮。

我想的太复杂了,这是一个简单的解决方案,完全符合我的需要:)