PHP Class for Google Maps API Comments (10)
by Wez Pyke - July 20, 2009 in PHP, Text Tutorials

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.

<?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.

Share and Enjoy:
  • Print this article!
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • E-mail this story to a friend!
  • FriendFeed
  • LinkedIn
  • Ping.fm
  • Tumblr
  • Twitter

Related posts:

  1. Adding security to CodeIgniter forms with a custom library class The class that we are going to create within CI...
  2. PHP Contact Form Every website needs a way of contacting the people behind...
  3. Google Wave invite I recently got a Google Wave invite and it is...
  4. Uploading files to Rackspace Cloud (Mosso) using PHP API Rackspace Cloud is a great VPS service, but when I...
  5. Easily and effectively display images with FancyBox FancyBox is a plugin for jQuery that allows you to...

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

Your Ad Here

10 Responses to “PHP Class for Google Maps API”

  1. Scott says:

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

  2. Declan says:

    Nice tutorial :)

  3. ManiG says:

    Great Tutorial

  4. locksmith says:

    http://www.uklocksmiths.net
    locksmiths

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

  5. Abhilash says:

    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 says:

    nice tutorial, thanks!

  7. cool tutorial,thanks

  8. Jose Lara says:

    @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

  9. Mariano says:

    Thank Jose your tip was very useful!

  10. fare says:

    thanx perfect samples code...

Leave a Reply