使用gps坐标从当前位置找到距离


Find distance from current location using gps coordinates

我正在尝试找出从我当前位置的特定gps点的距离。所以我有一个HTML文件,计算和显示当前位置。然后将坐标通过GET消息发送到服务器。服务器端的php文件中有一个计算距离的函数。但是,php文件中没有显示echo。下面是我的实现:

HTML文件

<!DOCTYPE HTML>
<html>
<head>
<script>
var lat;
var lng;
        function clicked(){
        if(navigator.geolocation){
            navigator.geolocation.getCurrentPosition(success,error);
        }
        else{
            document.getElementById("hello").innerHTML = "Not supported";
        }
        function success(position){
                lat = position.coords.latitude;
                lng = position.coords.longitude;
                document.getElementById("hello").innerHTML = "lat :"+lat+"<br>long :"+lng;
                }
        function error(err){
                document.getElementById("hello").innerHTML = "Error Code: "+error.code;
                if(err.code == 1){
                    document.getElementById("hello").innerHTML = "Access denied";
                    }
                if(err.code == 2){
                    document.getElementById("hello").innerHTML = "Position unavailable";
                }
        }
        var xmlhttp;
        if (window.XMLHttpRequest){ 
                xmlhttp = new XMLHttpRequest();
            }else{ 
                xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
            }
            xmlhttp.onreadystatechange = function(){
                if (xmlhttp.readyState==4 && xmlhttp.status==200){
                    document.getElementById("list").innerHTML = xmlhttp.responseText;
                    }
            }
            xmlhttp.open("GET","queryRestaurantsList.php?lat="+lat+"&lng="+lng,true);
            xmlhttp.send(); 
        }
</script>   
</head>
<body>
<div onclick="clicked()">Click Me!</div>
<div id="hello"></div>
<div id="list"></div>
</body>
</html>
PHP文件

<?
mysql_connect($host,$username,$password);
mysql_select_db($database) or die( "Unable to select database");
echo "HEllo WOrld!";

function distance($lat1,$lng1,$lat2,$lng2,$unit){
    $theta = $lng1 - $lng2;
    $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 == "M") {
        return (($miles * 1.609344)/1000);
      } 
}
//Check if POST array is empty or not
$lat1 = $_GET['lat'];
$lng1 = $_GET['lng'];
echo $lat1;
echo $lng1;
$query = "SELECT RestaurantID,Latitude,Longitude FROM RGeoLocation"
$result = mysql_query($query) or die("Unable to execute query");
$i = 0;
$answer = array();
while ($row = mysql_fetch_array($result)){
        $lat2 = row['Latitude'];
        $lng2 = row['Longitude'];
        $id = row['RestaurantID'];
        $answer[$i] = distance($lat1,$lng1,$lat2,$lng2,"M")
        $i++;
    } 
echo $answer;
?>

我记得您需要xmlhttp。打开第二个参数为false(这将导致脚本暂停并等待响应)。然后需要编写xmlhttp。responseText into from java, xmlhttp。repsonseText应该包含php脚本的输出

所以这不是一个关于GPS距离计算的问题-它更多的是关于基本的PHP基础知识。

在你的PHP脚本$answer是一个数组。不能像回显字符串或整数那样简单地回显数组。

也许你应该发送JSON编码的响应:

echo json_encode($answer);