8

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?

2 Answers 2

5

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.

4
  • 1
    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
    Commented Jun 13, 2011 at 9:51
  • 1
    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. Commented Jun 13, 2011 at 16:29
  • 1
    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
    Commented Jun 19, 2011 at 8:37
  • 1
    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. Commented Jun 21, 2011 at 13:02
2

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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