Copy codeThe Code is 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 (! = 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 );
}
});
}
}
}
}
I have a question: how can I ensure that the distance information obtained by myself is orderly when I use the Google map API to obtain the distance between batch points?
For example, how can I obtain [a, B], [a, c], [B, a], [B, c], [c, a], [c, B] distance information.
In the above Code, all! = The distance of B is transmitted to directionsService. route at a time to solve the problem. That is to say, the peripheral FOR loop control is useless. Therefore, the expectation FOR obtaining the distance one by the FOR loop fails. However, if debugging is added, the process of loop solution can be controlled step by step, but the ordered distance array can be obtained. It cannot be solved!