Creating a Store Locator with PHP, MySQL & 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 to create a store locator-type app. After completing this tutorial, you will have a database of locations and a webpage that lets a user enter their address and see markers on a map for the locations nearest to them, within a chosen distance restriction. Since the Maps API v3 is designed to work well on modern mobile browsers, this article will show how to create a website that displays nicely on them.

Advertisment
Creating the Table

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.

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. You can check out our tutorial on using the Google Maps API geocoder, or check out this link for a good list of geocoders: http://groups.google.com/group/Google-Maps-API/web/resources-non-google-geocoders

Advertisment

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

publive-image

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:

Advertisment
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 ) ENGINE = MYISAM ;
Click here to continue reading...!
(The above article is taken from code.google.com under Creative commons licensing)
tech-news