3

I want to have the State and county from an Address using geocode with Google API. This is my code:

$geocode=file_get_contents('http://maps.google.com/maps/api/geocode/json?address='.$street.''.$plz.''.$city.'&sensor=false');
        $output= json_decode($geocode);
        $lat = $output->results[0]->geometry->location->lat;
        $long = $output->results[0]->geometry->location->lng;
        $formatted_adress = $output->results[0]->formatted_address;
        $state = $output->results[0]->address_components->administrative_area_level_2;
        $county = $output->results[0]->address_components->administrative_area_level_1;

latitude, longitude and formatted_adress works find and I can save the results in my database, but the state and county doesn't work.

This is the output:

    {
   "results" : [
      {
         "address_components" : [
            {
               "long_name" : "Hauptgasse",
               "short_name" : "Hauptgasse",
               "types" : [ "route" ]
            },
            {
               "long_name" : "Solothurn",
               "short_name" : "Solothurn",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "Solothurn",
               "short_name" : "Solothurn",
               "types" : [ "administrative_area_level_2", "political" ]
            },
            {
               "long_name" : "Solothurn",
               "short_name" : "Südost",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "Schweiz",
               "short_name" : "CH",
               "types" : [ "country", "political" ]
            },
            {
               "long_name" : "4500",
               "short_name" : "4500",
               "types" : [ "postal_code" ]
            }
         ],
         "formatted_address" : "Hauptgasse, 4500 Solothurn, Schweiz",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 47.2088079,
                  "lng" : 7.540025399999999
               },
               "southwest" : {
                  "lat" : 47.2065432,
                  "lng" : 7.535295899999999
               }
            },
            "location" : {
               "lat" : 47.20778,
               "lng" : 7.537360000000001
            },
            "location_type" : "GEOMETRIC_CENTER",
            "viewport" : {
               "northeast" : {
                  "lat" : 47.20902453029149,
                  "lng" : 7.540025399999999
               },
               "southwest" : {
                  "lat" : 47.20632656970849,
                  "lng" : 7.535295899999999
               }
            }
         },
         "partial_match" : true,
         "place_id" : "ChIJS4BfGfnXkUcRqLUKnJzN54U",
         "types" : [ "route" ]
      }
   ],
   "status" : "OK"
}
Michał Perłakowski
  • 88,409
  • 26
  • 156
  • 177
Andi Wyder
  • 95
  • 11

1 Answers1

1

According to your json data that you show you need to get state and county like below:-

$state = $output->results[0]->address_components[2]->long_name;// i assume `Solothurn` is state name
$county = $output->results[0]->address_components[4]->long_name; // i assume `Schweiz` is county name

if county name also Solothurn then just change county code like below:-

$county = $output->results[0]->address_components[3]->long_name;
Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98