简体   繁体   English

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

[英]Google Maps API with PHP to find distance between two locations

On my current project, which is a delivery system, I have a list of available delivery drivers, which is shown on an orders page. 在我当前的项目,即交付系统,我有一个可用的交付驱动程序列表,显示在订单页面上。 But what I need is to show the distance each delivery is from the customers address. 但我需要的是显示每个交付距离客户地址的距离。 The distance should be shown next to each driver's name. 应在每个驾驶员姓名旁边显示距离。 Anyone have any clues on how I would go about this? 任何人都有任何线索我会如何解决这个问题?

Assuming that you want driving distance and not straight line distance, you can use the Directions web service: You need to make an http or https request from your PHP script and the parse the JSON or XML response. 假设您需要行驶距离而不是直线距离,则可以使用Directions Web服务:您需要从PHP脚本发出http或https请求并解析JSON或XML响应。

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

For example: Boston,MA to Concord,MA via Charlestown,MA and Lexington,MA (JSON) http://maps.googleapis.com/maps/api/directions/json?origin=Boston,MA&destination=Concord,MA&waypoints=Charlestown,MA|Lexington,MA&sensor=false 例如:波士顿,MA到Concord,MA通过Charlestown,MA和Lexington,MA(JSON) http://maps.googleapis.com/maps/api/directions/json?origin=Boston,MA&destination=Concord,MA&waypoints=Charlestown ,MA |列克星敦,MA&传感器=假

Boston,MA to Concord,MA via Charlestown,MA and Lexington,MA (XML) http://maps.googleapis.com/maps/api/directions/xml?origin=Boston,MA&destination=Concord,MA&waypoints=Charlestown,MA|Lexington,MA&sensor=false 马萨诸塞州波士顿,马萨诸塞州康科德市,马萨诸塞州查尔斯顿市和马萨诸塞州列克星敦市(XML) http://maps.googleapis.com/maps/api/directions/xml?origin =波士顿,马萨诸塞州和目的地= Concord,MA&waypoints = Charlestown,MA |列克星敦,MA&传感器=假

Note that there are usage limits. 请注意,有使用限制。

You can easily calculates the distance between two address using Google Maps API and PHP. 您可以使用Google Maps API和PHP轻松计算两个地址之间的距离。

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

You can get the getDistance() function codes from here - http://www.codexworld.com/distance-between-two-addresses-google-maps-api-php/ 您可以从这里获取getDistance()函数代码 - http://www.codexworld.com/distance-between-two-addresses-google-maps-api-php/

this will out seconds between 2 location 这将在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'];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM