Android - Google Map make marker sticky

2019-08-29 09:48发布

问题:

I have following marker code on my MapFragment. I want this marker stick to it's position. Means I want marker at center position in map and when I move/drag map then marker shouldn't get moved. How to do that with following marker?

PlaceMarker = PlaceMap.addMarker(
                            new MarkerOptions()
                            .position(new LatLng(0, 0))
                            .draggable(true)
                            .title("Drag Me"));

            PlaceMarker.showInfoWindow();

Thanks

回答1:

You can add a listener when the user pans the map and set the marker on the center.

mMap.setOnCameraChangeListener(new OnCameraChangeListener() {
        public void onCameraChange(CameraPosition cameraPosition) {   
            mMap.clear();               
            mMap.addMarker(new MarkerOptions().position(cameraPosition.target));
        }
});

Another way of doing this is by overlaying a marker icon (Not a real marker; defined in your XML layout) and get the location when the user sets that as a location.



回答2:

You would want to set draggable to false

public void setDraggable (boolean draggable)

The sets the draggability of the marker.