可能重复:
Fastest Way to Find Distance Between Two Lat/Long Points
我有一个包含城镇名称和经度和纬度坐标数字的数据库。我有一个系统,当用户想要租用公共汽车时,用户输入接送地点和目的地。我能够查询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 />';
}