Accessing ExtendedData information via Google Maps API v3

后端 未结 4 1710
天命终不由人
天命终不由人 2021-01-13 13:00

I have a KML file which contains within each node an node, and then a number of no

相关标签:
4条回答
  • 2021-01-13 13:15

    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);
    
        });
    
    0 讨论(0)
  • 2021-01-13 13:29

    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!

    0 讨论(0)
  • 2021-01-13 13:38

    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.

    0 讨论(0)
  • 2021-01-13 13:40

    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.

    0 讨论(0)
提交回复
热议问题