使用PHP的Google Maps API查找两个位置之间的距离

3
在我的当前项目中,这是一个交付系统,我有一个可用的交付司机列表,显示在订单页面上。但我需要显示每个交付员距离客户地址的距离。该距离应显示在每个司机姓名旁边。有人知道我该如何处理吗?

你到底想要什么?是想要一个函数来计算两个已知坐标位置之间的距离,还是想要获取它们的地理位置坐标并计算距离? - Neji
3个回答

6
假设您需要驾驶距离而非直线距离,则可以使用Directions网络服务:需要从您的PHP脚本发出http或https请求,然后解析JSON或XML响应。

Documentation: 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 请注意使用限制。

3

JoyGuru,我一直在尝试让它工作,这个对你有用吗? - Matthew
http://stackoverflow.com/questions/36143960/php-distance-between-2-addresses-with-google-maps - 刚刚发布了一个问题,也许你能提供一些见解?我似乎做得一切正确?只是想问问。 - Matthew

1
这句话的意思是:“这将在两个位置之间输出秒数。”
$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'];

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接