简体   繁体   English

使用PHP和谷歌地图Api计算2个邮政编码之间的距离(英国)

[英]Using PHP and google Maps Api to work out distance between 2 post codes (UK)

Hay, I wondering how to work out distance between 2 post codes using PHP and Google Maps Api. Hay,我想知道如何使用PHP和Google Maps Api计算2个邮政编码之间的距离。

Any one got any idea or links to examples. 任何人都有任何想法或链接到示例。

Assuming you are looking for the geographic distance , first you need to get the latitude and longitude of your two postcodes with the Google Maps server-side geocoding services as in the following example: 假设您正在寻找地理距离 ,首先您需要使用Google Maps服务器端地理编码服务获取两个邮政编码的纬度和经度,如下例所示:

$url = 'http://maps.google.com/maps/geo?q=EC3M,+UK&output=csv&sensor=false';

$data = @file_get_contents($url);

$result = explode(",", $data);

echo $result[0]; // status code
echo $result[1]; // accuracy
echo $result[2]; // latitude
echo $result[3]; // longitude

Then you can calculate the distance between the coordinates of your two postcodes by using a great-circle distance implementation such as the following: 然后,您可以使用大圆距离实现来计算两个邮政编码坐标之间的距离 ,如下所示:

Note that the server-side geocoding service may only be used in conjunction with displaying results on a Google map; 请注意,服务器端地理编码服务只能与在Google地图上显示结果一起使用; geocoding results without displaying them on a map is prohibited by the Google Maps API Terms of Service License Restrictions . Google Maps API服务条款许可限制禁止在地图上显示地理编码结果。


UPDATE: 更新:

If you are looking for the driving distance instead of the geographical distance, note that there is no documented and approved method at the moment to access the Google Maps Directions API via an HTTP request on the server-side. 如果您正在寻找行驶距离而不是地理距离,请注意目前没有经过记录和批准的方法可以通过服务器端的HTTP请求访问Google Maps Directions API。

Nevertheless, an undocumented method that returns a JSON output is the following: 然而,返回JSON输出的未记录方法如下:

http://maps.google.com/maps/nav?q=from:London%20to:Dover

This will return you the driving directions, along with the total driving distance in JSON format: "meters":122977 . 这将返回行车路线以及JSON格式的总行驶距离: "meters":122977

The format of the q parameter should be from:xxx%20to:yyy . q参数的格式应为from:xxx%20to:yyy Replace xxx and yyy with the start and destination respectively. 分别用起点和终点替换xxx和yyy。 You can use latitude and a longitude coordinates instead of full addresses: 您可以使用纬度和经度坐标而不是完整地址:

http://maps.google.com/maps/nav?q=from:51.519894,-0.105667%20to:51.129079,1.306925

Note that not only this is undocumented, but it may also violate the restrictions 10.1 and 10.5 of the Google Maps API Terms and Conditions . 请注意,这不仅是未记录的,而且还可能违反Google Maps API条款和条件的限制10.1和10.5。

You may also be interesting in checking out the following related articles: 您可能会对查看以下相关文章感兴趣:

Don't know if the V3 API changes anything, but I've been using this for quite a while and it still works: 不知道V3 API是否有任何变化,但我已经使用了很长一段时间它仍然有效:

From and to are each expressed as latitude,longitude (an address would probably work; I'm sure I tried it but don't remember and I had the coordinates anyway) From和to各自表示为纬度,经度(一个地址可能会起作用;我确定我尝试了但不记得了,我还是有坐标)

$base_url = 'http://maps.googleapis.com/maps/api/directions/xml?sensor=false';
$xml = simplexml_load_file("$base_url&origin=$from&destination=$to");
$distance = (string)$xml->route->leg->distance->text;
$duration = (string)$xml->route->leg->duration->text

Building on Daniel's answer, you can use Google's Geocoding API to get car, public transport, walking and cycling distances easily : 基于Daniel的答案,您可以使用Google的地理编码API轻松获取汽车,公共交通,步行和骑行距离:

$postcode1='W1J0DB';
$postcode2='W23UW';
$result = array();

$url = "http://maps.googleapis.com/maps/api/distancematrix/json?origins=$postcode1&destinations=$postcode2&mode=bicycling&language=en-EN&sensor=false";

$data = @file_get_contents($url);

$result = json_decode($data, true);
print_r($result);

Be sure to replace &mode=bicycling with your preferred parameter: 请务必使用您的首选参数替换&mode = bicycling

  • &mode=driving &模式=驱动
  • &mode=bicycling &模式=骑车
  • &mode=transit &模式=中转
  • &mode=walking &模式=行走

Take a look at the mailing list here: 看看这里的邮件列表:

http://groups.google.com/group/Google-Maps-API/browse_thread/thread/71e6aa2c3de66127 http://groups.google.com/group/Google-Maps-API/browse_thread/thread/71e6aa2c3de66127

Once you decide if you want a straight line or driving distance you can calculate it really easily. 一旦您决定是否需要直线或行驶距离,您就可以轻松地计算出来。 You don't have to use PHP but there are examples. 您不必使用PHP,但有一些示例。

Look at this also: 看看这个:

http://groups.google.com/group/google-maps-api/browse_thread/thread/d43de63d05adc62d/1a04053f39a3edf3?lnk=gst&q=calculate+distance#1a04053f39a3edf3 http://groups.google.com/group/google-maps-api/browse_thread/thread/d43de63d05adc62d/1a04053f39a3edf3?lnk=gst&q=calculate+distance#1a04053f39a3edf3

The Google Maps API is in JavaScript, not PHP. Google Maps API使用的是JavaScript,而不是PHP。 To achive what you want, turn the 2 post codes to LatLang coordinates and use the function distanceFrom to find the distance between them. 要获得所需内容,请将2个邮政编码转换为LatLang坐标,并使用函数distanceFrom查找它们之间的距离。

Take a look at this article for some sample code. 看看这篇文章的一些示例代码。

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

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