带有PHP的Google Maps API,用于查找两个位置之间的距离


Google Maps API with PHP to find distance between two locations

在我当前的项目(一个交付系统)上,我有一个可用的交付驱动程序列表,该列表显示在订单页面上。但我需要的是显示每次交付与客户地址的距离。距离应显示在每个驾驶员姓名旁边。有人知道我会怎么做吗?

假设您想要行驶距离而不是直线距离,则可以使用方向 Web 服务:您需要从 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|列克星敦,马萨诸塞州&传感器=假

波士顿,马萨诸塞州到康科德,马萨诸塞州,

途经查尔斯敦,马萨诸塞州和列克星敦,马萨诸塞州 (XML)http://maps.googleapis.com/maps/api/directions/xml?origin=Boston,MA&destination=Concord,MA&waypoints=Charlestown,MA|列克星敦,马萨诸塞州&传感器=假

请注意,存在使用限制。

您可以使用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'];