Use Google Maps API to get point and point distance of the JS code _javascript tips

Source: Internet
Author: User
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!

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.