使用Google Map API和PHP进行反向地理编码以使用Lat,Long坐标获取最近的位置

Mur*_*esh 15 php google-maps google-code reverse-geocoding

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

Red*_*ing 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);
    });
Run Code Online (Sandbox Code Playgroud)

编辑:好的.你正在做这个东西服务器端.这意味着您需要使用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'];
}
Run Code Online (Sandbox Code Playgroud)

注意:Google地图服务条款明确规定,禁止在不将结果放入Google地图的情况下对地理数据进行地理编码.

  • 老板,作为程序员的一部分是能够将源代码抽象到您的语言.Google Location Api在所有使用的语言中基本相同(包括GLatLng语法) (8认同)