Copy Code code as follows:
var request;
var distancearray = [];
function Getdistance () {
Distancearray = [];
var directionsservice = new Google.maps.DirectionsService ();
for (var a = 0; a < pointsarray.length; a++) {
for (var b = 0; b < pointsarray.length; b++) {
if (a!= b) {
request = NULL;
Request = {
Origin:pointsarray[a],
DESTINATION:POINTSARRAY[B],
TravelMode:google.maps.DirectionsTravelMode.DRIVING,
UnitSystem:google.maps.DirectionsUnitSystem.METRIC
};
Directionsservice.route (Request, function (response, status) {
if (status = = Google.maps.DirectionsStatus.OK) {
var myroute = Response.routes[0].legs[0].distance.value; Route length
Distancearray.push (Myroute);
}
});
}
}
}
}
There is a problem to communicate with you, using Google Maps API to get the amount of distance between the dots, how to ensure that the distance information is ordered?
For example: There are a,b,c three points in the pointsarray[of data, how can I order to get [a,b],[a,c],[b,a],[b,c],[c,a],[c,b] distance information.
In the above code, all the a!=b distances are passed to the Directionsservice.route for resolution, which means that the perimeter for loop control is not used. So the expectation of relying on a for loop to get the distance in order failed. However, if debugging is added, a step-by-step control of the process of its cyclic solution can be obtained, and an ordered distance array is derived. Do not have the solution Ah!