Erste Straße,Stadt und Land, durch reverse geocoding mit google

Ich versuche immer die $Straße, $city und $country string von google json.
Es funktioniert für meine home-Adresse :
http://maps.googleapis.com/maps/api/geocode/json?latlng=52.108662,6.307370&sensor=true

$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 die arrays wie in diesem Beispiel:
http://maps.googleapis.com/maps/api/geocode/json?latlng=52.154184,6.199592&sensor=true
es funktioniert nicht, weil es mehr Daten im json-array, und es macht die Provinz des Landes.

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

  • für Straße : long_name wo "types" : [ "route" ]
  • für die Stadt : long_name wo "types" : [ "locality", "political" ]
  • für Land : long_name wo "types" : [ "Land", "political" ]

Beispiel für die Ausgabe der 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 fixe es mich, 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"];
        }
    }
}

InformationsquelleAutor Ruben Bolink | 2013-08-11

Schreibe einen Kommentar