我想从下拉列表中计算 2 个选定位置之间的距离,我有一个表 ID、村庄名称、纬度、经度。
我确实遵循了一个关于计算的例子,但我不太明白,谁能帮助我??
地图.php
<?php
$village_id ="";
$sql = mysql_query("SELECT lattitude, longitude FROM village WHERE id = '$village_id' ")or die(mysql_error());
$get_row = mysql_fetch_assoc($sql);
$lattitude = $get_row['lattitude'];
$longitude = $get_row['longitude'];
if(isset($_POST['calculate']))
{
$pt1 = $_POST['pt1'];
$pt2 = $_POST['pt2'];
}
//function calculate distance
function distance($lat1, $lng1, $lat2, $lng2, $miles = true)
{
$result = "";
$lattitude = $lat1;
$lattitude = $lat2;
$longitude = $lng1;
$longitude = $lng2;
var_dump($lattitude);
$pi80 = M_PI / 180;
$lat1 *= $pi80;
$lng1 *= $pi80;
$lat2 *= $pi80;
$lng2 *= $pi80;
$r = 6372.797; // mean radius of Earth in km
$dlat = $lat2 - $lat1;
$dlng = $lng2 - $lng1;
$a = sin($dlat / 2) * sin($dlat / 2) + cos($lat1) * cos($lat2) * sin($dlng / 2) * sin($dlng / 2);
$c = 2 * atan2(sqrt($a), sqrt(1 - $a));
$km = $r * $c;
$result = ($miles ? ($km * 0.621371192) : $km);
echo $result;
}
//
?>
<div id="calculate-distance-form">
<?php require_once('include/select.class.php'); ?>
<form action="#" method="post">
Location one:
<select id="location1" name="pt1">
<?php echo $opt->Showlocation() ?>
</select><br />
<br />
Location two:
<select id="location2" name="pt2">
<option value="0">choose...</option>
</select><br />
<br />
<input type="submit" name="calculate" value="Calculate Distance" />
</form>