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.
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.
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
Wherever you insert this function is where the map will be displayed on the page.
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>
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();
The last function that we add is
<?php $map->printMap(); ?>
No comments:
Post a Comment