PHP Class for Google Maps API

google maps logo small blue PHP Class for Google Maps APIA 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.

$map = new GoogleMapAPI('map');
// enter YOUR Google Map Key
$map->addMarkerByAddress('621 N 48th St # 6 Lincoln NE 68502','Our Address','<b>Our Address</b>');
<body onload="onLoad()">
    <?php $map->printMap(); ?>

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.


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.

Bookmark and Share
Share and Enjoy:
  • Print
  • Digg
  • Sphinn
  • Facebook
  • Mixx
  • Google Bookmarks
  • email
  • FriendFeed
  • LinkedIn
  • Tumblr
  • Twitter

No related posts.

Related posts brought to you by Yet Another Related Posts Plugin.

, , , , , , , , , ,

17 Responses to PHP Class for Google Maps API

  1. Scott July 20, 2009 at 11:51 am #

    Thanks for a great article. Such a nice PHP class.

  2. Declan July 20, 2009 at 12:31 pm #

    Nice tutorial :)

  3. ManiG August 5, 2009 at 5:33 am #

    Great Tutorial

  4. locksmith August 14, 2009 at 4:17 pm #

    Thanks for that class and I was search it it for a long.

  5. Abhilash September 7, 2009 at 1:04 am #

    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

  6. maria October 5, 2009 at 2:35 pm #

    nice tutorial, thanks!

  7. MU VIRGO SEASON 5 October 7, 2009 at 8:06 pm #

    cool tutorial,thanks

  8. Jose Lara October 9, 2009 at 6:28 pm #

    @Abhilash: you're missing the DB function:

    make sure you have DB.php and DB folder in the same directory

    download it here:

  9. Mariano November 2, 2009 at 11:40 pm #

    Thank Jose your tip was very useful!

  10. fare December 28, 2009 at 8:20 am #

    thanx perfect samples code...

  11. Simon March 13, 2010 at 12:21 am #

    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

  12. Simon March 17, 2010 at 8:52 pm #

    Why you delete my question?

  13. James April 14, 2010 at 12:09 am #

    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/ on line 1343

    Warning: file_get_contents( [function.file-get-contents]: failed to open stream: no suitable wrapper could be found in /nfs/c01/h13/mnt/10526/domains/ on line 1343

  14. James April 14, 2010 at 12:30 am #

    I figured this problem out. file_get_contents was disabled on my server so i used an alternate method (code form this tutorial) 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)

  15. Brad May 3, 2010 at 11:23 am #

    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!

  16. Tim May 5, 2010 at 2:18 pm #

    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?


    location 1
    location 2

    New Jersey
    location 1
    location 2

  17. Brad May 6, 2010 at 11:47 am #

    Just wanted to update that the new link is actually , and the old 2.5 version of the code can be found there as well.

Leave a Reply