Find Longitude And Latitude Of PostCode or ZipCode Using Google Maps And PHP

4th September 2008 - 2 minutes read time

Converting from PostCode to map reference is far from accurate, but it can be done using the Google Maps API. You can get a Google Maps API key from Google by just asking for it, although you are limited to a certain number of requests each day.

Google Maps usually works through JavaScript, but it is possible to ask Google to return the data in JSON format and then use the PHP function json_decode() to decode the information into a usable array format. To get Google to return the data in JSON you must pass the parameter "output=json" in your query string.

The following function can take a postal code and convert it into longitude and latitude.

function getLatLong($code){
 $mapsApiKey = 'your-google-maps-api-key';
 $query = "http://maps.google.co.uk/maps/geo?q=".urlencode($code)."&output=json&key=".$mapsApiKey;
 $data = file_get_contents($query);
 // if data returned
 if($data){
  // convert into readable format
  $data = json_decode($data);
  $long = $data->Placemark[0]->Point->coordinates[0];
  $lat = $data->Placemark[0]->Point->coordinates[1];
  return array('Latitude'=>$lat,'Longitude'=>$long);
 }else{
  return false;
 }
}

The function can be used in the following way. To keep with the theme, the following two postal codes are one UK address and USA office locations of Google.

print_r(getLatLong('SW1W 9TQ'));
print_r(getLatLong('10011'));

This produces the following output.

Array
(
 [Latitude] => 51.489943
 [Longitude] => -0.154065
)
Array
(
 [Latitude] => 40.746497
 [Longitude] => -74.009447
)

I have tried this with UK and USA postal codes, but it would be interesting to see if it works with any other codes. Also, the query currently looks at google.co.uk, but that is only because I am based in the UK. You should change this to the nearest Google domain, so if you are based in the US then change this to google.com.

Comments

Permalink

Hi,

Thannks a lot for this code... it helped me a lot... i was looking for it from last one week :'(...

oh im so happy..... thank u so much :) :)

Anjali (Wed, 03/30/2011 - 05:08)

Permalink

Excellent is just one simple word to appreciate this work. I have been banging on certain forums just get this piece of thing you have mentioned about. I really appreciate it.

Zee Ally (Wed, 04/13/2011 - 19:38)

Permalink

this is not working in server wot to do?

aaa (Sat, 04/23/2011 - 03:46)

Permalink

Thanks a lot,

This code very helpful to me to find lattitude amd longitude uk postcodes

Thanks again

Shiju Joy

India

shiju (Fri, 10/14/2011 - 12:54)

Permalink

Thanks a lot :)

dave (Fri, 11/18/2011 - 07:46)

Permalink

Thanks for this.  I was using your original street maps version for one of my sites last year but it seemed to be less reliable lately.  This alternative works like a charm.

Chris Shennan (Fri, 01/13/2012 - 14:03)

Permalink

Hi,

How can we ge the zipcode using latitude & longitude..???

i mean vice versa of this process 

Anonymous (Thu, 03/08/2012 - 10:38)

Permalink

Hi there,

          My self avani and i m from india. I m using ur above mention code to find out lt & lg of perticular zip code.

When ever i input zipcode as '360007', its return nothing . 

 

So can you please help me .

 

Regards & lots of thanks.

Avani (Wed, 03/28/2012 - 06:17)

Permalink

Thanks alot for the code spinnet, best example

 

Good luck friend..! 

vipin kohli (Tue, 04/17/2012 - 19:31)

Permalink

Hello,

This is nice script but not working for us zip code.

 

Thanks

Anonymous (Wed, 04/18/2012 - 06:24)

Permalink
Many thanks for this great work. This helping me alots.

Renga (Thu, 06/14/2012 - 05:39)

Permalink
The reason it's not working for the US is because the query string is using co.uk TLD. Change it to $query = "http://maps.google.com/maps..." instead. Thanks for the code snippet, philipnorton42.

g (Sat, 06/30/2012 - 23:18)

Permalink
Please provide the equivalent code in java as well. It will be a great help for me. Thanks in advance.

Sirisha (Tue, 07/10/2012 - 09:50)

Permalink
Thank you for your code.

Anna (Wed, 08/01/2012 - 03:08)

Permalink
Hello Dear, Thanks for this tutorial l but below url is 404 so please provide or mail me correct url. $query = "http://maps.google.co.uk/maps/geo?q=".urlencode($code)."&output=json&key=".$mapsApiKey; Thanxs

Amber (Mon, 04/09/2018 - 12:09)

Add new comment

The content of this field is kept private and will not be shown publicly.