【问题标题】:Google Maps API with PHP to find distance between two locations使用 PHP 的 Google Maps API 查找两个位置之间的距离
【发布时间】:2012-07-04 11:57:28
【问题描述】:

在我当前的项目(交付系统)中,我有一个可用的交付驱动程序列表,该列表显示在订单页面上。但我需要的是显示每次送货到客户地址的距离。距离应显示在每个驾驶员姓名旁边。有人对我将如何处理有任何线索吗?

【问题讨论】:

  • 你到底想要什么??您是否希望函数计算坐标可用的 2 个位置之间的距离,或者您还想获取它们的地理位置坐标然后找到距离。

标签: php google-maps


【解决方案1】:

假设您想要行驶距离而不是直线距离,您可以使用路线网络服务: 您需要从 PHP 脚本发出 http 或 https 请求并解析 JSON 或 XML 响应。

文档: https://developers.google.com/maps/documentation/directions/

例如: 马萨诸塞州波士顿通过马萨诸塞州查尔斯敦和马萨诸塞州列克星敦前往马萨诸塞州康科德 (JSON) http://maps.googleapis.com/maps/api/directions/json?origin=Boston,MA&destination=Concord,MA&waypoints=Charlestown,MA|Lexington,MA&sensor=false

马萨诸塞州波士顿到马萨诸塞州康科德,途经马萨诸塞州查尔斯敦和马萨诸塞州列克星敦 (XML) http://maps.googleapis.com/maps/api/directions/xml?origin=Boston,MA&destination=Concord,MA&waypoints=Charlestown,MA|Lexington,MA&sensor=false

请注意,有使用限制。

【讨论】:

    【解决方案2】:

    您可以使用 Google Maps API 和 PHP 轻松计算两个地址之间的距离。

    $addressFrom = 'Insert from address';
    $addressTo = 'Insert to address';
    $distance = getDistance($addressFrom, $addressTo, "K");
    echo $distance;
    

    您可以从这里获取 getDistance() 函数代码 - http://www.codexworld.com/distance-between-two-addresses-google-maps-api-php/

    【讨论】:

    • JoyGuru,我一直在努力解决这个问题,这对你有用吗?
    • stackoverflow.com/questions/36143960/… - 刚刚发布了一个问题,也许您可​​以提供一些见解?我似乎一切都做得很好?只是想我会问。
    【解决方案3】:

    这将在 2 个位置之间持续几秒钟

    $origin =urlencode('Optus Sydney International Airport, Airport Drive, Mascot NSW 2020, Australia');
    $destination = urlencode('Sydney NSW, Australia');
    $url = "https://maps.googleapis.com/maps/api/distancematrix/json?units=imperial&origins=$origin&destinations=$destination&key=your_key";
    $data = @file_get_contents($url);
    $data = json_decode($data,true);
    echo $data['rows'][0]['elements'][0]['duration']['value'];
    

    【讨论】:

      猜你喜欢
      • 2015-03-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-12
      • 1970-01-01
      • 1970-01-01
      • 2014-10-21
      相关资源
      最近更新 更多