【发布时间】:2012-07-04 11:57:28
【问题描述】:
在我当前的项目(交付系统)中,我有一个可用的交付驱动程序列表,该列表显示在订单页面上。但我需要的是显示每次送货到客户地址的距离。距离应显示在每个驾驶员姓名旁边。有人对我将如何处理有任何线索吗?
【问题讨论】:
-
你到底想要什么??您是否希望函数计算坐标可用的 2 个位置之间的距离,或者您还想获取它们的地理位置坐标然后找到距离。
标签: php google-maps
在我当前的项目(交付系统)中,我有一个可用的交付驱动程序列表,该列表显示在订单页面上。但我需要的是显示每次送货到客户地址的距离。距离应显示在每个驾驶员姓名旁边。有人对我将如何处理有任何线索吗?
【问题讨论】:
标签: php google-maps
假设您想要行驶距离而不是直线距离,您可以使用路线网络服务: 您需要从 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
请注意,有使用限制。
【讨论】:
您可以使用 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/
【讨论】:
这将在 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'];
【讨论】: