Question

I need to find all the zipcodes with a certain range from a zipcode. I have all the zipcode lat/lon in a DB.

I have found two formulas online that vary slightly from each other. Which one is correct?

Formula 1:

def latRange = range/69.172
def lonRange = Math.abs(range/(Math.cos(Math.toRadians(zip.latitude)) * 69.172));
def minLat = zip.latitude - latRange
def maxLat = zip.latitude + latRange
def minLon = zip.longitude - lonRange
def maxLon = zip.longitude + lonRange

Formula 2: (is identical to formula one except for the following:)

def lonRange = Math.abs(range/(Math.cos(zip.latitude) * 69.172));

(The second one does not have Math.toRadians ) After I get the min/max Lat/Lon, I intend to search a DB table using a between criteria. Which formula is correct?

Was it helpful?

Solution

It depends on the units of your lat/long. If they are in degrees, you need to convert to radians.

OTHER TIPS

I would suggest letting the db do all the heavy lifting. Several DBs have geo add-ons, but here are a couple examples: MongoDB, postgres+postgis

If your latitude and longitude data isn't already in radians then you'll need to use the one that converts. You should be aware though that the way things are set up now you'll end up with a square range.

You'll be better off doing the distance calculation in your mysql query, using the Pythagorean theorem to find the distance so you end up with a circular range. This question should help you get started with that: mySQL select zipcodes within x km/miles within range of y .

If you need to improve it's performance you can index it using Sphinx.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top