[英]Distance between 2 GPS locations PHP
I am trying to display concatenated GPS locations in a string if the distance between the input location and the one on the database is lesser than 50 kms. 如果输入位置与数据库中的位置之间的距离小于50公里,我试图以字符串形式显示连接的GPS位置。 To do this I use a function distance($lat1, $lon1, $lat2, $lon2, $unit) which has the parameters for latitude and longitudes of the two locations.
为此,我使用一个函数distance($ lat1,$ lon1,$ lat2,$ lon2,$ unit),该函数具有两个位置的经度和纬度参数。 This function seems to work fine in many cases but I get a NAN as output (distance between points) in the string sometimes.
在许多情况下,此功能似乎可以正常工作,但有时在字符串中得到NAN作为输出(点之间的距离)。 I do not know what is causing it whether the function of the input.
我不知道是什么原因导致了该功能的输入。 Please check my code and sample input output below.
请在下面检查我的代码和示例输入输出。 PS : $date is the latitude of the input location.
PS:$ date是输入位置的纬度。
function getGPS($date,$gps){
$stringa="";
$connx = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $connx ) {
die('Could not connect: ' . mysql_error());
}
$sqlx = 'SELECT gpslat,gpslong FROM whosonline';
mysql_select_db('revietbw_reviewitappDB');
$retvalx = mysql_query( $sqlx, $connx );
if(! $retvalx ) {
die('Could not get data: ' . mysql_error());
}
while($row = mysql_fetch_assoc($retvalx)) {
$glat=(float)$row['gpslat'];
$glong=(float)$row['gpslong'];
$dis=distance((float)$date, (float)$gps, $glat, $glong, "K");
echo $dis;
if($dis<100){
$stringa = $stringa . ":" . $glat . " " . $glong;
}
}
//echo "Fetched data successfully\n";
mysql_close($connx);
return $stringa;
}
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;
}
}
Sample Input : gpslong=101.1020&gpslat=11.1241
Output : NANNANNAN872.77757900898
样本输入:
gpslong=101.1020&gpslat=11.1241
输出: NANNANNAN872.77757900898
The reason for NAN values was the 0 difference between the input and database GPS locations. NAN值的原因是输入和数据库GPS位置之间的差异为0。 I handled them separately.
我分开处理了。
if($dis<100 &$dis!='NAN'){
$stringa = $stringa . ":". $unz . " " . $glat . " " . $glong;
}
would do the job. 会做的工作。 Additionally I would also recommend to use Mysqli or pdo as its deprecated in PHP 5.5 and unsupported in higher PHP 7.
另外,我还建议使用Mysqli或pdo,因为它在PHP 5.5中已弃用,而在更高版本的PHP 7中则不受支持。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.