android - GoogleMap - animateCamera() 在 fragment 恢复后不执行任何操作

标签 android google-maps-android-api-2 android-mapview

我在布局中使用 GoogleMap/MapView,但作为 View 而不是 fragment (因为父级需要是 fragment ),因此 fragment 的布局包括以下内容:

<com.google.android.gms.maps.MapView
    android:id="@+id/map"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    />

该 fragment 包含以下内容:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle bundle) {
    <......>
    initMap(bundle, mapView);
    return rootView;
}

@Override
public void onResume() {
    super.onResume();
    MyApp.bus.register(this);
    updateMapLocation(MyApp.getMostRecentLocation());
}

@Override
public void onPause() {
    super.onPause();
    MyApp.bus.unregister(this);
}

@Subscribe
public void locationReceived(LocationReceived m) {
    Timber.i("Received bus message - Location!");
    updateMapLocation(MyApp.getMostRecentLocation());
}

其父 Fragment 包含以下内容:

private MapView mapView;
private GoogleMap map;

@Override
public void onResume() {
    super.onResume();
    if (mapView!=null) {
        mapView.onResume();
        map = this.mapView.getMap();
    }
}

@Override
public void onDestroy() {
    super.onDestroy();
    if (mapView!=null) mapView.onDestroy();
}

@Override
public void onLowMemory() {
    super.onLowMemory();
    if (mapView!=null) mapView.onLowMemory();
}

protected void initMap(Bundle bundle, MapView mapView) {
    this.mapView = mapView;
    this.mapView.onCreate(bundle);
    map = this.mapView.getMap();
    map.getUiSettings().setMyLocationButtonEnabled(false);
    map.setMyLocationEnabled(true);
    map.setBuildingsEnabled(true);
    map.getUiSettings().setZoomControlsEnabled(false);
    map.getUiSettings().setMyLocationButtonEnabled(true);
    try {
        MapsInitializer.initialize(this.getActivity());
    } catch (Exception e) {
        Timber.e(e, "Error initialising Google Map");
    }
    CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(MyApp.getCentralUKLatLng(), getResources().getInteger(R.integer.map_zoom_initial));
    map.animateCamera(cameraUpdate);
}

@Override
public void onPause() {
    super.onPause();
    if (mapView!=null) mapView.onPause();
}

protected void updateMapLocation(Location location) {
    Timber.i("Moving map to a new location: " + location);
    LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
    CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(latLng, 15);
    map.animateCamera(cameraUpdate);
}

新地点将通过奥托巴士运送。上面的代码可以完美运行,但只是第一次。如果我在这个 fragment 前面打开另一个 fragment ,然后再次关闭它, map 将无法动画到随后提供的位置。肯定会收到位置,肯定会调用 animateCamera() 方法(具有有效的位置和缩放),但绝对没有任何反应。没有错误,没有日志消息,什么都没有。更令人气愤的是,在一个 fragment (与上面的代码相同)上,它在恢复 fragment 时工作正常。

我认为我在恢复时(重新)初始化 GoogeMap 或 MapView 的方式上做错了,但我正在通过 onPause() 和 onResume() 调用 MapView,我认为这是必要的。我还需要做什么?

最佳答案

这是因为你没有在onResume()中编写代码。 看看当您打开和关闭第二个 fragment 时,第一个 fragment 将始终保留在那里,因此当第二个 fragment 关闭时,第一个 fragment 的 onResume() 将被调用。 因此,您必须在 onResume() 中为相机设置动画。

关于android - GoogleMap - animateCamera() 在 fragment 恢复后不执行任何操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27234724/

相关文章:

android - 如何在 Fragment 中创建一个 MapView

android - 对话框中的 map View

java - 尝试将新的dialogFragment添加到 fragment 上的现有dialogfragmen时,FragmentManager.beginTransaction()在空对象引用上

CameraPreview 和 GoogleMaps API v2 View 之间的 Android SurfaceView 冲突

Android Google Maps Direction Api - Api key 限制不起作用

android - 在 Android Map v2 API 中如何获取要共享的屏幕图像?

Android 在具有两个点的 map View 上绘制路线

android - 当我想从另一个 listView 创建 listView 时出错

android - 如何从另一个页面控制 viewPages 页面

android - 如何构建具有可用隐藏和内部 API 的 Android SDK?