Computer programming in PHP for business and passion
In: PHP
8 Apr 2009The Google Maps API are a very useful system to get geographic information. Maybe you are thinking cool! but i’m not a GIS developer so why i need to continue to read this post? Let me do an example. Scenario: you have to check the validity of the geo data of a list of customers stored into a database. Using the Google Maps API you can easly compare the data stored into your database with the google data and fix the errors.
In this post i propose a PHP class that uses the Google Maps API to provide geographic information from a generic address (city, country,street+city, etc). This class is named GMaps and in the follow there is a use case:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | require_once 'GMaps.php'; // Your google key $google_key = ''; if (!empty($_POST)) { $search= strip_tags($_POST['search']); } echo '<form action="example.php" method="post">'; echo '<input name="search" type="text" />'; echo '<input type="submit" value="Get geographic data!" />'; echo '</form>'; if (!empty($search)) { // Get the Google Maps Object $GMap = new GMaps($google_key); if ($GMap->getInfoLocation($search)) { echo 'Address: '.$GMap->getAddress().'<br>'; echo 'Country name: '.$GMap->getCountryName().'<br>'; echo 'Country name code: '.$GMap->getCountryNameCode().'<br>'; echo 'Administrative area name: '.$GMap->getAdministrativeAreaName().'<br>'; echo 'Postal code: '.$GMap->getPostalCode().'<br>'; echo 'Latitude: '.$GMap->getLatitude().'<br>'; echo 'Longitude: '.$GMap->getLongitude().'<br>'; } else { echo "The response of Google Maps is empty"; } } |
To use the GMaps class we initialize it with a valid Google key (line 16) and we get the geographic information with the method getInfoLocation (line 17), that’s it!
Here you can download the class GMaps.php with this example (GMaps.zip).
In order to execute the class you must have a valid Google API key. If you don’t have this key you can generate a new one here (every key is related to a specific url of your application).
This is my personal blog about computer programming in PHP for business and passion. I'm Enrico Zimuel a Software Engineer since 1996. I work as Senior Consultant & Architect at Zend Technologies. For more info about me visit my web site.
28 Responses to How to use the Google Maps API with PHP
Chris
June 10th, 2009 at 2:18 pm
Looks nice and works fine
BorstiStumpf
June 10th, 2009 at 2:52 pm
wow very nice class! thx!
olopsman
June 11th, 2009 at 4:31 am
just starting out with google maps api and this works great, very useful!
Tommi
June 11th, 2009 at 8:36 am
Please note that simplexml_load_file requires Google Maps API to return utf-8 encoded data, otherwise you will ge a warning of “parser error : Input is not proper UTF-8, indicate encoding ! “.
This is easily avoided if you add “oe=utf-8″ as a parameter to the GMap api request – see line 99 in _connect -method of your GMaps class
Enrico Zimuel
June 11th, 2009 at 9:18 am
The default encoding of the google maps web service is utf-8, so this is not necessary.
For more info about the google maps character encoding http://code.google.com/apis/maps/documentation/index.html#CharacterEncoding
Stephan
June 11th, 2009 at 12:26 pm
Hello,
great script, but Tommi IS right. At least from two servers over here in Germany I did not get utf-8 encoded data.
With the suggested “$request_url = $this->_baseUrl . “&q=” . urlencode($param).”&oe=utf-8″;” everything is fine
Stephan
Enrico Zimuel
June 11th, 2009 at 1:38 pm
Ok, i just uploaded the GMaps class with the utf-8 encoding specification. Thanks for your reply.
Amit
July 31st, 2009 at 7:38 am
gud one .. as i m looking for something else but i found the class is very fine
http://topmp3.info
August 11th, 2009 at 10:58 pm
Thanks a lot looks nice.
Locksmiths
August 14th, 2009 at 9:27 pm
Thanks for adding those file. I was searching for using in some sites. Thnaks. I will try those codes
Locksmiths
http://www.precisionlocksmiths.co.uk
Why is my computer so slow
August 30th, 2009 at 11:49 am
Great stuff! This looks sweet. Keen to try it myself.
martin
October 22nd, 2009 at 7:51 pm
This is awesome – exactly what i am looking for (after getting fed up with inaccurate postcode files).
Do you know is there a way to “hint” that the search is for the United Kingdom. I have a UK site and I want it to default to an English town where it may think it is American instead?
martin
October 22nd, 2009 at 8:00 pm
I figured it out – simply replace
“/maps/geo?output=xml&key=”
with
“/maps/geo?output=xml&gl=GB&key=”
i.e. add the parameter gl=GB to the query.
See http://code.google.com/apis/maps/documentation/geocoding/index.html#GeocodingRequests for other parameters.
Alessandro Coscia
December 21st, 2009 at 4:02 pm
Simple and useful, Enrico thanks for share.
Digisha Modi
January 5th, 2010 at 12:21 pm
Thanks for sharing this important stuff…
Get Location Information based on an address, city or country name | jigniter™
January 10th, 2010 at 10:48 pm
[...] address using a Geocoder tool. For that reason, we’d recommend to take a look over How to use Google Maps API with PHP from Zimuel’s [...]
petiar
February 28th, 2010 at 9:57 pm
Thanks for this article, nevertheless, I am interested in getting drivind distance between two location. I have found quite good article which shows how to do it in .js. But I was wandering if there is any way to achieve this using PHP.
Enrico Zimuel
March 1st, 2010 at 10:22 am
In my opinion you have two ways: use the Google Maps API to calculate the distance between two points or use a mathematics approach with the Havesine Formula (without http calls).
I found an interesting post on how to implement this formula using PHP (and MySQL), you can find it here.
christian louboutin
March 8th, 2010 at 4:48 am
it is interesting and informative article. This has been very helpful understanding a lot
of things. I’m sure a lot of other people will agree with me.
wholesale new era hats
March 27th, 2010 at 3:36 pm
great post.
nice job
ray
April 16th, 2010 at 11:44 am
Thank you so much! This is SO helpful.
designer
April 17th, 2010 at 9:31 am
This is a handy posting, im happy I found this
christian louboutin heels
May 5th, 2010 at 9:01 am
it is interesting and informative article. This has been very helpful understanding a lot
of things. I’m sure a lot of other people will agree with me.
Brad
May 6th, 2010 at 4:57 pm
Hey- I like your example of how you implemented use of the Google Maps API geocoder. Just wanted to share I am “re-invigorating” the popular PHPGoogleMapAPI class from Monte Ohrt to use the latest V3 Google Maps APIs – http://code.google.com/p/php-google-map-api/. You can use the class to get Geocodes, build maps, build polylines, add markers, and more, all with just a few simple lines of code.
Jorgen
May 8th, 2010 at 8:38 am
The google map API is great, but the new CloudMade map api is also quite interesting, with features like customized maps and support for many development languages. See examples on implementation for both ajax and php here: http://tips4php.net/2010/05/watch-your-back-google-here-comes-cloudmade-maps/
Cheap nfl jerseys
June 26th, 2010 at 3:44 pm
Good stuff,thank you for share
khanchai
July 21st, 2010 at 11:22 am
thank you for share,
ClubPenguinCheats
July 29th, 2010 at 1:59 am
I am interested in getting drivind distance between two location. I have found quite good article which shows how to do it in .js.