Android - Google Map make marker sticky

2019-08-29 10:05发布

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

2条回答
等我变得足够好
2楼-- · 2019-08-29 10:17

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.

查看更多
beautiful°
3楼-- · 2019-08-29 10:42

You would want to set draggable to false

public void setDraggable (boolean draggable)

The sets the draggability of the marker.

查看更多
登录 后发表回答