2levelsabove Posted March 18, 2008 Share Posted March 18, 2008 So I have not had a chance to research this yet but was just posting this out there for my fellow developers to help me a little bit. Where can I get the most updated database from ? Also any libraries or code samples that let you do that ? I know I am being lazy but just wanted your thoughts on it. Thanks !! Quote Link to comment Share on other sites More sharing options...
BlueSkyIS Posted March 18, 2008 Share Posted March 18, 2008 given a table of zip codes with lat and lon for each, and where $_GET['zipsearch'] is the target and $_GET['radius'] is the distance in miles: @$zipsearch = $_GET['zipsearch']; @$radius = $_GET['radius']; $dif = '2'; $results=''; $data=array(); //if(!empty($zipsearch)){} $query = "select * from zips where zip = '$zipsearch' LIMIT 1"; $result = mysql_query($query); $row = mysql_fetch_assoc($result); if(empty($row)){$results = "<tr><td align='center' class='results' style='font-size:16px;font-weight:bold;color:red;font-family:arial;'>Invalid zip code</td></tr>";} $lat = $row['lat'];$lon = $row['lon']; $lat1 = ($lat + $dif); $lat2 = ($lat - $dif); $lon1 = ($lon + $dif); $lon2 = ($lon - $dif); $query = "select * from zips where zips.lat < $lat1 AND zips.lat > $lat2 AND zips.lon < $lon1 AND zips.lon > $lon2"; $result = mysql_query($query); while($row = mysql_fetch_assoc($result)){ $dist = great_circle_distance($lat,$row['lat'],$lon,$row['lon']); if ($dist < $radius){ array_unshift($row,$dist); $row[0]=round($row[0],2); $data[] = $row; } } sort($data); // $data now has the zip codes and distances in increasing order of distance from $_GET['zipsearch']; function great_circle_distance($lat1,$lat2,$lon1,$lon2){ /* Convert all the degrees to radians */ $lat1 = deg_to_rad($lat1); $lon1 = deg_to_rad($lon1); $lat2 = deg_to_rad($lat2); $lon2 = deg_to_rad($lon2); /* Find the deltas */ $delta_lat = $lat2 - $lat1; $delta_lon = $lon2 - $lon1; /* Find the Great Circle distance */ $temp = pow(sin($delta_lat/2.0),2) + cos($lat1) * cos($lat2) * pow(sin($delta_lon/2.0),2); $EARTH_RADIUS = 3956; $distance = $EARTH_RADIUS * 2 * atan2(sqrt($temp),sqrt(1-$temp)); return $distance; } function deg_to_rad($deg){ $radians = 0.0; $radians = $deg * M_PI/180.0; return($radians); } zip code databases are plentiful. Quote Link to comment Share on other sites More sharing options...
2levelsabove Posted March 18, 2008 Author Share Posted March 18, 2008 You are the man my friend Quote Link to comment Share on other sites More sharing options...
variablesanity Posted March 18, 2008 Share Posted March 18, 2008 If this is a database driven application, you could also calculate the distance in the database query given the point of origin long/lat. and that every other destination has a long/lat. Check it out: http://maurus.net/resources/distance-queries/ Quote Link to comment Share on other sites More sharing options...
effigy Posted March 18, 2008 Share Posted March 18, 2008 MySQL GIS Quote Link to comment Share on other sites More sharing options...
BlueSkyIS Posted March 18, 2008 Share Posted March 18, 2008 ^ yes MySQL GIS is the best way to go. i posted code that i inherited, never had to update it for MySQL GIS implementation. Quote Link to comment Share on other sites More sharing options...
2levelsabove Posted March 18, 2008 Author Share Posted March 18, 2008 Thanks a lot guys. Theres a reason why this board is one of my favorites. Quote Link to comment Share on other sites More sharing options...
2levelsabove Posted March 18, 2008 Author Share Posted March 18, 2008 So I tried look for some GIS tutorials but really didn't see that much stuff out there. anybody got any cool stuff on MYSQL GIS . Thanks Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.