How to parse xml file for marker locations and plo

2019-09-06 10:05发布

I am trying to read points from an xml file rather than from javascript as in the example below.

https://google-developers.appspot.com/maps/documentation/javascript/examples/marker-animations-iteration

But it is not working for me. I have created an xml file containing:

<?xml version="1.0" encoding="UTF-8"?>
<companies>
    <company>
        <lat>52.511467</lat>
        <lng>13.447179</lng>
    </company>
    <company>
        <lat>52.549061</lat>
        <lng>13.422975</lng>
    </company>
    <company>
        <lat>52.497622</lat>
        <lng>13.396110</lng>
    </company>
    <company>
        <lat>52.517683</lat>
        <lng>13.394393</lng>
    </company>
</companies> 

But I cannot get the points displaying on google maps v3. Does anyone have an example of parsing an xml file for coordinates and then displaying them on a map?

2条回答
够拽才男人
2楼-- · 2019-09-06 10:54

Brilliant - thanks a lot for the hint! One little mistake is still in the above code:

replace

markers.setMap(map);

by

marker.setMap(map);

...then it'll work!

查看更多
成全新的幸福
3楼-- · 2019-09-06 10:56

I use jQuery both to get the XML file and then to parse it. I've used this approach many times but don't have time to test this, so there may be syntax errors.

<script src="http://code.jquery.com/jquery-1.7.1.min.js"></script> 
...
var map;
function init() 
{
    map = new google.maps.Map("map_canvas");
    jQuery.get("companies.xml", {}, function(data) {
        jQuery(data).find("company").each(function() {
            var company = jQuery(this);
            var lat = jQuery(company).find("lat").text();
            var lon = jQuery(company).find("lng").text();
            var latlng = new google.maps.LatLng( parseFloat(lat), parseFloat(lon) );
            var marker = new google.maps.Marker( { position: latlng, }); 
            markers.setMap(map);
        }); 
    }); 
}
查看更多
登录 后发表回答