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 :

print_r($obj->getCoordinates($address ));


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

For our "Danmark, Sjælland, Hørve, Dragsholm Alle 4534" we have these data :

    [lat] => 55.7754120
    [long] => 11.3901550
    [alt] => 0

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.

32 comments on “How to get coordinates for a given address using PHP

  1. Pingback: Sascha Tayefeh’s Homepage » Geocoding APIs for PHP, iPhone, and Android

  2. 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.

  3. 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

  4. 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.

  5. 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’];

  6. Pingback: Geocoding APIs for PHP, iPhone, and Android « tayefeh

  7. great, still seems to work like a charm!

    thanks for this class, it saved me hours 🙂

  8. 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 🙂

  9. This was very helpful – thanks!

    I did add the following line to the beginning of the getCoordinates() function, to try fix ‘bad’ addresses prior to sending them to Google:

    $address = preg_replace(‘~[^0-9a-z\s]~i’, ”, $address);

    I used this object to take a table of addresses, and then put the coordinate output into linked table for repeated use without hitting Google’s servers.

  10. Also, Google no longer requires and API key for this kind of call (not sure about any daily limit for requests from the same IP, etc). So, this line is sufficent:

    $url = ‘’ . $address . ‘&output=xml’; // &key=.$this->mapApiKey;

  11. Martijn is right.. the shared host is creating the problem with that code..

    You can simply use the following to get the latitude and longitude coordinates..

    Simply insert the following code in your web page and see the magic..



    echo $latitude;

  12. Hi, I tried using this code…..but this code is not wokring…….Instead of latitude & longitude nothing is showing……Please help!!

  13. function geocoding($address){
    $address = rawurlencode($address);
    $output= json_decode($geocode);
    $geo[‘lat’] = $output->results[0]->geometry->location->lat;
    $geo[‘long’] = $output->results[0]->geometry->location->lng;
    return $geo;

  14. It seems like this just recently stopped work randomly. I thought I messed something up, but even the test file doesn’t work anymore. Previously it worked no problem. Any ideas?

  15. Hi, for me it does not work. I add addresses dynamically and my API key and it does not return coordinates. Can someone help me?

  16. Its looks good but it is not working for me.
    It is giving me for your adderss
    For our “Danmark, Sj¾lland, H¿rve, Dragsholm Alle 4534” we have these data
    [lat] => 0
    [long] => 0
    [alt] => 0

    If enter any other address it gives error

  17. Thanks for the great article.
    Looks like it was made with old google Maps api. Hence it is not parsing.

    The URL is changed from :
    ‘’ . $address . ‘&output=xml&key=’ . $this->mapApiKey;


    ‘’ . $address . ‘&key=’ . $this->mapApiKey;

    and the response code also need to be changed a bit, because the Format is different.

    check the gist :

    Hope this helps someone 🙂

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.