Gone are the days when people have to get on to their shoes and explore their locality to know what is where nearby. With the advent of GPS, exploring a locality is just one click away.

Today, it’s a basic necessity for any business to let their customers know where they can get its products or services nearby their locality. It’s the friendly way for the business to say ‘we are here nearby to serve you’.

So, it’s a no wonder to see links like ‘Store Finder’, ‘Locate your store’ on any corporate website that run business across a nation or around the globe. How this is being implemented? To know this, read further.

Let us take an example of locating nearby ATM of a popular bank that runs business across a country. The following schema defines the table structure of ATM locations.

Table Name: TBL_ATM_LOCATIONS

S.NoField NameField Type
1ATM_IDInteger
2ATM_ADDRESSVarchar
3ATM_CITYVarchar
4ATM_STATEVarchar
5ATM_ZIPCODEVarchar

 

This schema is enough, if the ATM needs to be located by exact zip code. This can be the case, only if the bank has ATMs in each zip code of the country and then users could get the location of the ATM in their zip code. If the Bank doesn’t have ATMs in each zip code, then this table won’t provide information for the users who don’t have the Bank’s ATM in their zip code. Here, we need to add some more information to the table schema to make it show the nearest ATMs for those users.

Next come the question – how do we know the nearest zip code? Here comes the mathematics for our help. Since our earth is a near spherical in shape, it is possible to calculate the approximate distance between two points on the earth. For this, the coordinates of each point are required. So, we need latitude and longitude detail for each zip code. Now we add two new fields to the table “TBL_ATM_LOCATIONS”.

S.NoField NameField Type
1ATM_IDInteger
2ATM_ADDRESSVarchar
3ATM_CITYVarchar
4ATM_STATEVarchar
5ATM_ZIPCODEVarchar
6LATITUDENumeric
7LONGITUDENumeric

 

Now we added latitude and longitude on the table schema, but where do we get the actual data for these for each zip code? There are many third party zip code databases available on the internet that provide the latitude, longitude details for zip codes of any specific country. Most of them are commercially licensed. A few free databases are also available. One such free zip code database can be found at http://www.free-zipcodes.com/

The zip code table would typically look like the following,

Table Name: TBL_ZIP_CODE

S.NoField NameField Type
1ZIP_CODEVarchar
2LATITUDENumeric
3LONGITUDENumeric

 

The third party databases might have some other extra fields. The above are the necessary fields for our purpose. Whenever a new ATM record is added to the TBL_ATM_LOCATIONS table, get its latitude and longitude details from the TBL_ZIP_CODE table and store in TBL_ATM_LOCATIONS table.

Now, we have all required details to search the nearby ATMs. Let us try to find a nearby ATM that is within a radius of 5 KM for a zip code XXXXX. First, get the latitude and longitude for the zip code XXXXX from theTBL_ZIP_CODE table.

SELECT LATITUDE, LONGITUDE FROM TBL_ZIP_CODE WHERE ZIP_CODE='XXXXX'

Let us call its latitude and longitude as SRC_LAT and SRC_LONG respectively.

Now, we create a MYSQL Query that will get us the nearest ATMs within 5 KM radius,

SELECT *, (6371* ACOS( COS( RADIANS(SRC_LAT) ) * COS( RADIANS( LATITUDE ) ) * COS( RADIANS( SRC_LONG ) - RADIANS(LONGITUDE) ) + SIN( RADIANS(SRC_LAT) ) * SIN( RADIANS( LATITUDE ) ) ) ) AS DISTANCE FROM TBL_ATM_LOCATIONS HAVING DISTANCE<=5

In this query, we have used the following mathematical formula to calculate the distance,

Distance in KM = 6371 * acos( cos(radians(lat1)) * cos(radians(lat2)) * cos(radians(long1) – radians(long2)) + sin(radians(lat1)) * sin(radians(lat2)) )

This MYSQL query will get the list of ATMs that are within 5 KM radius from the zip code XXXXX. Further, these ATM locations can be shown on google map using google maps API and it would be a good user experience.

A PHP script with a sample database to accomplish the above the example has been given atm_finder. Kindly note that the zip code table in the sample database has only less records on US zipcodes and so the example won’t work for all US zipcodes. Below are the screenshots for this example script.

Screenshot showing the list of ATM locations for a given zipcode

Screenshot showing the ATM Locations in google map for a given zipcode.

Related Resources

Successful Web Application Development

Successful Web Application Development

Web Programming

This article meant for small, web application and web site development companies which need some formal guidelines in their development activities toward success. Written in

Writing clean, secure and easy to maintain PHP code

Writing clean, secure and easy to maintain PHP code

Web Programming

Any code when written in a clean, easy to understand and formatted way is readily accepted and acclaimed by one and all. It is essential

Search Engine Optimization Websites Using Flash

Search Engine Optimization Websites Using Flash

Web Programming

Overview Flash enabled sites can have stunning look, but it may not help to achieve the top position in the search engine. Flash movies have