Using PHP/MySQL with Google Maps

author-image
CIOL Bureau
Updated On
New Update

BANGALORE, INDIA: This tutorial is intended for developers who are familiar with PHP/MySQL, and want to learn how to use Google Maps with a MySQL database.

Advertisment

After completing this tutorial, you will have a Google Map based off a database of places. The map will differentiate between two types of places, restaurants and bars by giving their markers distinguishing icons. An info window with name and address information will display above a marker when clicked.

The tutorial is broken up into the following steps:

Creating the Table
Advertisment

When you create the MySQL table, you want to pay particular attention to the lat and lng attributes. With the current zoom capabilities of Google Maps, you should only need 6 digits of precision after the decimal. To keep the storage space required for your table at a minimum, you can specify that the lat and lng attributes are floats of size (10,6). That will let the fields store 6 digits after the decimal, plus up to 4 digits before the decimal, e.g. -123.456789 degrees. Your table should also have an id attribute to serve as the primary key, and a type attribute to distinguish between restaurants and bars.

Note: This tutorial uses location data that already have latitude and longitude information needed to plot corresponding markers. If you're trying to use your own data that don't yet have that information, use a batch geocoding service to convert the addresses into latitudes/longitudes. Some sites make the mistake of geocoding addresses each time a page loads, but doing so will result in slower page loads and unnecessary repeat geocodes. It's always better to hardcode the latitude/longitude information when possible. This link contains a good list of geocoders: http://groups.google.com/group/Google-Maps-API/web/resources-non-google-geocoders

If you prefer interacting with your database through the phpMyAdmin interface, here's a screenshot of the table creation.

Advertisment

If you don't have access to phpMyAdmin or prefer using SQL commands instead, here's the SQL statement that creates the table (phpsqlajax_createtable.sql):

CREATE TABLE `markers` (  `id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,  `name` VARCHAR( 60 ) NOT NULL ,  `address` VARCHAR( 80 ) NOT NULL ,  `lat` FLOAT( 10, 6 ) NOT NULL ,  `lng` FLOAT( 10, 6 ) NOT NULL ,  `type` VARCHAR( 30 ) NOT NULL) ENGINE = MYISAM ;
Advertisment
Click here to know more...!
(Pamela Fox and Ben Appleton, Google Geo Team, With contributions from Lary Stucker, Maps API Developer April 2007)
(The above article is taken from code.google.com under Creative commons licensing)
tech-news