0

I have multiple addresses recorded in my database and I've used the Google 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."

Viktor Gavras
  • 55
  • 1
  • 5

3 Answers3

1

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))
1

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

Community
  • 1
  • 1
NullUserException
  • 83,810
  • 28
  • 209
  • 234
0

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 ) ) ;
}
emory
  • 10,725
  • 2
  • 30
  • 58