Author: Seth McLean, Back-End Developer
So here’s a fairly common situation: Your organization has a handful of locations, and you want to provide a tool for people to find the one closest to them. Whether it’s a selection of retail partners providing your product or just some branch office locations, the convenience of letting people know where they can find you is important – and with a little well-organized information, we can make this happen.
It’s common to have a list of locations stored in your site’s database, usually with the name, address, locality and more. This may be good information for someone traveling the roads, but not very useful for pinpointing a position on the globe. What we need to work with is a simple set of coordinates: latitude and longitude. This is simple, easy to store and can be indexed for a rapid search. With a little math, we can use this to narrow down locations within a radius.
For Those Who Wish to Follow Along
For the purposes of this example, I’ll be using the U.S. data from the GeoNames data sets. You can download a zip file for most countries, containing a TSV file, which can be imported into your MySQL database via the tool of your choosing. This is a good sample to illustrate the performance of our queries, with more than two million records to search. We’ll want to be sure to index the “latitude” and “longitude” columns to speed up our searching.
Here’s the create table statement for the U.S. data that I used:
Using the Great-Circle Distance Formula and Limiting Distance
The great-circle distance is the shortest distance between two points on the surface of a sphere. This is determined by multiplying the radius of the sphere by the central angle of the two points (their latitude and longitude coordinates) in radians. Given the radius of the Earth is approximately 3,956 miles (the Earth isn’t exactly a perfect sphere, but this is close enough), we can express this in MySQL like so:
3956 * ACOS(COS(RADIANS(`orig_latitude`)) * COS(RADIANS(`latitude`)) * COS(RADIANS(`orig_longitude`) - RADIANS(`longitude`)) + SIN(RADIANS(`orig_latitude`)) * SIN(RADIANS(`latitude`)))
While that will provide the distance between the originating coordinate and the point of the location returned by the query, it’s quite calculation-heavy, and being run against a large data set can drastically hurt the query performance. The good news is that there’s a way around this. If we’re querying for locations nearby to a user, we can use our indices on the latitudes and longitudes to reduce the seeking of records to locations within proximity of the user’s latitude/longitude.
Since there are 69 miles per degree of latitude, we can limit our northern and southern bounds like so in the WHERE clause (where “radius” is our search radius):
`latitude` BETWEEN `orig_latitude` - (`radius` / 69) AND `orig_latitude` + (`radius` / 69)
Determining the east and west bounds is slightly different; since the miles per degree depends on the distance from the equator, we’ll use this function instead:
`longitude` BETWEEN `orig_longitude` - (`radius` / (69 * COS(RADIANS(`orig_latitude`)))) AND `orig_longitude` + (`radius` / (69 * COS(RADIANS(`orig_latitude`))))
So now that we have our formulas, it’s time to put the query together:
A few notes: I placed the main select as a subquery to limit by distance, as it is possible for the bounding-box limits for the latitude and longitude to produce a limited number of results that are outside the radius. As for the user’s latitude and longitude, this could be gathered via HTML5 Geolocation, but for the sake of this sample, hard-coding our office’s location will suffice.
We’re able to seek through 2,104,095 records, and return the 2,321 records with calculated distances within the search radius, all within 0.0474 seconds.