可能重复:
找到两个纬度/经度点之间距离的最快方法
我有一个数据库,其中包括城镇名称和经度和纬度坐标号。我有一个系统,用户在想租一辆公共汽车时输入接送地点和目的地。我能够查询 mysql 数据库以从正在招聘的人员中提取两个位置,但无法获取存储在数据库另一个表中的两个位置的坐标。我很确定计算两个位置之间距离的代码可以正常工作,因为它只在前面放数字而不是变量,但我不知道我做错了什么。任何帮助表示赞赏,谢谢。
$result = mysql_query("SELECT hireid FROM hire ORDER BY customerid DESC LIMIT 1");
while ($row = mysql_fetch_assoc($result)){
$hireid = $row['hireid'];
}
echo "hireid: " . $hireid . "<br/>";
$result = mysql_query("SELECT pickuplocation, destination FROM hire WHERE hireid='$hireid'");
while ($row = mysql_fetch_assoc($result)){
$pickuplocation = $row['pickuplocation'];
$destination = $row['destination'];
}
echo "pickuplocation: " . $pickuplocation . "<br/>";
echo "destination: " . $destination . "<br/>";
$result = mysql_query("SELECT longitude, latitude FROM coordinates WHERE town='$pickuplocation'") or die ('Error: '.mysql_error ());
while ($row = mysql_fetch_array($result)){
$long1 = $row['longitude'];
$lat1 = $row['latitude'];
}
"<br/>";
echo "Location1 longitude: " . $long1 . "<br/>";
echo "Location1 latitude: " . $lat1 . "<br/>";
$result = mysql_query("SELECT longitude, latitude FROM coordinates WHERE town='$destination'") or die ('Error: '.mysql_error ());
while ($row = mysql_fetch_array($result)){
$long2 = $row['longitude'];
$lat2 = $row['latitude'];
}
"<br/>";
echo "Location2 longitude: " . $long2 . "<br/>";
echo "Location2 latitude: " . $lat2 . "<br/>";
function getDistanceBetweenPointsNew($lat1, $long1, $lat2, $long2) {
$theta = $long1 - $long2;
$miles = (sin(deg2rad($lat1)) * sin(deg2rad($lat2))) + (cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($theta)));
$miles = acos($miles);
$miles = rad2deg($miles);
$miles = $miles * 60 * 1.1515;
$feet = $miles * 5280;
$yards = $feet / 3;
$kilometers = $miles * 1.609344;
$meters = $kilometers * 1000;
return compact('miles') . "<br/>";
}
$point1 = array('lat' => $lat1, 'long' => $long1);
$point2 = array('lat' => $lat2, 'long' => $long2);
$distance = getDistanceBetweenPointsNew($point1['lat'], $point1['long'], $point2['lat'], $point2['long']);
foreach ($distance as $unit => $value) {
echo $unit.': '.number_format($value,4).'<br />';
}