Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/426.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 当前坐标返回未定义_Javascript - Fatal编程技术网

Javascript 当前坐标返回未定义

Javascript 当前坐标返回未定义,javascript,Javascript,我正在尝试使用getLocation函数获取当前位置坐标,并在将来使用这些坐标进行距离计算。在我运行下面的代码之后,我没有定义 <script type="text/javascript"> var currentLatitude; var currentLongitude; function getLocation() { if (navigator.geolocation) { navigator.geolocation.getCurrentPositi

我正在尝试使用getLocation函数获取当前位置坐标,并在将来使用这些坐标进行距离计算。在我运行下面的代码之后,我没有定义

<script type="text/javascript">
var currentLatitude;
var currentLongitude;

function getLocation() {
    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(getCoordinates);
    } else { 
        document.getElementById("asdfsdafsdaf").innerHTML = "Geolocation is not supported by this browser.";
    }
}

function getCoordinates(position) {
    currentLatitude = position.coords.latitude;
    currentLongitude = position.coords.longitude;   
}

var xhr = new XMLHttpRequest();
xhr.open("POST", "rsvpButton.php", true);
var formData = new FormData();
    formData.append("userID", 100100);
xhr.send(formData);

xhr.onload=function(){
    getLocation();
    alert("1"+currentLongitude+" and "+currentLatitude);
    //some other codes to display the page
    //...
}
</script>

无功电流纬度;
经度;
函数getLocation(){
if(导航器.地理位置){
navigator.geolocation.getCurrentPosition(getCoordinates);
}否则{
document.getElementById(“asdfsdasdaf”).innerHTML=“此浏览器不支持地理位置。”;
}
}
函数getCoordinates(位置){
currentLatitude=位置坐标纬度;
当前经度=position.coords.longitude;
}
var xhr=new XMLHttpRequest();
open(“POST”,“rsvpButton.php”,true);
var formData=new formData();
formData.append(“userID”,100100);
xhr.send(formData);
xhr.onload=函数(){
getLocation();
警报(“1”+当前经度+”和“+当前纬度”);
//显示页面的其他一些代码
//...
}
我以为我把那两个VAR放错地方了,但试了几次还是不行。需要帮忙吗?提前谢谢


更新:我试图将底部代码放入回调函数,但整个页面消失。

getCurrentPosition
接受回调,因为它是异步的。请在此处阅读更多信息:

尝试将
警报
放在回调中:

<script type="text/javascript">
var currentLatitude;
var currentLongitude;

function getLocation(callback) {
    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(callback);
    } else { 
        document.getElementById("asdfsdafsdaf").innerHTML = "Geolocation is not supported by this browser.";
    }
}

var xhr = new XMLHttpRequest();
xhr.open("POST", "rsvpButton.php", true);
var formData = new FormData();
    formData.append("userID", 100100);
xhr.send(formData);

xhr.onload=function(){
    getLocation(function (position) {
        var currentLatitude = position.coords.latitude;
        var currentLongitude = position.coords.longitude;   

        alert("1"+currentLongitude+" and "+currentLatitude);
        //some other codes to display the page
        //...
    });

}
</script>

无功电流纬度;
经度;
函数getLocation(回调){
if(导航器.地理位置){
navigator.geolocation.getCurrentPosition(回调);
}否则{
document.getElementById(“asdfsdasdaf”).innerHTML=“此浏览器不支持地理位置。”;
}
}
var xhr=new XMLHttpRequest();
open(“POST”,“rsvpButton.php”,true);
var formData=new formData();
formData.append(“userID”,100100);
xhr.send(formData);
xhr.onload=函数(){
getLocation(函数(位置){
var currentLatitude=位置坐标纬度;
var currentLength=position.coords.longitude;
警报(“1”+当前经度+”和“+当前纬度”);
//显示页面的其他一些代码
//...
});
}
试试这个:

var currentLatitude;
var currentLongitude;

function getLocation() {
    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(getCoordinates);
    } else { 
        alert("4");
    }
}

function getCoordinates(position) {
    currentLatitude = position.coords.latitude;
    currentLongitude = position.coords.longitude; 

    // now latitude and longitude are available
    alert(currentLongitude+" and "+currentLatitude);
}

// actually call the function getLocation
getLocation();


这是基于您的代码的

,如果我想在GetCoordinations(position)功能之外的其他地方使用这两个变量,该怎么办?如果需要,我可以更新我的问题,以提供到目前为止我所拥有的全部代码。它们仍将在全局范围内可用,但是只有在调用getCoordinates回调后才可用。您可以使用一些有助于管理异步代码的库,比如我编辑并更新了我的问题。我确实需要在xhr.onload=function()中使用坐标。请告知。根据您的代码,如果我想在GetCoordinations(位置)功能之外的其他地方使用这两个变量,该怎么办?如果需要,我可以更新我的问题,以提供到目前为止我拥有的全部代码。只要变量可用,您就必须等待。您可以使用promise库编写如下内容:
getLocation().then(函数(coords){//使用coords做一些事情})看看“它仍然不工作”是什么意思?浏览器中有Javascript错误吗?向我们显示错误消息。不是重复的
getCurrentPosition
肯定会在某些平台上使用与不正确使用异步函数毫无关系的权限,做出一些奇怪的事情。