2011-11-27 83 views

回答

1

由於你的問題有點含糊,我假設你有一個lat,long座標列表。您可以使用這樣的事情讓他們出去的SQL:

SELECT 
z.id AS z__id, 
z.zipcode AS z__zipcode, 
z.city AS z__city, 
z.state AS z__state, 
z.county AS z__county, 
z.zip_class AS z__zip_class, 
z.latitude AS z__latitude, 
z.longitude AS z__longitude, 
((ACOS(SIN(* PI()/180) * SIN(z.latitude * PI()/180) + COS(* PI()/180) * COS(z.latitude * PI()/180) * COS((- z.longitude) * PI()/180)) * 180/PI()) * 60 * 1.1515) AS z__0, 
((ACOS(SIN(* PI()/180) * SIN(z.latitude * PI()/180) + COS(* PI()/180) * COS(z.latitude * PI()/180) * COS((- z.longitude) * PI()/180)) * 180/PI()) * 60 * 1.1515 * 1.609344) AS z__1 
FROM zipcode z 
WHERE z.city != ? 
ORDER BY z__0 asc 
LIMIT 50 

Z_ 0將在英里dinstance,而ž _1將成爲公里的距離。

(來源:http://www.doctrine-project.org/projects/orm/1.2/docs/manual/behaviors/en#core-behaviors:geographical

或者使用類似這樣(在PHP類似):

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 ($miles * 1.609344); 
    } else if ($unit == "N") { 
     return ($miles * 0.8684); 
    } else { 
     return $miles; 
     } 
} 

http://www.zipcodeworld.com/samples/distance.php.html