2013-04-26 105 views
1

我試過這段代碼,但結果有些錯誤:我只有1公里的結果,其他的都是0,並且它在解決方案或所有用戶或沒有人。也許我錯了mysql_fetch_array。這是PHP代碼: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

調試代碼'$ DLAT = $ LAT2 - $ LAT1;' '$ DLNG = $ lng2 - $ lng1;'你得到正確的價值,你想要什麼? – 2013-04-26 09:13:49

回答

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>"; 

http://www.geodatasource.com/developers/php

+0

它解決了距離問題,但不是問題,如果(km <1000)$解決方案[] = $行;與1235.125730806公里它進入那如果並且解答所有用戶...爲什麼? – 2013-04-26 11:47:00

+0

解決感謝:D – 2013-04-26 11:53:06

相關問題