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 :

Array
(
    [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.

31 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.
    -Harry

  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
    -Harry

  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 = ‘http://maps.google.com/maps/geo?q=’ . $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..

    $url=’http://maps.google.com/maps/geo?q=68+King+William+Street,+London,+United+Kingdom,+EC4N+7DZ&output=csv&oe=utf8&sensor=false’;

    $latitude=@file_get_contents($url);

    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);
    $geocode=file_get_contents(‘http://maps.google.com/maps/api/geocode/json?address=’.$address.’&sensor=false’);
    $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
    Array
    (
    [lat] => 0
    [long] => 0
    [alt] => 0
    )

    If enter any other address it gives error

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.