Android : If markers are overlapping each other on the map, then the click event is fired for the last hidden one
Asked Answered
D

2

8

In our application, a lot of markers are drawn in different locations and in certain cases at a certain zoom level, markers overlap each other. So when I click on the marker, I expect the top marker's onMarkerClick to be fired but instead it is fired for the last hidden marker i-e the last marker, with no markers behind it.

What do you suggest I do? Also, I have no info windows, therefore I return true from onMarkerClick method.

Diannediannne answered 7/2, 2014 at 6:9 Comment(1)
Did you implement any solution to this? I have the same issue. RegardsUlyanovsk
C
2

I found the solution here: https://github.com/googlemaps/android-maps-utils/issues/26

mGoogleMap.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter()
    {
        @Override
        public View getInfoWindow(Marker marker)
        {
            // Empty info window.
            return new View(getApplicationContext());
        }

        @Override
        public View getInfoContents(Marker marker)
        {
            return null;
        }
    });
Corrugation answered 7/1, 2016 at 0:57 Comment(0)
E
0
  1. Identify the size of your maker relative to the overall screen (e.g. 10%). Define this as a float called IMAGE_SIZE_RATIO

  2. Maintain List markers as you add markers to your map. In your OnMarkerClickListener onMarkerClick method iterate through your other markers and compare distance between the markers relative to the current visible map dimensions and the marker size. As in the following example code:

    static final float IMAGE_SIZE_RATIO = .15f; //define how big your marker is relative to the total screen
    private void setupListeners() {
    getMap().setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
    @Override
    public boolean onMarkerClick(Marker marker) {
        LatLngBounds b = getMap().getProjection().getVisibleRegion().latLngBounds;
        Float distance = distanceBetweenPoints(b.northeast, b.southwest) * IMAGE_SIZE_RATIO;
        for (Marker m : makers ) {
            if (marker.equals(m) ) { continue; } //don't compare the same one
            if (distanceBetweenPoint(m.getPosition(), marker.getPosition()) {
                /*Note do onMarkerClick this as an Aynch task and continue along 
                if also want to fire off against the main object.
                */
                return onMarkerClick(m);
            }
        }
        // do the operation you want on the actual marker.
    }
    ....(remainder of listener)...      
    }       
    }
    protected Float getDistanceInMeters(LatLng a, LatLng b) {
        Location l1 = new Location("");
        Location l2 = new Location("");
        l1.setLatitude(a.latitude);
        l1.setLongitude(a.longitude);
        l2.setLatitude(b.latitude);
        l2.setLongitude(b.longitude);
        return l1.distanceTo(l2)
    }
Esker answered 22/7, 2014 at 19:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.