Wednesday, November 23, 2011

Implementing Google Maps with PHP

The first thing you need to do is go to http://www.phpinsider.com/php/code/GoogleMapAPI/ 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.
 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','Our Address');   
<body onload="onLoad()">$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.
$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
 $map->printMap(); ?>

Wherever you insert this function is where the map will be displayed on the page.

No comments:

Post a Comment