Skip to content


How to get coordinates for a given address using PHP

During my work on a Danish website I needed to find a way to put on a map a list of Danish restaurants using only their real address (region,city,street,street number) so I start searching for the best solution to convert an address like “Danmark, Sjælland, Hørve, Dragsholm Alle 4534″ to its corresponding coordinates (55.7754120, 11.3901550).
After about an hour of trying multiple solutions I concluded that Google has ,again, the best solution, at least for outside-US locations, :) so I just made an extremely simple class to help me retrieve the information I needed.

I have attached here the class and a test file so you can see how it works.
I plan to develop this class in the future that’s why it requires a Google Maps API key.

Here is the test file for it, just to make an impression on how it looks like :

  1. <?php
  2.  
  3. //include our class
  4. require_once(dirname(__FILE__) . ‘/class.googleHelper.php’);
  5.  
  6. //your Google Maps API key (you can get one for free from http://code.google.com/apis/maps/signup.html)
  7. $apiKey = ‘API_KEY_HERE’;
  8.  
  9. //init our object
  10. $obj = new googleHelper($apiKey);
  11.  
  12. //get coordinates and print the debug info
  13. $address = ‘Danmark, Sjælland, Hørve, Dragsholm Alle 4534′;
  14. print ‘For our "’ . $address . ‘" we have these data : ‘;
  15. print ‘<pre>’;
  16. print_r($obj->getCoordinates($address ));
  17.  
  18. ?>

What it returns is latitude, longitude and altitude ( only for some locations you can also get this parameter ) :

  1. For our "Danmark, Sjælland, Hørve, Dragsholm Alle 4534" we have these data :
  2.  
  3. Array
  4. (
  5.     [lat] => 55.7754120
  6.     [long] => 11.3901550
  7.     [alt] => 0
  8. )

Note: To get the best results from Google try to format your address like this : COUNTRY, STATE/REGION, CITY, STREET_NAME STREET_NUMBER .

That’s all :) , if you have questions don’t hesitate to ask.

Posted in PHP Tips & Tricks.


19 Responses

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.

  1. Sergio says

    Very great article!!
    How do I set character encoding to utf-8, for addresses that have accents?

  2. Aiken says

    Superb, just what I needed.
    Thanks so much.
    Where can I make donation?

  3. Trimbitas Sorin says

    Hi Alken,

    If you want to do a donation you can use the PayPal button from here :

    http://stuff.nekhbet.ro/about

    Regards

    Sorin

  4. Riccardo says

    Great article. Thank you very much….

  5. JP says

    Trimbitas

    How can I extract the elements of the array?

    THanks JP

  6. Harry_Funakoshi says

    Hi, Trimitas

    Thank you so much for this tutorial.

    I have a question. Is there any way I can add the place names on a certain coordinate? If so, please tell me how. Thank you so much.
    -Harry

  7. Trimbitas Sorin says

    Hi Harry,

    I’m not sure I understand your question ….

    Regards

    Sorin

  8. Harry_Funakoshi says

    Hi, Sorin

    Well, first of all, I made a reverse geocoder application. It worked. But the addresses returned from a certain coordinates are only based on location data that Google has. So, for other countries outside of U.S, these location addresses are too general. I’d like to make it more specific. For example, I’d like to add my campus, home address, and other landmarks. Is there any way I can do this?

    Thank you so much for your reply and so sorry about my english
    -Harry

  9. Martijn says

    Love it! Works very well thank you.

    Although the CURLOPT_FOLLOWLOCATION is not allowed on my shared host (and i guess of many others) so it didnt work right away. But after changing CURL to the @file_get_contents($url) routine it worked like a charm.

  10. Francisco says

    Senhor,poderia disponibilizar o arquivo class.googleHelper.php. att.

  11. Trimbitas Sorin says

    Francisco .. here is the code : http://stuff.nekhbet.ro/wp-content/uploads/2008/12/googlehelper_v1.zip

    Regards

    Sorin

  12. Laurie Allen says

    Trimbitas How can I extract the elements of the array? THanks JP

  13. Trimbitas Sorin says

    Hi Laurie .. in what way? It is a simple array and you can access it like any other array. Example :
    $arr = $obj->getCoordinates($address));
    print ‘Latitude is : ‘ . $arr['lat'];

  14. Akshay says

    Hi,
    Is their any way to retirve lattitude and longitude based on just City and state?

  15. Barry Sakkers says

    hello there,

    someone can give me a hint?
    Try this page:
    http://www.vakantiereisboeker.nl/beheer/checkGPS.php
    The code goes abend on a country name with ë in it .
    Someone knows how to avoid this?

    Kind regards,

    Barry

  16. sushie says

    great, still seems to work like a charm!

    thanks for this class, it saved me hours :)

  17. Sid says

    Amazing mann! :)
    Really helped!
    One problem i faced(really stupid), the single quotes in my editor didnt match with the ones used here… if anyone else is facing a similar problem, pls try replacing the single quotes over here with the ones in your editor :)

Continuing the Discussion

  1. Sascha Tayefeh’s Homepage » Geocoding APIs for PHP, iPhone, and Android linked to this post on August 23, 2009

    [...] A good implementation of this strategy in form of a PHP class can be found here. [...]

  2. Geocoding APIs for PHP, iPhone, and Android « tayefeh linked to this post on July 17, 2011

    [...] A good implementation of this strategy in form of a PHP class can be found here. [...]



Some HTML is OK

or, reply to this post via trackback.


Get Adobe Flash playerPlugin by wpburn.com wordpress themes