Hi, I have a list of markers in an XML file. I want to calculate the distance between each of those markers.
I am trying to use the GDirections object to calculate the distance. I am not not well versed with Java scripts, but I do know that the GDirections feature works asynchronously and one should add a listener. Here's the code: ---------------------------------------------------- <!DOCTYPE html "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8"/ > <title>Google Maps JavaScript API Example</title> <script src="http://maps.google.com/maps? file=api&v=2&key=ABQIAAAAzux9E8IukW6Ti9tFK6txRBTh9x8Y- o9BOHklIrUs1UXEtkf6FxQA_NaulKv7K09mFIfaIzmuWWtljQ&sensor=true_or_false" type="text/javascript"></script> <script type="text/javascript"> var finalArray = new Array(); function initialize() { if (GBrowserIsCompatible()) { var map = new GMap2(document.getElementById("map_canvas")); map.setCenter(new GLatLng(48.8610789316, 2.40036207675), 13); map.setUIToDefault(); map.addOverlay(new GMarker(48.8610789316, 2.40036207675)); GDownloadUrl("stations.xml", function(data, responseCode) { // To ensure against HTTP errors that result in null or bad data, // always check status code is equal to 200 before processing the data if(responseCode == 200) { var points = new Array(); var xml = GXml.parse(data); var markers = xml.documentElement.getElementsByTagName("marker"); for (var i = 0; i < markers.length; i++) { var stationNumber = markers[i].getAttribute("number"); var temp = new GLatLng(parseFloat(markers [i].getAttribute("lat")), parseFloat(markers[i].getAttribute ("lng"))); points[stationNumber] = temp; //map.addOverlay(new GMarker(temp)); } var gDir = new GDirections(); var pointsArray = [points[20020],points[20019]]; finalArray[0]["station1"] = 20020; finalArray[0]["station2"] = 20019; GEvent.addListener(gDir, "load", onGDirectionsLoad); gDir.loadFromWaypoints(pointsArray); alert(distance); } else if(responseCode == -1) { alert("Data request timed out. Please try later."); } else { alert("Request resulted in error. Check XML file is retrievable."); } }); } } function onGDirectionsLoad() { finalArray[0]["distance"] = gDir.getDistance().meters; alert(finalArray[0]["distance"]); } </script> </head> <body onload="initialize()" onunload="GUnload()"> <div id="map_canvas" style="width: 500px; height: 300px"></div> </body> </html> ----------------------------------------------------------------------------- Can someone please tell me if I missing anything here? The method onGDirectionsLoad never seems to get invoked.. Thanks! --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Google Maps API" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/Google-Maps-API?hl=en -~----------~----~----~----~------~----~------~--~---
