Search code examples
androidandroid-layoutandroid-fragmentsandroid-activity

Center fixed marker in android v2 mapview


I want to set fixed marker in the center point of mapview while dragging map.It has been done at android Map V1 google mapview. But now it's deprecated.Now my question is, is it possible in android Map V2 google mapview ?(I have tried.but map doesn't show)


Solution

  • I'm betting you used getMapCenter() which, as per Google Maps for Android v2, no longer available to use. But no worries, just use this:

    GoogleMap.getCameraPosition().target
    

    It will return a LatLng object which basically represents the center of the map. You can then use it to reposition the marker to the center every time there's a drag event by assigning an OnCameraChangedListener to your GoogleMap.

    yourGMapInstance.setOnCameraChangeListener(new OnCameraChangedListener() {
        @Override
        public void onCameraChange (CameraPosition position) {
    
            // Get the center of the Map.
            LatLng centerOfMap = yourGMapInstance.getCameraPosition().target;
    
            // Update your Marker's position to the center of the Map.
            yourMarkerInstance.setPosition(centerOfMap);
        }
    });
    

    There. I hope this helped!