I have a database that I populate with specific locations of my choice. For each location, I will provide a longitude and latitude.
I want to get the geoIP (the longitude and latitude for a given IP matched in a database like maxmind.com) of a visitor to my website. Using the geoIP, I want to find the closest location to the visitor from my locations database table.
I've been spending a lot of time trying to figure out how to accomplish this in an efficient manner; I don't want the process to be expensive for every visitor. The process doesn't have to be precise, just precise enough. If it gives a user Sacramento instead of San Francisco, which would have been more correct, that's okay. Just as long as the degree of error is small enough that it wont bother a majority of users. What wouldn't be okay is if the algorithm gave a location completely off and irrelevant from where they are, say like Chicago (when they live in California).
So with that said, what are some solutions?
Here are some of my ideas:
1
Use the pythagorean theorem to find the distances between two points in a plane. The only problem is that the earth is not on a planar coordinate system, but rather a spherical one. So, I'll need a way to convert geolocation data (long. and lat.) into X and Y coordinates. Then I could run a SQL query that finds the record with the shortest distance
which is calculated using: sqrt(abs(locationX - geoIpX)^2 + abs(locationY - geoIpY)^2)
I'm not sure if this is a plausible solution. If it is, then please smooth out the rough edges for me so I can implement it.
2
Figure out an algorithm that uses the differences in longitudes and latitudes. For example, first find the locations that has the closest longitude to the geoIP's longitude, then find the location out of that set that has the closest latitude to the geoIP's latitude. The only issue with the algorithm just described is that the margin of error can potentially be huge. For example, say a city in California is 3 degrees longitude away from the geoIP, but a city in Canada's longitude is 2 degrees away, what will happen is that this longitude line would be used to find the closest latitude, which may be only the city in Canada. Despite the fact that the latitude in Canada is many more degrees further than the latitude in the city in California, the user will be presented with data relevant to a Canadian instead of a Californian, which would be too much of an error. However, maybe there are some modifications to this algorithm that may fix this?
afterword
Thanks for reading. All solutions and help are highly appreciated! :)