重新加载布局时Google地图会消失

时间:2017-12-14 21:01:50

标签: android

我在布局中包含了一个MapView,并在活动的onCreate期间加载了地图。这很好。

在活动中,我在某个点加载另一个布局,然后再次加载包含MapView的第一个布局。此时,MapView在视觉上消失,只显示一个巨大的空白 - 只是空白,没有灰色网格。

我在活动类中包含了我的地图和MapView变量:

private GoogleMap mMap;
private MapView mapView;

我在onCreate

期间设置了地图
mapView = (MapView) findViewById(R.id.mapView);
mapView.onCreate(savedInstanceState);

mapView.getMapAsync(this);

mapView.onResume();

我正在填充地图

@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;
    mMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);

    LatLng tee = new LatLng(49.784299, 8.529582);
    LatLng green = new LatLng(49.782525, 8.527233);
    mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(tee, 17));
    mapView.onResume();

}

1 个答案:

答案 0 :(得分:0)

神秘解决了。我需要再次在MapView上调用onCreate()。基本上它在布局B中按钮的onClick事件中看起来像这样:

    // set layout A
    setContentView(R.layout.content_track_shot);
    mapView = (MapView) findViewById(R.id.mapView);
    // for testing purposes I created a private savedInstanceState copy in the activity's onCreate method so I could use it on the button's onClick event.
    mapView.onCreate(test);
    // mapView is created now (displayed gray), now let's fill it with life again
    mapView.getMapAsync(this);