2012-04-17 18 views
5

Tôi có cơ sở dữ liệu với địa chỉ chính xác (đường phố, không, thành phố, vùng/khu vực, quốc gia). Tuy nhiên, tôi đã tự hỏi liệu có cách nào để sử dụng Google API để có được quận của một thành phố (ví dụ: "Manhattan") nếu chúng tôi ở New York không?Địa chỉ mã hóa địa lý - nhận quận của một địa chỉ nhất định (Google API)

Tất cả các thông tin khác mà tôi có đã có trong cơ sở dữ liệu, vì vậy tôi sẽ chỉ cần huyện nếu có một (tất nhiên điều này sẽ chỉ ở các thành phố lớn) ...

UPDATE:

Tôi tìm thấy chức năng này trên http://www.techques.com/question/1-3151450/Google-geolocation-API---Use-longitude-and-latitude-to-get-address và cố gắng để thay đổi formatted_address để sublocality (ngay cả những người khác như short_name vv) nhưng nó không trả lại bất cứ điều gì ... bất kỳ trợ giúp sẽ được đánh giá cao! Cảm ơn bạn!!

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; 
} 

Trả lời

5

Bạn có thể truy cập vào sublocality như thế này:

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, đây là những gì tôi đang tìm kiếm ... – Helmut

3

Bạn sẽ tìm thấy thông tin này bên trong một geocode-yêu cầu, khi có tồn tại một kết quả với các thiết lập types để

[ "sublocality", "political" ] 

Ví dụ: 317 Madison Ave,New York City


Modification của hàm ở trên để dễ dàng truy cập vào các thành phần phản hồi:

/** 
    * @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

Cảm ơn bạn Tiến sĩ Molle cho Anser của bạn và giúp đỡ của bạn! Tôi tìm thấy chức năng này (xem bài viết của tôi cập nhật), nhưng nếu tôi chỉ cần thay đổi formatted_address để bất cứ điều gì khác, nó trả về "địa chỉ không rõ" ... bạn có bất kỳ ý tưởng làm thế nào để có được sublocality chỉ với chức năng này? – Helmut

+0

đã thêm chức năng được sửa đổi để dễ dàng truy cập vào các thành phần đơn của phản hồi. –

0

tôi đã đưa ra những điều sau đây.

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

hàm latlng() { var geocoder = new google.maps.Geocoder(); địa chỉ var = 'Lindwurmstraße, Munich, Đức' // $ ('# location'). Val() geocoder.geocode ({'address': address}, function (kết quả, trạng thái) { if (status == google.maps.GeocoderStatus.OK) { var vĩ độ = kết quả [0] .geometry.location.lat(); var kinh độ = kết quả [0] .geometry.location.lng(); $ ('# vĩ độ '). val (vĩ độ) $ (' # longitude '). val (kinh độ) mã địa lý() } }); } // end –

+0

Nhận xét này có ý nghĩa gì? –