Use the Google map API to get javascript code _ javascript skills for the distance between a vertex and a vertex

Source: Internet
Author: User
The following code uses the Google map API to obtain the distance between vertices () The 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!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.