php - MySQL Great Circle Distance (Haversine formula) -
i've got working php script gets longitude , latitude values , inputs them mysql query. i'd make solely mysql. here's current php code:
if ($distance != "any" && $customer_zip != "") { //get great circle distance //get origin zip code info $zip_sql = "select * zip_code zip_code = '$customer_zip'"; $result = mysql_query($zip_sql); $row = mysql_fetch_array($result); $origin_lat = $row['lat']; $origin_lon = $row['lon']; //get range $lat_range = $distance/69.172; $lon_range = abs($distance/(cos($details[0]) * 69.172)); $min_lat = number_format($origin_lat - $lat_range, "4", ".", ""); $max_lat = number_format($origin_lat + $lat_range, "4", ".", ""); $min_lon = number_format($origin_lon - $lon_range, "4", ".", ""); $max_lon = number_format($origin_lon + $lon_range, "4", ".", ""); $sql .= "lat between '$min_lat' , '$max_lat' , lon between '$min_lon' , '$max_lon' , "; }
does know how make entirely mysql? i've browsed internet bit of literature on pretty confusing.
from google code faq - creating store locator php, mysql & google maps:
here's sql statement find closest 20 locations within radius of 25 miles 37, -122 coordinate. calculates distance based on latitude/longitude of row , target latitude/longitude, , asks rows distance value less 25, orders whole query distance, , limits 20 results. search kilometers instead of miles, replace 3959 6371.
select id, ( 3959 * acos( cos( radians(37) ) * cos( radians( lat ) ) * cos( radians( lng ) - radians(-122) ) + sin( radians(37) ) * sin(radians(lat)) ) ) distance markers having distance < 25 order distance limit 0 , 20;
Comments
Post a Comment