Javascript 将当前位置坐标作为GET消息发送到服务器

Javascript 将当前位置坐标作为GET消息发送到服务器,javascript,php,Javascript,Php,我需要将当前位置发送到服务器上的php文件。但是,我使用的方法在找到位置坐标之前打印出php响应的结果。显示坐标需要2-3秒: <script> var lat; var lng; function clicked(){ if(navigator.geolocation){ navigator.geolocation.getCurrentPosition(success,error); } e

我需要将当前位置发送到服务器上的php文件。但是,我使用的方法在找到位置坐标之前打印出php响应的结果。显示坐标需要2-3秒:

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

var lat;
乏液化天然气;
函数单击(){
if(导航器.地理位置){
navigator.geolocation.getCurrentPosition(成功,错误);
}
否则{
document.getElementById(“hello”).innerHTML=“不受支持”;
}
功能成功(职位){
纬度=位置坐标纬度;
lng=位置坐标经度;
document.getElementById(“hello”).innerHTML=“lat:+lat+”
long:+lng; } 函数错误(err){ document.getElementById(“hello”).innerHTML=“错误代码:”+Error.Code; 如果(err.code==1){ document.getElementById(“hello”).innerHTML=“拒绝访问”; } 如果(err.code==2){ document.getElementById(“hello”).innerHTML=“位置不可用”; } } } var-xmlhttp; 如果(window.XMLHttpRequest){ xmlhttp=新的XMLHttpRequest(); }否则{ xmlhttp=新的ActiveXObject(“Microsoft.xmlhttp”); } xmlhttp.onreadystatechange=函数(){ if(xmlhttp.readyState==4&&xmlhttp.status==200){ document.getElementById(“列表”).innerHTML=xmlhttp.responseText; } } open(“GET”、“queryrestarantslist.php?lat=“+lat+”&lng=“+lng,true”); xmlhttp.send(); }
php文件 $\u POST的回显在找到坐标之前出现,因此$\u POST数组显示为空

<?

echo var_dump($_POST);

echo "HEllo WOrld!";

?>

有几件事

1) 你(试图)通过GET而不是邮寄发送坐标。我希望$u POST是空的。
2) 您希望将AJAX调用移动到success()回调处理程序。e、 g

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

            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(); 

            }

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

    }
}

</script>   

var lat;
乏液化天然气;
函数单击(){
if(导航器.地理位置){
navigator.geolocation.getCurrentPosition(成功,错误);
}
否则{
document.getElementById(“hello”).innerHTML=“不受支持”;
}
功能成功(职位){
纬度=位置坐标纬度;
lng=位置坐标经度;
document.getElementById(“hello”).innerHTML=“lat:+lat+”
long:+lng; var-xmlhttp; 如果(window.XMLHttpRequest){ xmlhttp=新的XMLHttpRequest(); }否则{ xmlhttp=新的ActiveXObject(“Microsoft.xmlhttp”); } xmlhttp.onreadystatechange=函数(){ if(xmlhttp.readyState==4&&xmlhttp.status==200){ document.getElementById(“列表”).innerHTML=xmlhttp.responseText; } } open(“GET”、“queryrestarantslist.php?lat=“+lat+”&lng=“+lng,true”); xmlhttp.send(); } 函数错误(err){ document.getElementById(“hello”).innerHTML=“错误代码:”+Error.Code; 如果(err.code==1){ document.getElementById(“hello”).innerHTML=“拒绝访问”; } 如果(err.code==2){ document.getElementById(“hello”).innerHTML=“位置不可用”; } } } }

我不确定xmlhttp.onreadystatechange可能需要在函数范围之外。我使用jQuery,它可以为您处理所有的事情

你至少有两个错误。首先,您需要在
getCurrentPosition
的成功回调中创建并发送请求,
GET
请求的参数将不在
$\u POST