RBA Cash Rate: 4.35% · 1AUD = 0.67 USD · Inflation: 4.1%  
Leading Digital Marketing Experts | 1300 235 433 | Aggregation Enquires Welcome | Book Appointment

Get Country, State & City from Google’s Geocoding API

Get Country, State & City from Google’s Geocoding API

This article won't mean much to you unless you you have a specific problem to solve. I was working on a project that required mapping with Google's Geocoding API, and I had to extract the City, State and Country (most notably the state) from an address... and I encountered numerous issues with relation to how the json data was returned. After playing around, this function seems to serve my purpose. Initially published on Internoetics, it was one of the site's most downloaded snippets of PHP code.

From Google Maps, Geocoding is the process of converting an addresses into geographic [latitude and longitude] coordinates, which you can use to place markers or position the map. The Google Geocoding API provides a direct way to access a geocoder via an HTTP request. Additionally, the service allows you to perform the converse operation (turning coordinates into addresses); this process is known as "reverse geocoding."

A Geocoding API request must be in the following format: http://maps.googleapis.com/maps/api/geocode/output?parameters (where output may be either json or XML). Full details of the mapping functionality is available on Google's Geocoding website .

In my case, obtaining the state (defined by administrative_area_level_1 in the json data) was problematic because it wasn't a consistent element within the object array. The solution was to decode the json data and iterate over the resulting array (using in_array() to check the value).

The PHP Code

1
<?php 
2
/*
3
 Get Country, State & City from Google's Geocoding API
4
 http://www.beliefmedia.com/reverse-geocoding-google-maps
5
*/
6
 
7
function beliefmedia_reverse_geocode($address) {
8
 
9
 $address = str_replace(&quot; &quot;, &quot;+&quot;, &quot;$address&quot;);
10
 $url = &quot;http://maps.google.com/maps/api/geocode/json?address=$address&sensor=false&quot;;
11
 $result = file_get_contents(&quot;$url&quot;);
12
 $json = json_decode($result);
13
 
14
 foreach ($json->results as $result) {
15
  foreach($result->address_components as $addressPart) {
16
    if ((in_array('locality', $addressPart->types)) && (in_array('political', $addressPart->types)))
17
    $city = $addressPart->long_name;
18
      else if ((in_array('administrative_area_level_1', $addressPart->types)) && (in_array('political', $addressPart->types)))
19
    $state = $addressPart->long_name;
20
      else if ((in_array('country', $addressPart->types)) && (in_array('political', $addressPart->types)))
21
    $country = $addressPart->long_name;
22
  }
23
 }
24
 
25
   if(($city != '') && ($state != '') && ($country != ''))
26
      $address = $city.', '.$state.', '.$country;
27
   else if (($city != '') && ($state != ''))
28
      $address = $city.', '.$state;
29
   else if (($state != '') && ($country != ''))
30
      $address = $state.', '.$country;
31
   else if ($country != '')
32
      $address = $country;
33
 
34
 return $address;
35
}
36
 
37
/* Usage */
38
echo beliefmedia_reverse_geocode(&quot;19-29 Martin Pl, Sydney&quot;);
39
?>

Returns: Australia, New South Wales, Sydney

Considerations

  • Since Google introduced a requirement to have an API key for many of their products, this code may not work unless you whitelist your server IP address.
  • If you're looking to reverse geocode multiple addresses, it'd be best to avoid Google's services for such activity. There are plenty of applications that'll do this for you.

Download


Title: Get Country, State & City from Google's Geocoding API
Description: Get Country, State & City from Google's Geocoding API.
  Download • Version 0.2, 725.0B, zip, Category: PHP Code & Snippets

Download our 650-page guide on Finance Marketing. We'll show you exactly how we generate Billions in volume for our clients.

  AUS Eastern Standard Time (Virginia)

  Want to have a chat?
 

RELATED READING

Like this article?

Share on Facebook
Share on Twitter
Share on Linkdin
Share on Pinterest

Leave a comment