1

I'm currently using Google Maps API to get a longitude and latitude form the user's street, street number and zip code.

I would also like to grab their city and state from the Maps API results. The XML document formats it strangely though, and I'm not sure how I'm supposed to reference it (it's different from the way I'm grabbing the long and lat).

This is how I am loading the Maps API:

$map_address = str_replace($address."+".$zip);
$map_api_url = "https://maps.googleapis.com/maps/api/geocode/json?address=".$map_address."&key=myKey";
$resp_json = file_get_contents($map_api_url);
$resp = json_decode($resp_json, true);

I then get the Long and Lat:

$lati = $resp['results'][0]['geometry']['location']['lat'];
$longi = $resp['results'][0]['geometry']['location']['lng'];

But how do I grab the city and state?

{
   "results" : [
  {
     "address_components" : [
        {
           "long_name" : "3840",
           "short_name" : "3840",
           "types" : [ "street_number" ]
        },
        {
           "long_name" : "Fake Street",
           "short_name" : "Fake St",
           "types" : [ "route" ]
        },
        {
           "long_name" : "Wilson Heights",
           "short_name" : "Wilson Heights",
           "types" : [ "neighborhood", "political" ]
        },
        {
           "long_name" : "North York",
           "short_name" : "North York",
           "types" : [ "sublocality_level_1", "sublocality", "political" ]
        },
        {
           "long_name" : "Toronto",
           "short_name" : "Toronto",
           "types" : [ "locality", "political" ]
        },
        {
           "long_name" : "Toronto Division",
           "short_name" : "Toronto Division",
           "types" : [ "administrative_area_level_2", "political" ]
        },
        {
           "long_name" : "Ontario",
           "short_name" : "ON",
           "types" : [ "administrative_area_level_1", "political" ]
        },
        {
           "long_name" : "Canada",
           "short_name" : "CA",
           "types" : [ "country", "political" ]
        },
        {
           "long_name" : "M6H",
           "short_name" : "M6H",
           "types" : [ "postal_code_prefix", "postal_code" ]
        }
     ],
     "formatted_address" : "3840 Fake Street, Toronto, ON M6H, Canada",
     "geometry" : {
        "location" : {
           "lat" : 43.5399244,
           "lng" : -78.43486559999999
        },
        "location_type" : "ROOFTOP",
        "viewport" : {
           "northeast" : {
              "lat" : 43.5412733802915,
              "lng" : -78.43351661970848
           },
           "southwest" : {
              "lat" : 43.7385754197085,
              "lng" : -79.43621458029151
           }
        }
     },
     "place_id" : "ChIJPVDxjGgyK4gRt3O7zOSmIF4",
     "types" : [ "street_address" ]
  }
],
"status" : "OK"
}

1 Answer 1

0

Just answered my own question by running the code through a loop. Please let me know if you have a better way!

foreach($resp['results'][0]['address_components'] as $address_componenets) {
    if($address_componenets["types"][0]=="country") {
        $country = $address_componenets["long_name"];
    }
    if($address_componenets["types"][0]=="administrative_area_level_1") {
        $state = $address_componenets["long_name"];
    }   
    if($address_componenets["types"][0]=="locality") {
        $city = $address_componenets["long_name"];
    }   
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.