MarkerClusterer

131 views
Skip to first unread message

pogue5

unread,
Feb 6, 2012, 1:35:20 PM2/6/12
to google-map...@googlegroups.com
Hello,

If I want to create a clusterclick event for each cluster icon on my map, do I need to create an array of MarkerClusterer Objects and create an event for each, or is there a simpler way?

Thanks,

Greg

Rossko

unread,
Feb 6, 2012, 2:29:12 PM2/6/12
to Google Maps JavaScript API v3
> do I need to create an array of MarkerClusterer Objects and create an event
> for each

That wouldn't help, each MC Object may contain one, none or many
clusters at any given view.

> or is there a simpler way?

This thread for example explores having a single clusterclick listener
examine the markers in the cluster
http://groups.google.com/group/google-maps-js-api-v3/browse_thread/thread/8f8a965fb35d5e08/824797ad51320fac

pogue5

unread,
Feb 6, 2012, 2:58:28 PM2/6/12
to google-map...@googlegroups.com
Thanks  A-lot,

That code should help me a great deal! I have another question though. It is important that when I click on a cluster, that it displays several bits of information; a portion of data for each of the markers therein. I have the data which is related to a particular cluster stored in an string array. For example if i have 5 markers, one entry in the array contains all five html strings. If I bind an infobubble to multiple markers; each in the same cluster- what will the result be? Will the information window display one window with the data I have binded to individual markers even though i have binded the same data to many? Hope this makes sense.

I appreciate your help.

Greg

xelawho

unread,
Feb 6, 2012, 4:22:30 PM2/6/12
to Google Maps JavaScript API v3
Will the information window display
> one window with the data I have binded to individual markers even though i
> have binded the same data to many? Hope this makes sense.

It depends on your marker creation routine. Say your markers have an
attribute "html" that can be accessed through markerArray[i].html and
you want to display that. You can use cluster.getMarkers() to get an
array of the markers in that particular cluster, then loop through
them looking for what you want:

google.maps.event.addListener(markerCluster, 'clusterclick',
function(cluster) {
cstring=""
clusterArray=cluster.getMarkers()
for (var i = 0; i < clusterArray.length; i++){
cstring+=clusterArray[i].html
}
infoWindow.setPosition(cluster.getCenter());
infoWindow.setContent(cstring);
infoWindow.open(map);
});

Rossko

unread,
Feb 6, 2012, 6:12:05 PM2/6/12
to Google Maps JavaScript API v3
> I have
> the data which is related to a particular cluster stored in an string
> array.

I don't really understand what you mean by 'cluster' in this context.

MarkerClusterer manages your markers for you; you create a bunch of
markers and pass them to MC. MC then decides which markers are in
view, which need to be grouped into clusters, which don't. Every time
the view changes, this is recalculated. Clusters can form and break
up as views change. There's no sense in some chunk of data
permanently associated with a transient cluster.

If you want to display a bunch of info about the markers that make up
an MC cluster, you identify that at the time you want it e.g. on a
click. You identify each of the markers comprising the (current)
cluster and retrieve the info about each, and assemble the whole as
you see fit.
Perhaps each of your marker objects would have a unique ID property
that you can use to index into an array.
Or, perhaps each of your marker objects would have the actual data
needed attached as a property.

xelawho

unread,
Feb 6, 2012, 6:35:25 PM2/6/12
to Google Maps JavaScript API v3
Clusters can form and break
> up as views change.  There's no sense in some chunk of data
> permanently associated with a transient cluster.

equally, it's a little odd opening an infowindow when clicking on a
cluster unless you have 'zoomOnClick' set to false... otherwise the
cluster has disappeared by the time the window opens...

pogue5

unread,
Feb 7, 2012, 8:42:58 AM2/7/12
to google-map...@googlegroups.com
I think I have almost accomplished my goal. In the following code, you set the cstring variable to clusterArray[i].html. I don't think a marker object has a string property. I couldn't find a property in the Marker library, so how do I display the string associated with a particular marker?

google.maps.event.addListener(
Message has been deleted

pogue5

unread,
Feb 7, 2012, 9:45:48 AM2/7/12
to google-map...@googlegroups.com
I'm trying to figure out how to attach data to a marker object as a property. This may be a problem since I don't think the marker object contains a property for storing string data. Any ideas?

Rossko

unread,
Feb 7, 2012, 10:16:29 AM2/7/12
to Google Maps JavaScript API v3
> I'm trying to figure out how to attach data to a marker object as a
> property. This may be a problem since I don't think the marker object
> contains a property for storing string data. Any ideas?

As with any other javascript object, you make one. You just need to
take care not have the name clashing with any existing marker property
- 'x' would be unwise, for example. Otherwise -
myMarker.myspecialproperty = "some text";

pogue5

unread,
Feb 7, 2012, 10:22:09 AM2/7/12
to google-map...@googlegroups.com
I ended up using the title attribute of the Marker variable and it seems to have worked. I just want to thank both of you for your help on this issue! I appreciate it..
Message has been deleted

Rossko

unread,
Feb 7, 2012, 2:50:14 PM2/7/12
to Google Maps JavaScript API v3
> I ended up using the title attribute of the Marker variable and it seems to
> have worked.

I'd include that under 'unwise'. The title attribute is already used
for another purpose, not for holding your arbritary content; some
future version of the API might just start laundering your content or
something else unexpected. Best to create your own attribute.
Reply all
Reply to author
Forward
0 new messages