Collapsed markers on Google Maps with MarkerClusterer
I'm using GMaps V3 with MarkerClusterer from http://code.google.com/p/gmaps-utility-library-dev/
Markers displayed on the ma开发者_如何转开发p are user-defined and often collapse, so no matter how much you zoom-in, the clusterer always shows there are multiple items in one location. But the user should be able to click each single marker to see the info window.
I tried randomizing location a bit on the server, but the random is not random enough and markers keep collapsing, just not so often.
Do you know any way to deal with this?
I had this problem and solved it by defining a constructor property of "maxZoom" that was lower than the maxZoom for the Google map:
var markerCluster = new MarkerClusterer(map, markers, { maxZoom: 18 });
Reply to OP's comment in my previous answer as it didn't fit in the comment box:
This prevents clusterer from grouping the markers, but when the coordinates are identical all markers stay on top of each other. Do you know any workaround for this?
I think that is the expected behaviour with Google maps - if markers have the exactly the same lat/lng then you get the markers on top of each other.
One possible workaround is to only add the first marker to the map and then when you attempt to add another marker to a lat/lng already defined (keep track of them with a JS map), you could add a count (or build up an array of marker info) and update the infowindow text with that information. E.g. clicking on the marker would then show an infowindow saying there are 15 "things" at this location and then list their names and attributes in a list under it...
Another possibility is to "dither" subsequent markers by adding a random number to the end of the decimal lat/lng. That way when the user zooms in all the way the markers will be slightly offset. This option feels a bit "dirty" though.
When adding a marker you can check if there are other markers at that same location and in the infowindow you could note that there are multiple markers.
精彩评论