tassadar
tassadar

Reputation: 65

MapView show completly after screen turned on again

i've this problem with MapView inside my layout:

once added it shows buttons, google logo, the grid but no map. To make it visible i've to turn the screen off and then on. What's the problem?

Here my code

    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"));
        }
    });
}

Upvotes: 0

Views: 166

Answers (2)

tassadar
tassadar

Reputation: 65

I solved initializing the map during onCreateView and then adding it on UserInteraction requests

Upvotes: 2

GSutey
GSutey

Reputation: 316

Implement 'OnMapReadyCallback' to your activity or what ever you're using then in your onMapready method

@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);
            }
    }    

Upvotes: 0

Related Questions