Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I have several thousand markers stored in a kmz file that I load into a Google Maps (v3 API).

I've looked around to find a way to cluster the overlay data, but to no avail.
MarkerClusterer looks nice, but it only seems to work with a marker array?

function initialize() {
    geocoder = new google.maps.Geocoder();
    var latlng = new google.maps.LatLng(25,-20);
    var myOptions = {
        zoom: 2,
        center: latlng,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
    var ctaLayer = new google.maps.KmlLayer('thedata.kmz');
    ctaLayer.setMap(map);

    //cluster
    var mcOptions = {gridSize: 50, maxZoom: 15};
    var mc = new MarkerClusterer(map, ctaLayer, mcOptions);
}

How can I cluster the markers in the overlay, just like markerclusterer does it?

share|improve this question
add comment

migrated from stackoverflow.com Jun 8 '11 at 19:17

This question came from our site for professional and enthusiast programmers.

2 Answers

up vote 4 down vote accepted

The KML file is actually read by a Google server, then served to the map as image tiles. Thus, you don't have access to the actual point objects. If you want to do clustering, you would have to load the KML another way and parse the points into an array, and then you can use whatever client-side clustering you want.

From the Google Maps V3 API docs:

Because the components within a KmlLayer are rendered on demand, the layer allows you to easily manage the rendering of thousands of markers, polylines, and polygons. Note that you can't access these constituent objects directly, though they each provide click events which return data on those individual objects.

share|improve this answer
    
I have about 3500 markers in the kml/kmz file. Wouldn't it be very memory consuming to load them into an javascript array? Also, I imagine it would take quite a while to load the markers. –  reggie Jun 13 '11 at 9:51
    
There various solutions to the clustering approach. Generally, server side options are the best. However, if you want to go client side only, you don't necessarily need to load all 3500 points all the time, e.g. you could just load them for a specific scale and/or extent. –  Anthony -GISCOE- Jun 13 '11 at 16:29
    
So what is a good server-side solution? Using simpleXML to load the kml and then output it as a (very big) array in the google maps javascript? –  reggie Jun 19 '11 at 8:37
    
Use the server to do to clustering (e.g. just google for 'google maps server side clustering'). The client would then make requests to get only the points for the cluster locations, not all the data. –  Anthony -GISCOE- Jun 21 '11 at 13:02
add comment

This is possible. You can use a Javascript KML parser library that gets you access to all the data in each marker.

In Maps V2 the best one was here:

econym.org.uk/gmap/egeoxml.htm

In V3 you can use one of these 2:

code.google.com/p/geoxml3/

code.google.com/p/geoxml/

Once used, you can do things like loop through each KML marker, and use javascript to group points in the same location, and use JS to show different info windows for each marker in the same place.

I do that on my Your Mapper site here. Click a black numbered marker to see it in action:

http://www.yourmapper.com/map/111/crime-reports/crime-in-metro-louisville-ky-since-jan-2003.htm

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.