Geocoding Addresses using Google Maps

May 19, 2008 | Google Maps | 5 comments

In my previous post I showed you how to create a beautiful Google Map for your site with controls showing Google headquarters. Some might say that this isn’t very useful, I don’t know why they think that but for those that agree here is how to get it to show any postcode or address you like.

I strongly suggest you read my other post first if you don’t know much about the Google Map API (read it here)


The process of turning an address into latitude and longitude coordinates is called Geocoding and Google’s API makes it very easy to do so using a simple request to their server. We will go into the legal side later on in this post but for now lets just say that Geocoding is a server intensive process for Google and they recommend client side caching of added coordinates to avoid re-requesting coordinates if you already have them.

Getting the coordinates is actually very easy. Use the following URL in your browser (making sure to replace your API key and an address) ## ADDRESS OR POSTCODE ## &output=xml&key= ## YOUR API KEY ##

This URL will return some XML (if you have done it correctly) which will contain three pieces of useful information

  • Latitude
  • Longitude
  • Geocode Accuracy

If you know what country your going to be searching in then you can add another argument to the URL telling it the TLD or Top Level Domain (com, uk, etc..) to make it a little easier for Googles Geocoder:


We can use the returned data to create a Google map of that location but first we need to extract the coordinates. Yes we could use Javascript but where’s the fun in that (or the point)? Using PHP means all the processing is done server side so the client doesn’t need to do any sort of processing (useful for slower client computers).

Here’s how you get PHP to send the request:

$ch = curl_init();
$url = "" . urlencode($address) . "&output=xml&key=" . $api_key;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$return = curl_exec($ch);

Why why why are you using CURL you ask? (what is CURL anyway?) Well..

A fair few server administrators are blocking the use for file handling functions to URLs (fopen, file, file_get_contents, etc..); This is sensible because there are exploits that malicious programmers can use to gain access to your site and start causing trouble. CURL is a little slower (I’m told) but safer (apparently).

The querystring we sent to Google passes the type of return we want (XML, KML, CSV, JSON), the address or postcode (urlencoded string) and of course our API key.

The curl options (specifically CURLOPT_RETURNTRANSFER) mean that whatever comes back from your CURL request can be stored in a variable and worked on (as opposed to echoing to the screen).

Although XML parsing in PHP is really easy (xml_parse_into_struct if your interested), it’s a bit overkill for what we need; Instead specify the output as CSV which returns a simple string that is incredibly easy to use. Something like:


This breaks down into the following:

  • Request status code (success is 200, anything else means it’s broken)
  • Accuracy of search result (5 is postcode level, higher is better as far as 9 which is premise level)
  • Latitude
  • Longitude

The easiest way to get split the returned CSV string is the PHP explode function:

$array = explode(',', $csv_string);

This returns an array of values split on the delimiter specified (in this case the comma). Now the latitude and longitude can be accessed at positions 2 and 3 respectively ($array[2] and $array[3]).

So whats next?: Use the code I gave in my previous example to generate a map for an address we actually want by passing the latitude and longitude into my render_map function ($map->render_map($array[2], $array[3]);).

Some things to bear in mind:

The next step in theory would be to build up a database of geocoded coordinates next to addresses and use Google maps literally everywhere. Unfortunately (to my understanding (May 2008)) the Post Office have licensed their data and Google have to follow through on this. Basically it means that in every country except the UK you can save the coordinates wherever you like whereas here you have to geocode Google’s data on the fly EVERY page load.

If you decide this is unacceptable but don’t want to break the Terms & Conditions of Google’s API you can use a geocoding service and pay per postcode. It costs about 5 pence per geocode (Postcodeanywhere May 2008) and they allow you to store the data wherever you like.

Coming next: A find my nearest … search using google maps

If you want more information on the geocoding process then Googles explanation is here

A Donate Button!


  1. Micky Hill

    Do you know how to extract only the postal_code and the Locality from the xml file?

  2. admin

    Hi Micky, This is one of the reasons I prefer to use the CSV version of the return feed. It is incredibly easy to parse and much smaller to receive. The downside of course is that it doesn’t return any of the information you need. It returns geocoded coordinates and an accuracy rating.

    As for parsing the XML feed… PHP has a huge library of XML parsing functions and there are various examples online for doing so. I personally find the PHP funcion xml_parse_into_struct a nice one to use.

    Let me know if i can help you any more.


  3. Jay

    Hi Sean,

    I’m only conceptually aware of XML. I’m attempting to get the other address values out of the values returned by google. While my formatting is consistent, I can only get coordinates, postal code and accuracy. Can you help me get the rest?

    Much appreciated.


  4. admin

    Hi Jay, Yes no problem see your email.


  5. Drew

    This post was a lifesaver! GoDaddy updated the Security on their servers 4-24-14 which broke my index.php files. I was told they needed a curl. Many thanks!


Submit a Comment

Your email address will not be published. Required fields are marked *

CommentLuv badge

Stay in touch!

Page Builder Cloud

Page Builder Cloud

A truly universal template library for WordPress Page Builders.

Page Builder Recommendation

Elementor Banner

We are BIG fans of the Elementor page builder. Give it a look!

About this site and Sean Barton

Picture of Sean
Sean Barton is a Freelance PHP Website Developer in Crewe, Cheshire. He is a Wordpress and CMS/Framework specialist and Co-Founder of Page Builder Cloud.
This site was set up in 2008 as a tutorial and scripting resource for the PHP language and Wordpress.
Find out more about Sean on the About Me page or use the Hire Me page to get in touch. For more information about Sean's work take a look at the Portfolio

Our Services

  • Wordpress plugin/theme development
  • Divi specialist
  • Ecommerce (Woocommerce, WPSC, Shopify, Magento)
  • PSD to Wordpress theme conversion (Responsive)
  • Website design work (Banners, Logos, Full Site, etc)
  • Website analysis (security, usability, SEO)
  • API Integrations (InfusionSoft, SalesForce, Ontraport, Customer Thermometer, etc..)
  • Wordpress consultancy & expert advice
  • Crisis support
  • Website hosting

The main services offered are Wordpress based although we do a great deal of technical programming for bespoke systems. From troubleshooting, extending frameworks, finding bugs to writing them from scratch.

Find out more by looking through our past projects or get a quote.

Be the first to hear about new products/updates!

This is a mailing list for those people interested in being told when we release a new product (Divi plugin or Theme).

We shall also use this list to let you know about product updates and releases.

You have Successfully Subscribed!