15

我需要一个函数来使用谷歌地图api反向地理编码和php从坐标(纬度,经度)获取最近的地址或城市......请提供一些示例代码

4

1 回答 1

34

您需要在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);
    });

编辑:好的。您正在服务器端做这些事情。这意味着您需要使用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'];
}

注意Google Maps 服务条款明确规定,禁止在未将结果放在 Google Map 上的情况下对数据进行地理编码。

于 2010-01-13T05:35:59.967 回答