Create Your Own Store Locator with Google Maps, PHP, and MySQL
This occasional series introducing various facets of the powerful Google Maps API has examined quite a few different features that are likely to appear in an enterprise web-based mapping implementation, ranging from calculating user-defined route distances to using the Google Maps API Geocoder to convert mailing addresses to their respective coordinates. In this installment, I discuss yet another feature that has become standard, particularly on the web sites of large retail chains: the "Store Locator."
The Location Table
Let's start by creating the table, which will contain information about each of the retail chain's outlets. For the purposes of example, I'll chart several locations of my favorite store, Barnes & Noble. This table, called
location, contains each outlet's name, mailing address, and latitudinal/longitudinal coordinates. The coordinates were presumably saved to the database after geocoding the outlet's address using the Google Maps API geocoder. As I explained this in a previous article, I'll leave this task as an exercise for you and instead just present the
location table structure:
CREATE TABLE location ( id INTEGER UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, name VARCHAR(100) NOT NULL, street1 VARCHAR(100) NOT NULL DEFAULT '', street2 VARCHAR(100) NOT NULL DEFAULT '', city VARCHAR(100) NOT NULL DEFAULT '', zip VARCHAR(10) NOT NULL DEFAULT '', state CHAR(2) NOT NULL DEFAULT '', latitude FLOAT(10,6) NOT NULL, longitude FLOAT(10,6) NOT NULL );
With several stores in the database, I can plot their locations simultaneously on a map, as shown in Figure 1.
Figure 1. Plotting Ohio-based Barnes & Noble Locations: I can plot all store locations in the database simultaneously on a map.