I have multiple addresses recorded in my database and I've used the Goo开发者_StackOverflowgle Geocoding service to get their longitude, latitude, etc.
What do I need to do to calculate the proximity of one address from another in miles?
Ideally I want to show an entry and then say below it: "The following recorded addresses are close by."
There are several algorithms that convert latitude/longitude coordinates to distance in miles.
Refer to: http://www.meridianworlddata.com/Distance-Calculation.asp
I tested all of them, and if you are really worried about performance, you can use:
sqrt(x * x + y * y)
where x = 69.1 * (lat2 - lat1)
and y = 53.0 * (lon2 - lon1)
Which is a good enough approximation.
Related: PHP MySql and geolocation
see http://www.movable-type.co.uk/scripts/latlong.html for example.
In meter:
D_AB = 6378137 * acos( cos(PI/ 180 * latA) * cos(PI/ 180 * lngA) *
cos(PI/ 180 * latB) * cos(PI/ 180 * lngB) + cos(PI/ 180 * latA) *
sin(PI/ 180 * lngA) * cos(PI/ 180 * latB) * sin(PI/ 180 * lngB) +
sin(PI/ 180 * latA) * sin(PI/ 180 * latB))
If all you are worried about is proximity then Manhattan distance may be sufficient.
boolean isProximate ( double latE , double longE )
{
return ( ( Math . abs ( latA - latB ) < latE ) && ( Math . abs ( longA - longB ) < longE ) ) ;
}
精彩评论