简体   繁体   中英

How to calculate distance between more than two points with google maps api?

Calculating distance between two point with google maps is easy and I did. But how to calculate distance between more then two marks in google map with the help of google maps api?

This is quite a broad use-case, But for a simple solution of your question I would recommend you using a Distance Matrix API . This would help you to find distances in between two or even more cities. The API request to find the distance in between 4 cities would be like this.

https://maps.googleapis.com/maps/api/distancematrix/json?origins=Vancouver+BC|Seattle&destinations=San+Francisco|Victoria+BC&mode=bicycling&language=fr-FR&key=API_KEY

This request will return four elements - two origins times two destinations:

Vancouver to San Francisco, Vancouver to Victoria, Seattle to San Francisco & Seattle to Victoria

Please take a look at the official Google Documentation of Distance Matrix API to know more about the API requests for more origins and destination.

One more option for you is to use a Direction API , but that would cover points in a path and there respective distance.

Hope that Helps!!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM