javascript - not able to access global variable while using google maps api js -
in below code, not able access value of variable distances . think because of asynchronous call directionsservice.route. how can value variable distances ?
var totaldistance; var distances = new array(); var directionsdisplay; var directionsservice = new google.maps.directionsservice(); var map; var start = "abc xyz"; var end ; var points = new array("location abc", "location pqr", "location xyz", "location more", "and other location"); function initialize() { directionsdisplay = new google.maps.directionsrenderer(); var mapoptions = { center: new google.maps.latlng(13.0604220, 80.2495830), zoom: 10, maptypeid: google.maps.maptypeid.roadmap, draggablecursor: "crosshair" }; map = new google.maps.map(document.getelementbyid("map-canvas"), mapoptions); directionsdisplay.setmap(map); } function calcroute() { for(var j=0;j<points.length;j++) { end = points[j]; var waypoints = new array(); for(var i=0; i<points.length;i++) { if(i!=j) { waypoints.push({location:points[i], stopover: true}); } } var request = { origin: start, destination: end, waypoints: waypoints, optimizewaypoints: true, travelmode: google.maps.travelmode.driving }; directionsservice.route(request, function(response, status) { if (status == google.maps.directionsstatus.ok) { var route = response.routes[0]; totaldistance = 0; ( var i=0;i<route.legs.length;i++) { totaldistance+=route.legs[i].distance.value; } distances.push(totaldistance); } }); } /*now want distances value accessed here i.e outside loop.*/ /*so can compare distances obtained */ } google.maps.event.adddomlistener(window, 'load', initialize);
edit: have updated complete code. trying : have fixed start point , waypoints (order not fixed), trying optimize waypoints, end point not fixed, can optimize path, necessary provide end point while calling directionsservice.route method , taking 1 of waypoints end point , keeping rest other in waypoints , calculating total distance of route. each of waypoint become end point 1 one , , others remain waypoint. calculate total distance of combinations , show directions of route has minimum distance.
i avoid calling asynchronous functions inside loop. can pain keeping track of everything.
from understand of question trying find shortest route arbitrary number of destinations. instead of looping through each waypoint, pass starting address , of destinations distancematrix service returns of route lengths origin each waypoint. when results return sort shortest longest. longest destination end address. pass start address, end address, , remaining waypoints directionservice
optimizewaypoints
turned on.
demo: http://jsfiddle.net/bryan_weaver/snyj2/
relavent code:
var map; var origin = "4100 ashby road, st. ann, mo 63074" var destinations = [ "2033 dorsett village, maryland heights, mo 63043", "1208 tamm avenue, st. louis, mo 63139", "1964 s old highway 94 st charles, mo 63303"]; var directionsdisplay; var directionsservice = new google.maps.directionsservice(); function calculatedistances() { var service = new google.maps.distancematrixservice(); service.getdistancematrix({ origins: [origin], //array of origins destinations: destinations, //array of destinations travelmode: google.maps.travelmode.driving, unitsystem: google.maps.unitsystem.metric, avoidhighways: false, avoidtolls: false }, callback); } function callback(response, status) { if (status != google.maps.distancematrixstatus.ok) { alert('error was: ' + status); } else { //we have 1 origin there should 1 row var routes = response.rows[0]; var sortable = []; var resulttext = "origin: <b>" + origin + "</b><br/>"; resulttext += "possible routes: <br/>"; (var = routes.elements.length - 1; >= 0; i--) { var rtelength = routes.elements[i].duration.value; resulttext += "route: <b>" + destinations[i] + "</b>, " + "route length: <b>" + rtelength + "</b><br/>"; sortable.push([destinations[i], rtelength]); } //sort result lengths shortest longest. sortable.sort(function (a, b) { return a[1] - b[1]; }); //build waypoints. var waypoints = []; (j = 0; j < sortable.length - 1; j++) { console.log(sortable[j][0]); waypoints.push({ location: sortable[j][0], stopover: true }); } //start address == origin var start = origin; //end address furthest desitnation origin. var end = sortable[sortable.length - 1][0]; //calculate route waypoints calculateroute(start, end, waypoints); //log routes , duration. $('#results').html(resulttext); } } //calculate route of shortest distance found. function calculateroute(start, end, waypoints) { var request = { origin: start, destination: end, waypoints: waypoints, optimizewaypoints: true, travelmode: google.maps.travelmode.driving }; directionsservice.route(request, function (result, status) { if (status == google.maps.directionsstatus.ok) { directionsdisplay.setdirections(result); } }); } function initialize() { directionsdisplay = new google.maps.directionsrenderer(); var centerposition = new google.maps.latlng(38.713107, -90.42984); var options = { zoom: 12, center: centerposition, maptypeid: google.maps.maptypeid.roadmap }; map = new google.maps.map($('#map')[0], options); geocoder = new google.maps.geocoder(); directionsdisplay.setmap(map); calculatedistances(); } google.maps.event.adddomlistener(window, 'load', initialize);
Comments
Post a Comment