1 Star2 Stars3 Stars4 Stars5 Stars (1 votes, average: 5.00 out of 5)
Loading ... Loading ...

Google Geocoding Service – get latitude, longitude from address, postcode


Google has a very handy web service available for geocoding. Very often we require to know the longitude, latitude of a postcode, may be for finding distance between 2 places or any other reasons (who cares) and as always, Google is ready with the service before you realize you’ll need that :-) . This is the beauty of google – they thinks ahead.
To use the service, at first, you need to get a google api key for your site which takes less than 30 seconds from here.

Following is the typical format of the request string to get geocoding information:

http://maps.google.com/maps/geo?q=95678&output=json&oe=utf8&sensor=false&key=your_key_here

Description:

  • q = address that you are searching for, it can be anything from a postcode to any full or partial address. Google will search the best matching result for you.
  • output = this is the output format of the response. Can be json, xml, kml or csv. Isn’t it great?
  • oe = output encoding format. This is optional but strongly encouraged.
  • sensor = value can be either true or false. This indicates that whether the request is coming from any device with location sensor.
  • key = your google api key
  • There are lot other options to be added. You can find official documentation here

    Following is a simple geocoding script I wrote in PHP to get the lat/long of a postcode (it can be any address format other than postcode). Ask me in comments if you want the script, I’ll mail it to you.
    Open the app in new window

    Bookmark and Share

    ,

    1. #1 by Ryan McDermott on July 25th, 2009

      Great script, I\’ve been looking for this! can you plz send me it?

      thanks!

    2. #2 by Imran on July 25th, 2009

      Hi Ryan, Sent to your email address. Thanks a lot for coming.

    3. #3 by steve on August 7th, 2009

      thats just what ive been looking could you please send me the script?

    4. #4 by Imran on August 7th, 2009

      Hi Steve,
      Sent it to your email. Thanks for coming.

    5. #5 by Ray on May 2nd, 2010

      I see that this was last year, but appears to be what I am looking for. Could you please send me the script.
      Thanks

    6. #6 by Imran on May 2nd, 2010

      Sent to your email address. Keep coming :-)

    7. #7 by Kaspar on May 5th, 2010

      Hello, very good stuff! Is it possible to get the script? Appreciated.

    8. #8 by Imran on May 6th, 2010

      Hi Kaspar, Sent the script to your email address. Keep coming.

    9. #9 by Allan G on May 12th, 2010

      Just awesome, simply awesome. Any chance of a look at that script?

    10. #10 by Imran on May 12th, 2010

      Hi Allan,
      On the way to your mail account. Thanks :-)

    11. #11 by Sam on May 22nd, 2010

      Good stuff!

      Any chance I can have the script?

      Thx, Sam

    12. #12 by Imran on May 24th, 2010

      Hi Sam,

      Sent to your email address. Thanks for coming.

    13. #13 by Jay on June 11th, 2010

      Hello mate,

      Could you please send me the script as I would like to use it for my forum members. The site is designed in joomla.

      Will I need to get my own google API Keys?

      cheers

    14. #14 by Imran on June 11th, 2010

      Hi Jay,

      Sent to your mail add. Yes you need your own API key.

      Cheers

    15. #15 by Jay on June 11th, 2010

      Hi,

      I have figure it our, but it is not showing the longitude and latatude values?

      any ideas

    16. #16 by Imran on June 11th, 2010

      Jay,

      What I would suggest is:

      1. Check if the script has any php error. Accidentally you might have added some characters(?)
      2. Add “print_r($googleresponse)” at line 20 to see if googles response is coming through.

    (will not be published)
    Security Code:

    1. No trackbacks yet.