使用 Google Map API 和 PHP 进行反向地理编码以使用经纬度坐标获取最近位置
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2054635/
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
Reverse Geocoding With Google Map API And PHP To Get Nearest Location Using Lat,Long coordinates
提问by Murugesh
I need a function to get an nearest address or city from coordinates(lat,long) using google map api reverse geocoding and php... Please give some sample code
我需要一个函数来使用谷歌地图 api 反向地理编码和 php 从坐标(纬度,经度)获取最近的地址或城市...请给出一些示例代码
回答by RedBlueThing
You need to use the getLocationsmethod on the GClientGeocoderobject in the Google Maps API
您需要在Google Maps API 中的GClientGeocoder对象上使用getLocations方法
var point = new GLatLng (43,-75);
var geocoder = new GClientGeocoder();
geocoder.getLocations (point, function(result) {
// access the address from the placemarks object
alert (result.address);
});
EDIT: Ok. You are doing this stuff server side. This means you need to use the HTTP Geocoding service. To do this you will need to make an HTTP request using the URL format described in the linked article. You can parse the HTTP response and pull out the address:
编辑:好的。你在服务器端做这个东西。这意味着您需要使用HTTP 地理编码服务。为此,您需要使用链接文章中描述的 URL 格式发出 HTTP 请求。您可以解析 HTTP 响应并拉出地址:
// set your API key here
$api_key = "";
// format this string with the appropriate latitude longitude
$url = 'http://maps.google.com/maps/geo?q=40.714224,-73.961452&output=json&sensor=true_or_false&key=' . $api_key;
// make the HTTP request
$data = @file_get_contents($url);
// parse the json response
$jsondata = json_decode($data,true);
// if we get a placemark array and the status was good, get the addres
if(is_array($jsondata )&& $jsondata ['Status']['code']==200)
{
$addr = $jsondata ['Placemark'][0]['address'];
}
N.B.The Google Maps terms of serviceexplicitly states that geocoding data without putting the results on a Google Map is prohibited.
NB的服务的谷歌地图条款明确规定,没有把结果在谷歌地图的地理编码数据是禁止的。

