再次打开屏幕后,MapView将完整显示

时间:2016-03-23 10:34:07

标签: android android-maps-v2

我在布局中遇到了MapView的问题:

一旦添加它显示按钮,谷歌徽标,网格但没有地图。为了使其可见,我必须关闭屏幕然后再打开。问题是什么?

这是我的代码

    mapView = (MapView) geo.findViewById(R.id.map_view);
    mapView.onCreate(savedInstanceState);
    mapView.getMapAsync(this);
    final LocationManager lm = (LocationManager)getActivity().getSystemService(Context.LOCATION_SERVICE);
    lastKnownLocation = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
    if(lastKnownLocation == null) {
        askLocationRefreshInitMapView(lm);
    } else {
        if((System.currentTimeMillis() - lastKnownLocation.getTime()) > THIRTY_MINUTES) {
            askLocationRefreshInitMapView(lm);
        } else {
            initMapView(lastKnownLocation);
        }
    }



private void initMapView(final Location location) {
    lastKnownLocation = location;
    // Gets to GoogleMap from the MapView and does initialization stuff
    mapView.getMapAsync(new OnMapReadyCallback() {

        @Override
        public void onMapReady(GoogleMap map) {
            map.getUiSettings().setMyLocationButtonEnabled(false);
            map.setMyLocationEnabled(true);

            // Needs to call MapsInitializer before doing any CameraUpdateFactory calls
            try {
                MapsInitializer.initialize(getActivity());
            } catch (Exception e) {
                e.printStackTrace();
            }

            // Updates the location and zoom of the MapView
            if(location != null) {
                CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(new LatLng(location.getLatitude(), location.getLongitude()), 10);
                map.moveCamera(cameraUpdate);
                map.animateCamera(cameraUpdate);
            }
            map.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker"));
        }
    });
}

2 个答案:

答案 0 :(得分:2)

我解决了在onCreateView期间初始化地图,然后将其添加到UserInteraction请求

答案 1 :(得分:0)

为您的活动或您正在使用的活动实施'OnMapReadyCallback' 然后在你的onMapready方法

@Override
    public void onMapReady(GoogleMap googleMap) {
        MapsInitializer.initialize(context);
        gMap=googleMap;
     if (mapView != null) {
                // Initialise the MapView
                mapView.onCreate(null);
                // Set the map ready callback to receive the GoogleMap object
                mapView.getMapAsync(this);
            }
    }