C.1. Understanding Proximity SearchProximity search works by applying the law of cosines, which calculates the distance between two points on the globe. Because the law of cosines takes into account the curvature of the earth, it is considered better than other methods, such as the Pythagorean Theorem. An abridged version of the law of cosines is as follows: a = sin(latitude_1) * sin(latitude_2) b = cos(latitude_1) * cos(latitude_2) * cos(longitude_2 - longitude_1) c = arcos( a + b ) d = R*c In this law, the following facts are true:
This formula takes two latitude and longitude pairs, one for a location stored in your SQL data store, such as the address of an ATM, and one for a location provided by your end user, such as the user's current location around which she wants to find ATMs. During a proximity search, the distance is calculated between the user's current address and each business location within the specified search radius, and the results are then ordered by distance. In the following sections, I will go through a step-by-step approach to building your own spatial proximity search using SQL Server. |