简体   繁体   English

如何使用PHP和Google API计算沿特定路径从A点到B点的总距离?

[英]How do I calculate total distance from Point A to point B along a certain path using PHP & the google API?

I need to calculate the total distance traveled from a location to a destination. 我需要计算从一个位置到目的地的总距离。

However, it's not as simple as the distance between two points. 但是,它不如两点之间的距离那么简单。 I need to calculate the total distance along a certain path, so I'll have a few other locations that must be visited before I arrive at the destination. 我需要计算沿某条路径的总距离,因此在到达目的地之前,我将需要访问一些其他位置。

Is this possible via PHP, I've done it in JS but my PHP background is not as strong... 通过PHP可以做到这一点吗,我已经在JS中做到了,但是我的PHP背景却不那么强大...

So I ended up looking more into the issue. 所以我最终对这个问题进行了更多的研究。 It seems there's no straight API that will solely calculate the distance between multiple points... 似乎没有直接的API可以单独计算多个点之间的距离...

However, with a little bit of effort we are able to calculate the distance traveled using this API... 但是,通过一点点的努力,我们就能使用此API计算行进的距离...

https://developers.google.com/maps/documentation/directions/intro https://developers.google.com/maps/documentation/directions/intro

Essentially we want to get a route (index 0 should be fine) we then want to iterate over each of it's legs and add each legs distance.value to a total sum. 本质上,我们要获取一条路线(索引0应该很好),然后我们要遍历每条腿并将每条腿的distance.value加到一个总和上。 The result is the distance of all destinations... 结果就是所有目的地的距离...

Only downside, you also get a bunch of unnecessary information as this API is meant for giving directions. 唯一的缺点是,您还会得到很多不必要的信息,因为此API只是用于指示路线。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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