identifying clustered markers and non-clustered markers

1,293 views
Skip to first unread message

gdp

unread,
Jul 12, 2011, 3:22:35 AM7/12/11
to Google Maps JavaScript API v3
Hi - I am using clustering on my map and as I zoom in I will have some
markers on the map which are shown individually and some that are
grouped into a cluster. When I loop through all the markers within the
map bounds is it possible to tell which markers are showing up
individually and which are showing as part of a cluster?

Thanks very much for any help given.

GDP

DiTieM

unread,
Jul 12, 2011, 4:25:29 AM7/12/11
to google-map...@googlegroups.com
grouped into a cluster. When I loop through all the markers within the
map bounds is it possible to tell which markers are showing up
individually and which are showing as part of a cluster?


Depends on your algorithm or if you are using a third party library. If you
are doing it by yourself, then the answer is yes.

   David

gdp

unread,
Jul 12, 2011, 4:30:07 AM7/12/11
to Google Maps JavaScript API v3
well thanks david, any hints as to where to start?

gdp

unread,
Jul 12, 2011, 4:29:58 AM7/12/11
to Google Maps JavaScript API v3
well thanks david, any hints as to where to start?

On Jul 12, 9:25 am, DiTieM <dit...@gmail.com> wrote:

Rossko

unread,
Jul 12, 2011, 4:47:08 AM7/12/11
to Google Maps JavaScript API v3
> When I loop through all the markers within the
> map bounds is it possible to tell which markers are showing up
> individually and which are showing as part of a cluster?

Perhaps you could ask whatever mechanism you are using to create the
clusters, which markers it is handling? Perhaps you could remember
which markers you gave to it in the first place?

gdp

unread,
Jul 12, 2011, 5:02:52 AM7/12/11
to Google Maps JavaScript API v3
I am using

var mc = new MarkerClusterer(map, markers, mcOptions);

with marker being all the markers on the map - g maps then controls
the clustering of those markers according to zoom level so I am not
giving it a subset of markers which I can 'remember', I am giving it
all markers and it automatically clusters them (or not as the case
maybe)......I am new to this so may not be understanding the details
of clustering but that is what happens on my map.

DiTieM

unread,
Jul 12, 2011, 5:28:45 AM7/12/11
to google-map...@googlegroups.com
var mc = new MarkerClusterer(map, markers, mcOptions);

Ok, that is 3rd party. I had a quick look at the source code of MarkerClusterer
and I did not find a way to do what you are looking for. MarkerCluster only
send you the click event of markers. Not sure why you want to know the 
markers that appear with respect the last zoom. If you want to provide a
more general idea maybe we can help you a bit more.

The other hard way, is to make MarkerClusterer by yourself, but you really
need to be into google maps, js, projections... I will not recommend it unless
you are very brave :)

Regards,
  David

gdp

unread,
Jul 12, 2011, 5:39:53 AM7/12/11
to Google Maps JavaScript API v3
Thanks David

The reason I want to do this is I have a div next to the map in which
I want to display some details for individual markers. The scenario
is....

1. they open the map fully zoomed out - it shows no individual markers
and several clusters.

2. they click a cluster, the map zooms in and the cluster splits into
several individual markers and maybe some smaller clusters - at this
point I wish to loop through the markers that are within the map
bounds (I know how to do this and can identify the markers that are
within bounds but have no way of distinguishing those that are
individual and those that are still hidden in the smaller clusters) -
what I want if I can identify the markers that are individual is to
then add some info about them (and only them - not the ones still
clustered) to the div next to the map.

Hope that makes sense. I would love to post a link to the site but
cannot at present due it all being 'top secret' :)

Thanks again for any help.

Rossko

unread,
Jul 12, 2011, 5:42:30 AM7/12/11
to Google Maps JavaScript API v3
You might check each markers getVisible() if it is invisible then
MarkerCluster has hidden it e.g. it is in a cluster

gdp

unread,
Jul 12, 2011, 5:53:43 AM7/12/11
to Google Maps JavaScript API v3
Thanks Rossko - that sounds hopeful - I will have to try it this
evening but will post an update here as to whether or not that works
Message has been deleted

gdp

unread,
Jul 12, 2011, 3:50:19 PM7/12/11
to Google Maps JavaScript API v3
unfortunately the following code....

for ( i=0;i<markers.length;i++ ) {
if ( map.getBounds().contains(markers[i].getPosition()) ) {
console.log('marker');
if ( markers[i].getVisible() )
console.log(' - visible');
}

}


....finds getVisible() to be true for every marker when the map is
showing a mix of markers and clusters - even those hidden in a cluster

thanks anyway

gdp

On Jul 12, 10:42 am, Rossko <ros...@culzean.clara.co.uk> wrote:

Rossko

unread,
Jul 12, 2011, 4:16:16 PM7/12/11
to Google Maps JavaScript API v3
My mistake ; MarkerClusterer manages marker visibility by use of
setMap()
Try checking the getMap() value for null

gdp

unread,
Jul 12, 2011, 4:43:27 PM7/12/11
to Google Maps JavaScript API v3
Thats it!

substituting getMap() for getVisible() in the code gives me what I
want. The only anomaly is that often as the map is moved (this is set
up as below so it fires when the map is changed ).....

google.maps.event.addListener(map, 'idle', function() {

console.clear();
for ( i=0;i<markers.length;i++ ) {
if ( map.getBounds().contains(markers[i].getPosition()) ) {
console.log('marker');
if ( markers[i].getMap() )
console.log(' - visible');
}

}
});

......I get it reporting one extra marker - so if I can see within
bounds 7 individual and 1 cluster of two I will get a total marker
count of 10 markers. This is a small problem however now I know this
can be done, one that I can live with. Thanks very much for this
solution Rossko, really appreciate the help. Hope this code will be of
use to someone else too.

gdp
Reply all
Reply to author
Forward
0 new messages