如何计算PHP中两个纬度和经度之间的驾驶距离 [英] How to calculate driving distance between two latitude and longitudes in PHP
问题描述
我有一个包含许多经纬度的数据库。
我需要根据我可以手动设置的纬度和经度按照距离升序排列所有纬度和经度。
我使用了一个公式来解决这个问题,即:
pre code> SELECT id,(3959 * acos(cos(radians (37))* cos(弧度(lat))* cos(弧度(lng) - 弧度(-122))+ sin(弧度(37))* sin(弧度(lat))))AS距离
距标记距离< 25 ORDER BY距离LIMIT 0,20;
37和-122是纬度和经度,它将返回名为距离的字段中的所有值以排序的方式。
我的问题是,如果一个地方距离2公里,但驾驶距离为11公里,那不是我的必要条件。我需要一个基于谷歌地图的公式,它将计算并返回两个坐标之间的驾驶距离。 如果你需要驾驶距离,你可以使用 Google路线API (适用于单个路线,可能带有路标)或 Google距离矩阵API (可以提供多个来源/目的地的结果)
I have a database which contains many latitude and longitudes. I need to get all the latitudes and longitudes sorted in ascending order by distance with respect to a latitude and longitude which I can set manually. I used a formula to solve this problem which is:
SELECT id, ( 3959 * acos( cos( radians(37) ) * cos( radians( lat ) ) * cos( radians( lng ) - radians(-122) ) + sin( radians(37) ) * sin( radians( lat ) ) ) ) AS distance
FROM markers HAVING distance < 25 ORDER BY distance LIMIT 0 , 20;
37 and -122 are the latitudes and longitudes and it will return all the values in a field named distance in a sorted way. My problem is that's not my necessity if a spot is 2 km away but driving distance is 11 km. I need a formula based on google maps which will calculate and return the driving distance between two coordinates.
You can calculate the straight line distance, you can't "calculate" the driving distance without a routing database/engine.
If you need the driving distance, you can use either the Google directions API (for a single route, potentially with waypoints) or the Google distance matrix API (can provide results for multiple origin/destinations)
这篇关于如何计算PHP中两个纬度和经度之间的驾驶距离的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!