Android - Google Map make marker sticky

半腔热情 提交于 2019-12-12 04:56:50

问题


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.



来源:https://stackoverflow.com/questions/29473652/android-google-map-make-marker-sticky

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!