Access ExtendedData information using the Google Maps API v3

I have a KML file that is contained in each <Placemark>node a <ExtendedData>node, and then several nodes <Data>with key / value pairs. I followed the following examples: http://code.google.com/apis/kml/documentation/extendeddata.html and code.google.com/apis/kml/documentation/kmlelementsinmaps.html suggests that maps support KML ExtendedData nodes (although partially), but I cannot find a way to access the ExtendedData object through javascript. I use:

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

(where agency_layer is a KML object). kmlEvent contains all the data in the KML function, but not extendedData, and I'm scratching my head. I want to make my KML semantically reasonable, and not load more data into the description and parse it later using javascript.

Does anyone have similar experience or know how to access ExtendedData nodes through the Google Maps v3 API?

+3
source share
4 answers

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

Since jQuery can easily parse xml and kml can parse xml, it works pretty well. Here is 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 contents of your value tags. Hope this helps!

+2
source

I am looking for the same thing. You can see what data is returned using the function of the JSON.stringify()object kmlEvent:

alert(JSON.stringify(kmlEvent));    

ExtendedData KML, Google, , .

0

Another solution may transfer data in the label description and preprocess when necessary.

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

    });
0
source

Some data is deleted from ExtendedData, but you can use getBalloonHtml()or getBalloonHtmlUnsafe()if you trust the KML source. See ' https://developers.google.com/kml/documentation/extendeddata ' for help.

0
source

Source: https://habr.com/ru/post/1783083/


All Articles