Question

I am looking to create a Choropleth map made up of polygons overlaid atop Google Earth via the plug-in using JavaScript.

The polygons exist in a KML file, all with unique IDs, on a server (not necessarily mine). I want to be able to change the color of the polygon dynamically to show different data sets.

Is this doable?

I looked up the KML mechanism, but that only works with files on the same server.

Thanks,

Bill

Was it helpful?

Solution

You can load/parse remote KML via the Google Earth API from any network accessible URL (same server or otherwise) then iterate over the KML objects and programmatically change the styles and the polygon colors.

var href = 'http://code.google.com/'
           + 'apis/earth/documentation/samples/kml_example.kml';

google.earth.fetchKml(ge, href, function(kmlObject) {
      if (kmlObject) {
         checkObject(kmlObject);
         // append KML objects to current view
         ge.getFeatures().appendChild(kmlObject);
      }    
});

function checkObject(kmlObject) {
    var type = kmlObject.getType();         
    if (type == 'KmlDocument' || type == 'KmlFolder') {
        var features = kmlObject.getFeatures();
        if (features.hasChildNodes()) {
            var children = features.getChildNodes();                    
            for (i=0; i < children.getLength(); i++) {
                checkObject(children.item(i));                      
            }
        }
    } else if (type == 'KmlPlacemark') {
        // check/set style, change color, etc.
        // ...
    }
}

Reference: https://developers.google.com/earth/documentation/kml#fetchkml_and_parsekml

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top