簡體   English   中英

使用Google Map API和PHP進行反向地理編碼以使用Lat,Long坐標獲取最近的位置

[英]Reverse Geocoding With Google Map API And PHP To Get Nearest Location Using Lat,Long coordinates

我需要一個函數來使用谷歌地圖api反向地理編碼和php從坐標(lat,long)獲取最近的地址或城市...請提供一些示例代碼

您需要在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地圖服務條款明確規定,禁止在不將結果放入Google地圖的情況下對地理數據進行地理編碼。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM