且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2023-02-01 22:55:33

假设您正在寻找 geographic距离,首先您需要使用 Google 地图服务器端地理编码服务,如下例所示:

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:

$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

然后您可以使用 great-circle 计算两个邮政编码的坐标之间的距离距离实现如下:

Then you can calculate the distance between the coordinates of your two postcodes by using a great-circle distance implementation such as the following:

请注意,服务器端地理编码服务只能与在 Google 地图上显示结果结合使用;Google Maps API 服务条款禁止地理编码结果而不显示在地图上许可限制.

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.

更新:

如果您正在寻找行驶距离而不是地理距离,请注意,目前没有记录和批准的方法可以通过服务器端的 HTTP 请求访问 Google Maps Directions API.

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.

然而,返回 JSON 输出的未记录方法如下:

Nevertheless, an undocumented method that returns a JSON output is the following:

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

这将以 JSON 格式返回行车路线以及总行车距离:"meters":122977.

This will return you the driving directions, along with the total driving distance in JSON format: "meters":122977.

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

The format of the q parameter 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:

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

请注意,这不仅没有记录,而且还可能违反 Google Maps API 条款和条件.

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.

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

You may also be interesting in checking out the following related articles: