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 ?

有帮助吗?

解决方案

You can set onMarkerClickListener in GoogleMap object

Java:

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

In Kotlin, you can simply do:

map.setOnMarkerClickListener { true }

其他提示

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.

You can use onMarkerClickListener for this like below,

mMap.setOnMarkerClickListener( new OnMarkerClickListener()
{
    @Override
    public boolean onMarkerClick ( Marker marker )
    {
        // do nothing
        return false;
    }
});
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top