Click here to Skip to main content
15,885,182 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have to read latitudes and longitudes from an xml file and plot it on the map. Below is js:

C#
function initialize() {
        var latlng = new google.maps.LatLng(18.73033, 72.87844);

        var mapOpts = {
            zoom: 3,
            center: latlng,
            mapTypeId: google.maps.MapTypeId.ROADMAP,
        };

        var map = new google.maps.Map(document.getElementById("map_canvas"),
            mapOpts);

        downloadUrl("output.xml", function (data) {
            var xmlDoc = xmlParse(data);
            var records = xmlDoc.getElementsByTagName("Table");


            for (var i = 0; i < records.length; i++) {
                var rec = records[i];


                var lat = parseFloat(rec.getAttribute("lat"));
                var lng = parseFloat(rec.getAttribute("long"));

                var pt = new google.maps.LatLng(lat, lng);
                alert(pt)


                createMarker(pt, map);
            }
        })
    }

But nothing is being plot. On applying alert i found nothing is comming in lat, lng. That means the java script is not getting 'output.xml'. Please tell me where to store xml file.
Posted
Comments
ZurdoDev 5-Apr-13 8:04am    
Have you read google's api docs?

you could use jQuery to read the xml document.

JavaScript
$.get('locations.xml', function(xml) {
     $(xml).find('location').each(function() {
        var lat = parseFloat($(this).find('lat').text());
        var lng = parseFloat($(this).find('lng').text());
            var pt = new google.maps.LatLng(lat, lng);
        createMarker(pt, map);
})
});


in this case the xml looks like

XML
<?xml version= "1.0"?>
<locations>
   <location>
     <lat>0.00000</lat>
     <lng>0.00000</lng>
   </location>
   <location>
     <lat>1.00000</lat>
     <lng>1.00000</lng>
   </location>
   <location>
     <lat>2.00000</lat>
     <lng>2.00000</lng>
   </location>
   <location>
     <lat>3.00000</lat>
     <lng>3.00000</lng>
   </location>
   <location>
     <lat>4.00000</lat>
     <lng>4.00000</lng>
   </location>
   <location>
     <lat>5.00000</lat>
     <lng>5.00000</lng>
   </location>
</locations>


hope this helps
 
Share this answer
 
v2
$.get('locations.xml', function(xml)
Is there any need to provide path like 'xml/locations.xml'?
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900