Distance Equation Help, once again..

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Distance Equation Help, once again..

Post by s.dot »

Given a zip code 46952 with accurate lat and long

Code: Select all

Zip: 46952
Lat: 40.573173
Long: -85.668479
Trying to find a minimum/maximum lat & long within a 50 mile radius of that zip code:

I have this function:

Code: Select all

function getPoints($zip,$range){

        //    this pulls the data seen above
	if(!$latlon = $this->getZipData($zip)){
		return false;
	}
	
	// declare variables for easier usage
	$lat = $latlon['lat'];
	$lon = $latlon['long'];
	
	// calculate ranges
	$lat_range = $range/69.172;
	$lon_range = abs($range/(cos($lat) * 69.172));
	
	$min_lat = $lat - $lat_range;
	$max_lat = $lat + $lat_range;
	$min_lon = $lon - $lon_range;
	$max_lon = $lon + $lon_range;
	
	return array($min_lat,$max_lat,$min_lon,$max_lon);
	
}
Which outputs:

Code: Select all

Array
(
    [0] => 39.8503371705  // min lat
    [1] => 41.2960088295  // max lat
    [2] => -86.4179771882 // min long
    [3] => -84.9189808118 // max long
)
So, to sum it up: To find all locations within 50 miles of zip 46952, I would use those values in my query. However this pulls some (close but not accurate) data that is a bit off.

Is this function doing the math correctly?

Does anyone know of a good resource where I can find the min/max lats/longs within xx Miles of zip xxxxx to check my data against?
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
Post Reply