How do I disable click on a map marker in android?

烂漫一生 提交于 2019-12-24 13:16:29

问题


I've written my custom location listener which checks the user's location in every 10 minutes and updates a marker on the map which denotes the user's location. The problem is that the marker is clickable i.e. it shows a button to get directions to the marker. I want to disable that, how can I do that?

Here's the function which creates/updates the marker

 public void updateUserMarker() {
        Double temp_latitude = ((MainActivity)mContext).mLatitude;
        Double temp_longitude = ((MainActivity)mContext).mLongitude;

        if(mMap!=null) {
            if (user_marker == null) {
                MarkerOptions markerOptions = new MarkerOptions().position(new LatLng(temp_latitude, temp_longitude));
                markerOptions.icon(BitmapDescriptorFactory.fromResource(R.drawable.my_marker_icon));
                user_marker = mMap.addMarker(markerOptions);
            } else {
                user_marker.setPosition(new LatLng(temp_latitude, temp_longitude));
            }
        }

    }


回答1:


Try this code.

mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
        @Override
        public boolean onMarkerClick(Marker marker) {
            return true;
        }
    });

Returning true will also prevent info window from being opened.

For using this with a ClusterManager:

mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
        @Override
        public boolean onMarkerClick(Marker marker) {
            if (marker == user_marker) {
               return true;
            }
            return clusterManager.onMarkerClick(marker);
        }
    });


来源:https://stackoverflow.com/questions/29722649/how-do-i-disable-click-on-a-map-marker-in-android

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