How to disable marker click event?

我们两清 提交于 2019-12-18 05:40:49

问题


I am developing an simple app, where I already displayed marker title with mrkr.showInfoWindow() but I don't want user to tap again that marker. How to disable clicking event of particular marker ?

This is how I tried.

   Marker marker = gMap.addMarker(new MarkerOptions().position(new LatLng(location
                    .getLatitude(), location.getLongitude())).title("I am here").icon(BitmapDescriptorFactory.fromResource(R.drawable.mrk1)));
            marker.showInfoWindow();
//how to use marker.setClickable here or somthing here.?

How to display that infowindow()/title of my marker throughout my application ?


回答1:


You can set onMarkerClickListener in GoogleMap object

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



回答2:


It is clear by this point 3 years later, that there is no way to let marker clicks pass through to be handled by the map itself (OnMapClickListener). Markers will always intercept this event, although you can disable the default behavior from the click (as the answer above shows). The following is posted in case it can help someone still needing a better solution.

To work around this limitation, there are two options. I have implemented both, and can clean up and share the code on request.

1.) Create a GroundOverlay Bitmap that has the waypoints which you don't want to handle painted onto it. You use the GoogleMap's Projection object to convert from screen Points to LatLng, and calculate where to position the Bitmap to have the waypoints appear in the correct place. Its fairly complicated to handle rotation, map padding, and a few other issues. As you zoom in, the waypoint markers do grow in size, and then in the onCameraIdle event, you re-create the bitmap, and re-position the GroundOverlay if necessary. This is the method I am using in production. Panning, rotating and zooming are all super smooth, and as Ground Overlays can be set to handle or not handle taps, it solves the problem. I was not able to figure out how to make this work with tilt. The waypoints themselves do not stay facing North during rotation, but can be rotated upright when the GroundOverlay is re-created. There is a short pause/non-responsiveness of the map after the onCameraIdle causes the re-creation of the ground overlay.

2.) Create a View object that covers the entire screen, and paints the waypoint markers in the onDraw method of the view. Again, use the GoogleMap's projection object to calculate where to paint the markers. Call invalidate() on the view when in the onCameraMove event, so the markers are re-drawn at their new screen positions. The main advantage of this is simplicity. Rotation, tilt, map padding offsets are all handled for you. The main disadvantage is that if you invalidate on every onCameraMove event, it is a bit too cpu intensive (I presume) and the map panning / zooming can become choppy. If you invalidate every 3 or 5 move events, then the waypoints appear to be bouncing as you pan/zoom as they aren't repainted in their new position frequently enough. There is no delay at the end of the map move, though. Ultimately, with 100-200 waypoints on the map, the "while-panning" performance issues were too noticeable for me.

Hope this helps someone. Let me know if you need some code help.




回答3:


You can use onMarkerClickListener for this like below,

mMap.setOnMarkerClickListener( new OnMarkerClickListener()
{
    @Override
    public boolean onMarkerClick ( Marker marker )
    {
        // do nothing
        return false;
    }
});


来源:https://stackoverflow.com/questions/23197027/how-to-disable-marker-click-event

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