7

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.

Khalil
  • 360
  • 1
  • 4
  • 14

2 Answers2

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;
        }
    });
Jiajun
  • 71
  • 6
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)
    }
John Fontaine
  • 1,019
  • 9
  • 14