使用 PHP 和 google Maps Api 计算出 2 个邮政编码之间的距离(英国)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2296087/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 05:55:18  来源:igfitidea点击:

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

phpgoogle-maps

提问by dotty

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.

任何人都有任何想法或示例链接。

回答by Daniel Vassallo

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 servicesas in the following example:

假设您正在寻找地理距离,首先您需要使用谷歌地图服务器端地理编码服务获取两个邮政编码的纬度和经度,如下例所示:

$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 distanceimplementation such as the following:

然后,您可以使用如下所示的大圆距离实现来计算两个邮政编码的坐标之间的距离

Note that the server-side geocoding service may only be used in conjunction with displaying results on a Google map; geocoding results without displaying them on a map is prohibited by the Google Maps API Terms of Service License Restrictions.

请注意,服务器端地理编码服务只能与在 Google 地图上显示结果结合使用;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 qparameter should be from:xxx%20to:yyy. Replace xxx and yyy with the start and destination respectively. You can use latitude and a longitude coordinates instead of full addresses:

q参数的格式应该是from:xxx%20to:yyy. 分别用开始和目的地替换 xxx 和 yyy。您可以使用纬度和经度坐标而不是完整地址:

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:

您可能还对以下相关文章感兴趣:

回答by Cuse70

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

回答by Nao

Building on Daniel's answer, you can use Google's Geocoding APIto 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=bicyclingwith your preferred parameter:

请务必将&mode=bicycling替换为您的首选参数:

  • &mode=driving
  • &mode=bicycling
  • &mode=transit
  • &mode=walking
  • &mode=驾驶
  • &mode=骑自行车
  • &mode=公交
  • &mode=步行

回答by Layke

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

回答by Traveling Tech Guy

The Google Maps API is in JavaScript, not PHP. To achive what you want, turn the 2 post codes to LatLang coordinates and use the function distanceFromto find the distance between them.

Google Maps API 使用的是 JavaScript,而不是 PHP。要达到您想要的效果,请将 2 个邮政编码转换为 LatLang 坐标并使用该函数distanceFrom找到它们之间的距离。

Take a look at this articlefor some sample code.

看看这篇文章的一些示例代码。