Javascript 脚本不';不允许geolocation.getCurrentPosition完成其回调函数
我需要将用户的当前位置打包到一个对象中,并将其发送到一个PHP脚本。所以我现在有以下几点 定位对象Javascript 脚本不';不允许geolocation.getCurrentPosition完成其回调函数,javascript,jquery,geolocation,Javascript,Jquery,Geolocation,我需要将用户的当前位置打包到一个对象中,并将其发送到一个PHP脚本。所以我现在有以下几点 定位对象 function position(lat, lon) { this.latitude = lat; this.longitude = lon; this.setlat = function(lat) { this.latitude = lat; }; this.setlon = function(lon) { this.
function position(lat, lon) {
this.latitude = lat;
this.longitude = lon;
this.setlat = function(lat) {
this.latitude = lat;
};
this.setlon = function(lon) {
this.longitude = lon;
};
this.print = function() {
alert(this.longitude + ", " + this.latitude);
};
}
控制流
var origin = new position(0, 0);
$(document).ready(function() {
getLocation();
origin.print();
});
功能
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(setLocation, showError, {timeout:60000});
} else {
alert("GeoLocation is not supported on this browser or device.");
return null;
}
}
function setLocation(position) {
alert('callback success!');
origin.setlat(position.coords.latitude);
origin.setlon(position.coords.longitude);
}
function showError(error) {
switch(error.code) {
case error.PERMISSION_DENIED:
console.log("User denied the request for Geolocation.");
break;
case error.POSITION_UNAVAILABLE:
console.log("Location information is unavailable.");
break;
case error.TIMEOUT:
console.log("The request to get user location timed out.");
break;
case error.UNKNOWN_ERROR:
console.log("An unknown error occurred.");
break;
}
}
问题是,在调用setLocation
回调之前,主控制流会继续,并忽略我请求它为我获取一些东西的事实。我调用getLocation,得到一个“0,0”的警报,因为origin最初设置为“0,0”。因此,出于某种原因,我的setLocation
回调函数在程序中的其他所有操作完成之前不会被调用
我已经查阅了文档,但它相当简单,没有太多的内容,所以很难确定为什么会发生这种情况
我知道很多人都在努力解决这个问题,似乎有很多类似于“看看这个主题”的东西,但所有这些主题都指向的代码实际上与我自己的代码相同,但格式有点不同
需要注意的是,我确实需要将这个位置数据封装在一个全局变量中,因为我将把我的位置与页面加载上的几十个其他位置进行比较
尝试的解决方案我已尝试将我的
navigator.geolocation.getCurrentPosition()
限制为一个while循环,该循环基于回调函数的完成而退出,但这总是导致一个无休止的循环
-我尝试在PositionOptions
中使用maximumAge
和timeout
值,以便让函数有一段时间完成,但这同样只会导致在所有其他预定函数调用完成后setLocation
完成
-我尝试使用两个独立的$(document).ready(function(){})
以分段getLocation()
和origin.print()
函数
-我尝试使用watchPosition
而不是getCurrentPosition.
最后,我真的需要澄清一下为什么getCurrentPosition
中的回调函数在其他所有操作完成之前都没有完成。您可以替换以下内容:
var origin = new position(0, 0);
$(document).ready(function() {
getLocation();
origin.print();
});
用这个
var origin = new position(0, 0);
$(document).ready(function() {
getLocation();
});
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(setLocation, showError, {timeout:60000});
} else {
alert("GeoLocation is not supported on this browser or device.");
return null;
}
}
// this is the callback
function setLocation(position) {
alert('callback success!');
origin.setlat(position.coords.latitude);
origin.setlon(position.coords.longitude);
// this is a new function that makes stuff
processLocation();
}
// do your stuff here
function processLocation(){
origin.print();
// do your stuff here like comparing to other locations or sending data
if (origin.latitude == something) blah...
}
基本上,在函数中执行逻辑,并在setLocation回调函数中调用它,虽然它与在回调函数中为
getCurrentPostion
执行逻辑似乎没有什么不同,但Javascript中有一些技术允许您以这种方式处理异步性,从而大大清理了代码。下面我将演示的是使用jQuery获取您当前的位置,并将其打包成一个可供其他函数使用的变量
function getLocation() {
var deferred = $.Deferred();
setTimeout(function() {
navigator.geolocation.getCurrentPosition(function(pos) {
origin = new position(pos.coords.latitude, pos.coords.longitude);
deferred.resolve();
});
}, 2000);
// 2.00 seconds
return deferred.promise();
}
需要注意的是,我们正在实例化源文件
,并在同一范围内使用deferred.resolve()
解析延迟对象
现在我们要做的是调用getLocation()
,但是添加了一些语法来指定处理getLocation()
中解析的对象的函数必须等到完成后才能访问它们
getLocation().done( function() {
origin.print();
//do other stuff with origin
});
这将成功打印您的坐标,您可以自由地使用getCurrentPosition获取的数据执行任何其他操作,如使用Google Maps。您应该将警报移动到回调函数setLocation中,在该函数中,您可以将对象发送到PHP,请记住,javascript是异步的。不幸的是,由于我将我的位置与几十个其他位置进行比较,所以我有点受限于使用一个全局变量。所以这样做没有什么意义。实现我描述的功能应该不会那么难。虽然我可以编写脚本,使我的所有逻辑都在这个特定回调函数中完成,但我觉得它不负责任,难以扩展。因此,重要的是我能够在
getLocation()
我明白后进行函数调用,不幸的是,据我所知,javascript定义为异步的,无法强制javascript调用等待完成,您可以更改代码的结构以使其更美观,但是你不能摆脱回调函数我找到了一个叫做承诺的东西,但我不能完全让它解决问题。我想我已经很接近了。