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...
- PHP Contact Form Every website needs a way of contacting the people behind...
- Deep Linking with jQuery You can view a demo and download the resources...
- 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...