If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > Search nearby in distance

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 01-06-06, 04:15
flyingduck flyingduck is offline
Registered User
 
Join Date: Jan 2006
Posts: 1
Search nearby in distance

Hi all
I'm looking for something like find nearby attraction (ID=1) within radius of 5km and disply distance and show distance asc...
Dose anyone can help me to figure out how to do this without GIS feature.

I'm trying to figure out how to put formula inside one sql statement to get desire result out put

something like this but having problem..
Code:
SELECT a.m_name, b.m_name, round(degrees(acos(sin(radians(a.att_lat)) * sin(radians(b.att_lat)) + cos(radians(a.att_lat)) * cos(radians(b.att_lat)) * cos(radians(a.att_lon - b.att_lon)))) *60 * 1.1515) AS distance
FROM (
SELECT m_name, att_lat, att_lon
FROM attraction
WHERE ID = '1'
)a attraction b
WHERE round( degrees( acos( sin( radians( a.att_lat ) ) * sin( radians( b.att_lat ) ) + cos( radians( a.att_lat ) ) * cos( radians( b.att_lat ) ) * cos( radians( a.att_lon - b.att_lon ) ) ) ) *60 * 1.1515 ) > 5 ORDER BY distance ASC

Code:
table: attraction
+---+---------+---------+---------+
| ID | m_name | att_lat | att_lon | 
+---+---------+---------+---------+
| 1  | att_1  | 51.6528 | -0.2023 |
| 2  | att_2  | 51.5915 | -0.2283 |
| 3  | att_3  | 51.5953 | -0.1893 |
+---+---------+--------+----------+

Result out put...
+--------+---------+
| m_name | distance| 
+--------+---------+
| att_2  |    1.22 |
| att_3  |    2.51 |
| att_4  |    3.12 |
| att_5  |    4.34 |
+--------+---------+
Caluculating distance between two distance...
Code:
function distance($lat1, $lon1, $lat2, $lon2, $unit) {  
  $theta = $lon1 - $lon2;  
  $dist = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) +  cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($theta));  
  $dist = acos($dist);  
  $dist = rad2deg($dist);  
  $miles = $dist * 60 * 1.1515; 
  $unit = strtoupper($unit); 

  if ($unit == "K") { 
    return round(($miles * 1.609344),2); 
  } else if ($unit == "N") { 
      return round(($miles * 0.8684),2); 
    } else { 
        return  round($miles,2); 
      } 
} 
echo distance(32.9697, -96.80322, 29.46786, -98.53506, "k") . " kilometers<br>"; 
?>
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On