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 :
-
<?php
-
-
//include our class
-
-
//your Google Maps API key (you can get one for free from http://code.google.com/apis/maps/signup.html)
-
$apiKey = ‘API_KEY_HERE’;
-
-
//init our object
-
$obj = new googleHelper($apiKey);
-
-
//get coordinates and print the debug info
-
$address = ‘Danmark, Sjælland, Hørve, Dragsholm Alle 4534′;
-
print ‘<pre>’;
-
-
?>
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.
Very great article!!
How do I set character encoding to utf-8, for addresses that have accents?
Superb, just what I needed.
Thanks so much.
Where can I make donation?
Hi Alken,
If you want to do a donation you can use the PayPal button from here :
http://stuff.nekhbet.ro/about
Regards
Sorin
Great article. Thank you very much….
Trimbitas
How can I extract the elements of the array?
THanks JP
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
Hi Harry,
I’m not sure I understand your question ….
Regards
Sorin
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
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.
Senhor,poderia disponibilizar o arquivo class.googleHelper.php. att.
Francisco .. here is the code : http://stuff.nekhbet.ro/wp-content/uploads/2008/12/googlehelper_v1.zip
Regards
Sorin
Trimbitas How can I extract the elements of the array? THanks JP
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'];
Hi,
Is their any way to retirve lattitude and longitude based on just City and state?
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
great, still seems to work like a charm!
thanks for this class, it saved me hours
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