我已尝试使用此代码,但结果出现问题:我只获得了1公里的结果,其他人的结果是0,并且它会进入解决方案或所有用户或没有人。也许我错了mysql_fetch_array。这是php代码:
<?php
$response = array();
$username=$_POST["user"];
if($username!=null){
$latm=0;
$lonm=0;
mysql_connect("localhost:3306","blabla","lol");
mysql_select_db("my_app");
$posut1=mysql_query(" SELECT lat,lon FROM utenti WHERE user='$username' ");
$posut2= mysql_query(" SELECT lat,lon,user FROM utenti WHERE user!='$username' ");
if($posut1){
$row = mysql_fetch_array($posut1);
list($latm,$lonm)=$row;}
if ($posut2) {
$solutions = array();
$i=0;
while ($row = mysql_fetch_array($posut2))
{
list($lat2, $lng2, $us) = $row;
$lat1=$latm;
$lng1=$lonm;
$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;
$kmm[$i]=$km;
if(km<1000) $solutions[]=$row;
$i++;}
$response["km"] = $kmm;
$response["success"] = $solutions;
// echoing JSON response
print(json_encode($response));
} else {
$response["success"] = 0;
$response["message"] = "dati errati";
// echoing JSON response
print(json_encode($response));
}
}
?>
答案 0 :(得分:0)
$kmm=array();// add this before while
并提供一些信息
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;
}
}
echo distance(32.9697, -96.80322, 29.46786, -98.53506, "M") . " Miles<br>";
echo distance(32.9697, -96.80322, 29.46786, -98.53506, "K") . " Kilometers<br>";
echo distance(32.9697, -96.80322, 29.46786, -98.53506, "N") . " Nautical Miles<br>";