How to parse an XML file for marker locations and a graph on a map

I am trying to read points from an XML file, not from javascript, as in the example below.

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

But this does not work for me. I 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 can’t get the display of points on Google Maps v3. Does anyone have an example of parsing an XML file for coordinates and then displaying them on a map?

0
source share
2 answers

Brilliant - thanks a lot for the tip! There is another error in the above code:

replace

markers.setMap (map);

marker.setMap (map);

... then it will work!

+1
source

I use jQuery both to get the XML file and to parse it. I have used this approach many times, but I don’t have time to check it, therefore syntax errors are possible.

 <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); }); }); } 
0
source

All Articles