A few days ago I was developing a website for a client. They required that there be a map showing where addresses are that were retrieved from a database. I have never properly looked at the Google Maps API before because I have never needed to use it; so for the first time I looked at it and gave it a try. Then after playing around with it for a bit i realised that reverse geocoding was needed to convert the address into latitude and longitude figures.
Because I was programming this website in PHP I thought I would have a look around for a PHP class to see if there was a quicker way of doing this. Luckily enough I found a PHP class called PHP GoogleMapAPI. It took me a few minutes to read through the documentation for this class but it was so straight forward and simple. All in all it takes only 6 lines of code using this class to create a google map and locate it to an address; because it does all the other work for you that you don't need to know about if you're not going to be programming Google Maps every day of your life.
Implementing Google Maps on your website
The first thing you need to do is go to the website and download the class. Then copy it into the directory where you keep your classes stored on your server.
When you have downloaded the class, it then needs to be included in the file that we want to show the map in. At the top of your file insert the code below.
<?php require('GoogleMapAPI.class.php'); $map = new GoogleMapAPI('map'); // enter YOUR Google Map Key $map->setAPIKey('YOURGOOGLEMAPKEY'); $map->addMarkerByAddress('621 N 48th St # 6 Lincoln NE 68502','Our Address','<b>Our Address</b>'); <body onload="onLoad()"> <?php $map->printMap(); ?> </body>
What we are doing here is including the class with the require function in PHP. If the class fails to be included then the rest of the page will stop executing.
We then create a new instance of the class and then we insert.
If you have a Google Maps API key then insert it where it says 'YOURGOOGLEMAPKEY'. If you do not have one you can get one from here.
The function below adds a marker to the address you would like and centers it. This function takes 3 parameters; the address, a title and html.
$map->addMarkerByAddress();
We then need to include the onLoad function so that it knows to load the map on this page.
The last function that we add is
<?php $map->printMap(); ?>
Wherever you insert this function is where the map will be displayed on the page.
Let us know
I hope this tutorial has been useful and you have learned something new from it. Feel free to post a comment below and share your experience with this class or the Google Maps API.
Related posts:
- Adding security to CodeIgniter forms with a custom library class The class that we are going to create within CI...
- Build a Twitter-like site with CodeIgniter and jQuery Part 2 Don't forget to check out part 1 if you have...
- Deep Linking with jQuery You can view a demo and download the resources for...
- Google Wave invite I recently got a Google Wave invite and it is...
Related posts brought to you by Yet Another Related Posts Plugin.

Thanks for a great article. Such a nice PHP class.
Nice tutorial
Great Tutorial
http://www.uklocksmiths.net
locksmiths
Thanks for that class and I was search it it for a long.
The following error occured...
Warning: require_once(DB.php) [function.require-once]: failed to open stream: No such file or directory in /var/www/html/googlemap/GoogleMapAPI.class.php on line 1239
Fatal error: require_once() [function.require]: Failed opening required 'DB.php' (include_path='.:/php/includes:/usr/share/pear') in /var/www/html/googlemap/GoogleMapAPI.class.php on line 1239
Plz gimme the format to set variables in DB.php
nice tutorial, thanks!
cool tutorial,thanks
@Abhilash: you're missing the DB function:
make sure you have DB.php and DB folder in the same directory
download it here:
http://pear.php.net/package/DB
Thank Jose your tip was very useful!
thanx perfect samples code...
Guys sorry im newbie onto this
i did this exactly as on this feed and i see a blank page.
The i download pear.php and DB.php
but i need any more?
i yous this class exactly like the above example so i guess the address to show would be the one on the example or i need ti create a db?
Cause this dont work for me.
sorry to bother
Why you delete my question?
I'm having this problem, any insight or direction:
Warning: file_get_contents() [function.file-get-contents]: URL file-access is disabled in the server configuration in /nfs/c01/h13/mnt/10526/domains/rentblvd.com/html/dat/classes/GoogleMapAPI.class.php on line 1343
Warning: file_get_contents(http://maps.google.com/maps/geo?&q=621%20N%2048th%20St%20%23%206%20Lincoln%20NE%2068502&output=csv&key=ABQIAAAABcFhv9eMaNjwsPhjh0CtURQ2Vr1Maqh9VM5ZubsemhfZe-a1_RTVr8JE7X__FQOFQsDpM5pcF7WALQ) [function.file-get-contents]: failed to open stream: no suitable wrapper could be found in /nfs/c01/h13/mnt/10526/domains/rentblvd.com/html/dat/classes/GoogleMapAPI.class.php on line 1343
I figured this problem out. file_get_contents was disabled on my server so i used an alternate method (code form this tutorial) http://www.php2k.com/blog/php/advanced-php/alternative-to-file_get_contents-using-curl/ to perform a similar function. Works great. Also, I'm not rocking the pear db, you DO NOT need it unless you want to catch the data, (only a problem if you plan on serving more than 15k requests for an address per day)
Hey - I just decided to do an overhaul and update the PHPGoogleMapAPI class to use the latest V3 of the Google Maps API. It's still in beta as I'm still working some of the kinks out, but just wanted to get this first revision out there and start getting the word out. Check it out! http://www.bradwedell.com/blog/category/phpgooglemapapiv3/
I've been using this class for a while now. I'm pulling my address information from MySQL. However, I'd like to categorize my listings in the sidebar by State and County. I can sort them coming out of MySQL, but I'd like headings in the sidebar. Is this possible?
ie.
Locations
Pennyslvania
location 1
location 2
New Jersey
location 1
location 2
Just wanted to update that the new link is actually http://code.google.com/p/php-google-map-api/ , and the old 2.5 version of the code can be found there as well.