title: Getting street,city and country by reverse geocoding using google
body: <p>I'm trying to getting the $street, $city and $country string from google json.
  It works for my home address :
  <a href="http://maps.googleapis.com/maps/api/geocode/json?latlng=52.108662,6.307370&amp;sensor=true" rel="noreferrer">http://maps.googleapis.com/maps/api/geocode/json?latlng=52.108662,6.307370&amp;sensor=true</a></p>
  
  <pre><code>$url = "http://maps.googleapis.com/maps/api/geocode/json?latlng=".$lat.",".$lng."&amp;sensor=true";
      $data = @file_get_contents($url);
      $jsondata = json_decode($data,true);
      if(is_array($jsondata) &amp;&amp; $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'];
      }
  </code></pre>
  
  <p>But for a different address with more data in the arrays like this example:
  <a href="http://maps.googleapis.com/maps/api/geocode/json?latlng=52.154184,6.199592&amp;sensor=true" rel="noreferrer">http://maps.googleapis.com/maps/api/geocode/json?latlng=52.154184,6.199592&amp;sensor=true</a>
  it doesn't work, because there is more data in the json array and it makes the province the country.</p>
  
  <p>How can I select the type that I need (long_name)? </p>
  
  <ul>
  <li>for street    : long_name where "types" : [ "route" ] </li>
  <li>for city      : long_name where "types" : [ "locality", "political" ] </li>
  <li>for country   : long_name where "types" : [ "country",  "political" ]</li>
  </ul>
  
  <p>Example output from the geocode JSON:</p>
  
  <pre><code>{
     "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" ]
              }
  </code></pre>
  
  <p>I think I fixed it myself, hereby my code:</p>
  
  <pre><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"];
          }
      }
  }
  </code></pre>