【问题标题】:to calculate driving distance between two coordinates on earth in php在php中计算地球上两个坐标之间的行驶距离
【发布时间】:2011-09-06 20:32:36
【问题描述】:

我正在使用基于位置的应用程序,我需要在服务器端找到地球上两个位置之间的行驶距离。我正在用 php 编写 Web 服务。

到目前为止,我可以使用 Haversine 公式计算空中距离,但现在我想计算行驶距离。

这个问题可能有重复,但我无法找到任何帮助来计算两个“坐标”(即纬度和经度)之间的“行驶距离”,而不是 PHP 中的地址

【问题讨论】:

  • 使用 Google Maps API。

标签: php coordinates distance latitude-longitude


【解决方案1】:

使用Google Distance Matrix API :-

Google 距离矩阵 API 是一个 提供旅行距离的服务 和时间矩阵的起源和 目的地。

这里是代码:-

$q = "http://maps.googleapis.com/maps/api/distancematrix/json?origins=Seattle&destinations=San+Francisco&mode=driving&sensor=false";

$json = file_get_contents($q);

$details = json_decode($json, TRUE);

echo "<pre>"; print_r($details); echo "</pre>";

Google Distance Matrix API了解更多信息

【讨论】:

    【解决方案2】:

    这应该让你开始......

    $json = file_get_contents('http://maps.google.com/maps/nav?q=from:Buderim,Australia%20to:Brisbane,Australia');
    
    $details = json_decode($json, TRUE);
    
    var_dump($details['Directions']['Distance']['meters']);
    

    【讨论】:

    • 感谢您的帮助,但我希望它在两个纬度、经度之间
    • @Saphire 把纬度和经度放在那里就好了。
    • @alex,这是公共 API 吗?或者只是谷歌使用的东西?如果它不是公共 API(即,谷歌说它是公共的),使用它可能是非法的。
    • @binaryLV 我认为它没有记录。
    • @SAPHIRE,你还想要什么? “2918 米”不是您想要的距离吗?
    猜你喜欢
    • 2016-07-16
    • 2012-08-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-27
    • 2020-02-12
    • 2018-06-12
    相关资源
    最近更新 更多