2012-04-17 208 views
5

我有確切的地址(街道,無,城市,地區/區域,國家)的數據庫的地區。但是,我想知道如果我們在紐約,是否有辦法使用Google API獲取城市的區域(例如「曼哈頓」)?地理編碼地址 - 獲取某個地址(谷歌API)

所有我已經在數據庫中的其他信息,所以我只需要該地區是否有一個(當然這將是隻在較大的城市)...

UPDATE:

我在http://www.techques.com/question/1-3151450/Google-geolocation-API---Use-longitude-and-latitude-to-get-address上找到了這個功能,並試圖將formatted_address更改爲sublocality(即使其他人,如short_name等),但它不會返回任何內容......任何幫助將不勝感激!謝謝!!

function reverse_geocode($lat, $lon) { 
    $url = "http://maps.google.com/maps/api/geocode/json?latlng=$lat,$lon&sensor=false"; 
    $data = json_decode(file_get_contents($url)); 
    if (!isset($data->results[0]->formatted_address)){ 
     return "unknown Place"; 
    } 
    return $data->results[0]->formatted_address; 
} 

回答

5

您可以訪問sublocality這樣的:

function reverse_geocode($lat, $lon) { 
    $url = "http://maps.google.com/maps/api/geocode/json?latlng=$lat,$lon&sensor=false"; 
    $data = json_decode(file_get_contents($url)); 
    if (!isset($data->results[0]->address_components)){ 
     return "unknown Place"; 
    } 

    if ($data->results[0]->address_components[2]->types[0]=="sublocality") { 

     $return_array['type']="sublocality"; 
     $return_array['sublocality_long_name']=$data->results[0]->address_components[2]->long_name; 
     $return_array['sublocality_short_name']=$data->results[0]->address_components[2]->short_name; 

     return $return_array; 
     } 

} 
+0

THX,這就是我一直在尋找... – Helmut 2012-04-18 19:43:05

3

你會發現一個地址解析請求這裏面的信息,當存在結果與types設置爲

[ "sublocality", "political" ] 

例子:317 Madison Ave,New York City


功能的修改以上易於訪問響應組件:

/** 
    * @param $a mixed latitude or address 
    * @param $b mixed optional longitude when $a is latitude 
    * @return object geocoding-data 
    **/ 

    function geocode($a, $b=null) { 
    $params=array('sensor'=>'false'); 
    if(is_null($b)) 
    { 
     $params['address']=$a; 
    } 
    else 
    { 
     $params['latlng']=implode(',',array($a,$b)); 
    } 
    $url = 'http://maps.google.com/maps/api/geocode/json?'.http_build_query($params,'','&'); 
    [email protected]_get_contents($url); 

    $response=new StdClass; 
    $response->street_address    = null; 
    $response->route      = null; 
    $response->country      = null; 
    $response->administrative_area_level_1 = null; 
    $response->administrative_area_level_2 = null; 
    $response->administrative_area_level_3 = null; 
    $response->locality     = null; 
    $response->sublocality     = null; 
    $response->neighborhood    = null; 
    $response->postal_code     = null; 
    $response->formatted_address   = null; 
    $response->latitude     = null; 
    $response->longitude     = null; 
    $response->status      = 'ERROR'; 

    if($result) 
    { 
     $json=json_decode($result); 
     $response->status=$json->status; 
     if($response->status=='OK') 
     { 
     $response->formatted_address=$json->results[0]->formatted_address; 
     $response->latitude=$json->results[0]->geometry->location->lat; 
     $response->longitude=$json->results[0]->geometry->location->lng; 

     foreach($json->results[0]->address_components as $value) 
     { 
      if(array_key_exists($value->types[0],$response)) 
      { 
      $response->{$value->types[0]}=$value->long_name; 
      } 
     } 
     } 
    } 
    return $response; 
} 

//sample usage 
echo '<hr/>'.geocode('317 Madison Ave,New York City')->sublocality; 
    //Manhattan 

echo '<hr/>'.geocode('foobar')->status; 
    //ZERO_RESULTS 

echo '<hr/>'.geocode('40.689758, -74.04513800000001')->formatted_address; 
    //1 Liberty Is, Brooklyn, NY 11231, USA 
+0

謝謝莫爾博士的雁和您的幫助!我發現這個函數(見我的帖子更新),但如果我只是將formatted_address更改爲其他任何內容,它將返回「未知地址」...你有任何想法只有使用此函數才能獲得sublocality? – Helmut 2012-04-18 14:02:05

+0

增加了一個修改後的功能,可以輕鬆訪問單個響應組件。 – 2012-04-18 20:43:50

0

我想出了以下內容。

function geocode() { 
 
    var geocoder = new google.maps.Geocoder(); 
 
    var lat = $('#latitude').val() 
 
    var lng = $('#longitude').val() 
 
    var latlng = {lat: parseFloat(lat), lng: parseFloat(lng)}; 
 

 
    geocoder.geocode(
 
    {'location': latlng}, 
 
    function(results, status) { 
 
     if (status === 'OK') { 
 
     for (var i = 0; i < results[0].address_components.length; i++) 
 
     { 
 
      if (status == google.maps.GeocoderStatus.OK) { 
 
    \t \t \t \t if (results[0]) { 
 
    \t \t \t \t \t for (var i = 0; i < results.length; i++) { 
 
       //alert(results[i].types[0]+','+results[i].types[1]+','+results[i].address_components[0].long_name) 
 
       //district 
 
       if (results[i].types[0]=='political' && results[i].types[1]=='sublocality'){ 
 
        alert(results[i].address_components[0].long_name); 
 
       } 
 
       //City 
 
       if (results[i].types[0]=='locality' && results[i].types[1]=='political'){ 
 
        alert(results[i].address_components[0].long_name); 
 
       } 
 
       //country 
 
       if (results[i].types[0]=='country' && results[i].types[1]=='political'){ 
 
        alert(results[i].address_components[0].long_name); 
 
       } 
 
    \t \t \t \t \t } 
 
    \t \t \t \t } 
 
    \t \t \t \t else {console.log("No reverse geocode results.")} 
 
    \t \t \t } 
 
    \t \t \t else {console.log("Geocoder failed: " + status)} 
 

 

 
     } 
 
    }}) 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

+0

功能經緯度(){VAR 解析器=新google.maps.Geocoder(); (地址:地址),函數(結果,狀態){ if(status) == google.maps.GeocoderStatus.OK){ var latitude = results [0] .geometry.location.lat(); var longitude = results [0] .geometry.location.lng(); $('# valitude(緯度) }((#'longitude' } // end – 2018-02-07 23:14:40

+0

這個評論應該是什麼意思? – 2018-02-07 23:31:47