我已经尝试使用此代码,但结果有问题:我只得到 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));
}
}
?>