使用PHP和Google Maps Api来计算两个邮政编码(英国)之间的距离 [英] Using PHP and google Maps Api to work out distance between 2 post codes (UK)

查看:89
本文介绍了使用PHP和Google Maps Api来计算两个邮政编码(英国)之间的距离的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hay,我想知道如何使用PHP和Google Maps API来计算两个邮政编码之间的距离。



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

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

  $ 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]; //状态码
echo $ result [1]; //准确性
echo $ result [2]; //纬度
echo $ result [3]; // longitude

然后,您可以通过使用实现大圆距离,如下所示:



请注意,服务器端地理编码服务可能只是与在Google地图上显示结果一起使用;地理编码结果不会在地图上显示,这是 Google Maps API服务条款所禁止的许可限制






更新:
$ b

如果您正在寻找行车距离而不是地理距离,请注意,目前没有文档和批准的方法可通过服务器端的HTTP请求访问Google Maps Directions API。然而,一个返回JSON输出的未公开方法如下:

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

这将返回驾驶路线,以及JSON格式的总驾驶距离:meters:122977



q 参数的格式应该为:来自:xxx%20to:yyy 。将xxx和yyy分别替换为start和destination。您可以使用纬度和经度坐标,而不是完整的地址:

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

请注意,这不仅仅是没有记录,但也可能违反 Google Maps API条款和条件的限制10.1和10.5

您可能在查看以下相关文章时感兴趣:


Hay, I wondering how to work out distance between 2 post codes using PHP and Google Maps Api.

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:

$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; geocoding results without displaying them on a map is prohibited by the Google Maps API Terms of Service License Restrictions.


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.

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

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.

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

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:

这篇关于使用PHP和Google Maps Api来计算两个邮政编码(英国)之间的距离的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆