Accessing ExtendedData information via Google Maps API v3

情到浓时终转凉″ 提交于 2019-12-30 10:06:18

问题


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.com/apis/kml/documentation/extendeddata.html and code.google.com/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?


回答1:


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!




回答2:


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);

    });



回答3:


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.




回答4:


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.com/kml/documentation/extendeddata' for reference.



来源:https://stackoverflow.com/questions/4578141/accessing-extendeddata-information-via-google-maps-api-v3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!