2012-04-17 3 views
5

Ich habe eine Datenbank mit genauen Adressen (Straße, nein, Stadt, Region/Gebiet, Land). Ich frage mich jedoch, ob es eine Möglichkeit gibt, Google API zu verwenden, um den Stadtteil einer Stadt (z. B. "Manhattan") zu bekommen, wenn wir in New York sind?Geo-Coding-Adresse - erhalten Bezirk einer bestimmten Adresse (Google API)

die alle anderen Informationen, die ich bereits in der Datenbank haben, so würde ich brauche nur den Kreis, wenn es einen gibt (natürlich dies nur in größeren Städten ist) ...

UPDATE:

Ich fand diese Funktion auf http://www.techques.com/question/1-3151450/Google-geolocation-API---Use-longitude-and-latitude-to-get-address und versuchte, formatted_address zu sublocality (sogar andere wie short_name etc.) zu ändern, aber es gibt nichts zurück ... jede Hilfe würde sehr geschätzt werden! Vielen Dank!!

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

Antwort

5

Sie können die Nachbar wie folgt zugreifen:

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, das ist, was ich gesucht habe ... – Helmut

3

Sie finden diese Informationen in einer Geocode-Anfrage finden, wenn ein Ergebnis mit den zu types Sets bestehen

[ "sublocality", "political" ] 

Beispiel: 317 Madison Ave,New York City


Änderung der Funktion oben für einfachen Zugang der Antwort-Komponenten:

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

Danke Dr. Molle für Ihre Anser und Ihre Hilfe! Ich habe diese Funktion gefunden (siehe mein Postupdate), aber wenn ich einfach format_address in irgendwas anderes ändere, gibt es "unbekannte Adresse" zurück ... hast du eine Idee, wie man die Unterlocation nur mit dieser Funktion bekommt? – Helmut

+0

hinzugefügt eine modifizierte Funktion für den einfachen Zugriff von einzelnen Komponenten der Antwort. –

0

ich mit folgendem kam.

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

Funktion latlng() { var geocoder = neu google.maps.Geocoder(); . var Adresse = 'Lindwurmstraße, München, Deutschland' // $ ('# location') val() geocoder.geocode ({ 'Adresse': Adresse} function (Ergebnisse, Status) { if (Status == google.maps.GeocoderStatus.OK) { var latitude = Ergebnisse [0] .geometry.location.lat(); var longitude = Ergebnisse [0] .geometry.location.lng(); $ ('# Breite '). val (Breitengrad) $ (' # Länge '). val (Länge) Geocode() } }); } // Ende –

+0

Was soll dieser Kommentar bedeuten? –

Verwandte Themen