2013-08-11 23 views
10

Ich versuche, die $ Straße, $ city und $ country String von Google json zu bekommen. Es funktioniert für meine Heimatadresse: http://maps.googleapis.com/maps/api/geocode/json?latlng=52.108662,6.307370&sensor=trueErste Straße, Stadt und Land durch Reverse Geocoding google mit

$url = "http://maps.googleapis.com/maps/api/geocode/json?latlng=".$lat.",".$lng."&sensor=true"; 
    $data = @file_get_contents($url); 
    $jsondata = json_decode($data,true); 
    if(is_array($jsondata) && $jsondata['status'] == "OK") 
    { 
      $city = $jsondata['results']['0']['address_components']['2']['long_name']; 
      $country = $jsondata['results']['0']['address_components']['5']['long_name']; 
      $street = $jsondata['results']['0']['address_components']['1']['long_name']; 
    } 

aber für eine andere Adresse mit mehr Daten in den Feldern wie in diesem Beispiel: http://maps.googleapis.com/maps/api/geocode/json?latlng=52.154184,6.199592&sensor=true es nicht funktioniert, weil es mehr Daten im JSON-Array und es macht die Provinz zum Land.

Wie kann ich wählen Sie die Art, die ich brauche (long_name)?

  • für die Straße: long_name wo "Typ": [ "route"]
  • für Stadt: long_name wo "Typ": [ "Ort", "politischer"]
  • für Land: long_name wo " Typen“: [ "Land", "politische"]

Beispiel Ausgabe aus dem geocode JSON:

{ 
    "results" : [ 
     { 
     "address_components" : [ 
      { 
       "long_name" : "89", 
       "short_name" : "89", 
       "types" : [ "street_number" ] 
      }, 
      { 
       "long_name" : "Wieck De", 
       "short_name" : "Wieck De", 
       "types" : [ "establishment" ] 
      }, 
      { 
       "long_name" : "Industrieweg", 
       "short_name" : "Industrieweg", 
       "types" : [ "route" ] 
      }, 
      { 
       "long_name" : "Zutphen", 
       "short_name" : "Zutphen", 
       "types" : [ "locality", "political" ] 
      }, 
      { 
       "long_name" : "Zutphen", 
       "short_name" : "Zutphen", 
       "types" : [ "administrative_area_level_2", "political" ] 
      }, 
      { 
       "long_name" : "Gelderland", 
       "short_name" : "GE", 
       "types" : [ "administrative_area_level_1", "political" ] 
      }, 
      { 
       "long_name" : "Nederland", 
       "short_name" : "NL", 
       "types" : [ "country", "political" ] 
      }, 
      { 
       "long_name" : "7202 CA", 
       "short_name" : "7202 CA", 
       "types" : [ "postal_code" ] 
      } 

ich glaube, ich habe es selbst fixiert, hiermit mein Code:

// street 
foreach ($jsondata["results"] as $result) { 
    foreach ($result["address_components"] as $address) { 
     if (in_array("route", $address["types"])) { 
      $street = $address["long_name"]; 
     } 
    } 
} 
// city 
foreach ($jsondata["results"] as $result) { 
    foreach ($result["address_components"] as $address) { 
     if (in_array("locality", $address["types"])) { 
      $city = $address["long_name"]; 
     } 
    } 
} 
// country 
foreach ($jsondata["results"] as $result) { 
    foreach ($result["address_components"] as $address) { 
     if (in_array("country", $address["types"])) { 
      $country = $address["long_name"]; 
     } 
    } 
} 

Antwort

6

Sie könnten die Daten an das assoziativen Array umwandeln und wie mit ihm arbeiten

$data = array(); 
foreach($jsondata['results']['0']['address_components'] as $element){ 
    $data[ implode(' ',$element['types']) ] = $element['long_name']; 
} 
print_r($data); 

echo 'route: ' . $data['route'] . "\n"; 
echo 'country: ' . $data['country political']; 
3

Ihrem Code ist vollkommen gut, aber wäre es nicht besser sein, einen Schalter innerhalb 1 zu verwenden, foreach statt wiederholten foreach Schleifen? Hier ist, wie ich genau das gleiche Array analysieren:

$location = array(); 

    foreach ($result['address_components'] as $component) { 

    switch ($component['types']) { 
     case in_array('street_number', $component['types']): 
     $location['street_number'] = $component['long_name']; 
     break; 
     case in_array('route', $component['types']): 
     $location['street'] = $component['long_name']; 
     break; 
     case in_array('sublocality', $component['types']): 
     $location['sublocality'] = $component['long_name']; 
     break; 
     case in_array('locality', $component['types']): 
     $location['locality'] = $component['long_name']; 
     break; 
     case in_array('administrative_area_level_2', $component['types']): 
     $location['admin_2'] = $component['long_name']; 
     break; 
     case in_array('administrative_area_level_1', $component['types']): 
     $location['admin_1'] = $component['long_name']; 
     break; 
     case in_array('postal_code', $component['types']): 
     $location['postal_code'] = $component['long_name']; 
     break; 
     case in_array('country', $component['types']): 
     $location['country'] = $component['long_name']; 
     break; 
    } 

    } 
+0

das wird nicht funktionieren, weil die erste Komponente die zuverlässigste ist, sondern nur die 'cases' machen,' if' Loops und es wird gut. –

2

Wenn verwenden Sie Postleitzahl, die Adresse zu finden, wie ich vor kurzem erzeugt Straße, Stadt, Land mit Google Map API ist der Code:

$search_code = urlencode($postcode); 
     $url = 'http://maps.googleapis.com/maps/api/geocode/json?address=' . $search_code . '&sensor=false'; 
     $json = json_decode(file_get_contents($url)); 
     if($json->results == []){ 
      return ''; 
     } 
     $lat = $json->results[0]->geometry->location->lat; 
     $lng = $json->results[0]->geometry->location->lng; 

     //Now build the actual lookup 
     $address_url = 'http://maps.googleapis.com/maps/api/geocode/json?latlng=' . $lat . ',' . $lng . '&sensor=false'; 
     $address_json = json_decode(file_get_contents($address_url)); 

     $address_data = $address_json->results[0]->address_components; 
     //return $address_data = $address_json->results[0]->formatted_address; 

     $street = str_replace('Dr', 'Drive', $address_data[1]->long_name); 
     $town = $address_data[2]->long_name; 
     $county = $address_data[3]->long_name; 

     return $street.', '. $town. ', '.$county; 
Verwandte Themen