admin管理员组

文章数量:1405412

I have a KML file which contains within each <Placemark> node an <ExtendedData> node, and then a number of <Data> nodes with key/value pairs. I've followed the examples at: .html and code.google/apis/kml/documentation/kmlelementsinmaps.html suggests maps do support KML ExtendedData nodes (albeit partially), but I cannot find a way of accessing the ExtendedData object via javascript. I'm using:

google.maps.event.addListener(agency_layer, 'click', function(kmlEvent) {
  console.debug( kmlEvent );
}

(where agency_layer is the KML object). kmlEvent contains all of the KML feature data, but not the extendedData, and i'm scratching my head. I want to make my KML semantically sensible, rather than loading more data into the description and parsing it later with javascript.

has anyone had a similar experience, or knows how to access ExtendedData nodes via Google Maps API v3?

I have a KML file which contains within each <Placemark> node an <ExtendedData> node, and then a number of <Data> nodes with key/value pairs. I've followed the examples at: http://code.google./apis/kml/documentation/extendeddata.html and code.google./apis/kml/documentation/kmlelementsinmaps.html suggests maps do support KML ExtendedData nodes (albeit partially), but I cannot find a way of accessing the ExtendedData object via javascript. I'm using:

google.maps.event.addListener(agency_layer, 'click', function(kmlEvent) {
  console.debug( kmlEvent );
}

(where agency_layer is the KML object). kmlEvent contains all of the KML feature data, but not the extendedData, and i'm scratching my head. I want to make my KML semantically sensible, rather than loading more data into the description and parsing it later with javascript.

has anyone had a similar experience, or knows how to access ExtendedData nodes via Google Maps API v3?

Share Improve this question edited Dec 24, 2013 at 16:59 Kara 6,22616 gold badges53 silver badges58 bronze badges asked Jan 2, 2011 at 11:14 Matthew KnightMatthew Knight 6315 silver badges13 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 2

I was looking for the exact same thing. I constructed a jQuery solution from the information I found here.

Since jQuery can parse xml easily, and kml is xml, it works out pretty well. Here's the function I wrote.

function extendedDataToArray(feature)
{
    var returnArray = new Array();
    $(feature.getKml()).find("Data").each(function()
    {
        returnArray[$(this).attr("name")] = $(this).find("value").text();
    }
    );
    return returnArray;
}

The function returns an associative array with keys equal to the names of your data elements, and values as the content of your value tags. Hope this helps!

Another solution might to pass data in the placemark description and preprocess description when it needs to be used.

/*
<Placemark>
    <name></name>
    <description><![CDATA[
    Lorem Ipsum  [data]{"datakey":524}[/data]
    ]]>
    </description>
    <Point>
        <coordinates>COORDINATES</coordinates>
    </Point>
</Placemark>
*/
   var map_overlay = new google.maps.KmlLayer(
        'URL_TO_KML_FILE',
        {
            'suppressInfoWindows': true
        }
    );
    map_overlay.setMap( gmap );

    var placemarkInfo = new google.maps.InfoWindow();
    google.maps.event.addListener(map_overlay, 'click', function (kmlEvent) {

        var text_to_process = kmlEvent.featureData.description,
        matches = text_to_process.match(/\[data\](.*)\[\/data\]/),
        json_data_string = matches[1],
        json_data = JSON.parse(json_data_string),
        real_description = text_to_process.split('[data]')[0];

        alert(json_data.datakey);

        placemarkInfo.setContent( real_description );
        placemarkInfo.setPosition(kmlEvent.latLng);
        placemarkInfo.open(gmap);

    });

I'm looking for the same thing. You can see what data is being returned by using the JSON.stringify() function on the kmlEvent object:

alert(JSON.stringify(kmlEvent));    

ExtendedData nodes are partially supported according to the KML Elements Supported in Google Maps page but I have yet to figure out how to properly use them.

Certain Data is stripped from the ExtendedData, but you can use the getBalloonHtml() or getBalloonHtmlUnsafe() if you trust the source of the KML. See 'https://developers.google./kml/documentation/extendeddata' for reference.

本文标签: javascriptAccessing ExtendedData information via Google Maps API v3Stack Overflow