首页 文章

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

提问于
浏览
15

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

1 回答

  • 34

    你需要在Google Maps APIGClientGeocoder对象上使用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 :好的你正在做这个东西服务器端 . 这意味着您需要使用HTTP Geocoding service . 为此,您需要使用链接文章中描述的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. Google Maps terms of service明确声明禁止将结果放在Google Map上进行地理编码数据 .

相关问题