Javascript 无法处理navigator.geolocation的异步特性
我正在使用Firefox3.6中的navigator.geolocation.getCurrentPosition(函数)api。当我反复尝试调用这个方法时,我发现它有时有效,有时无效。我认为问题在于它的异步回调特性。我可以看到回调函数在某个点被调用,但我的外部函数已经退出,因此我无法捕捉位置坐标的值 我对javascript非常陌生,所以我假设其他javascript程序员可能已经知道了如何处理它。请帮忙 编辑:下面是我正在使用的一段代码示例Javascript 无法处理navigator.geolocation的异步特性,javascript,geolocation,w3c,Javascript,Geolocation,W3c,我正在使用Firefox3.6中的navigator.geolocation.getCurrentPosition(函数)api。当我反复尝试调用这个方法时,我发现它有时有效,有时无效。我认为问题在于它的异步回调特性。我可以看到回调函数在某个点被调用,但我的外部函数已经退出,因此我无法捕捉位置坐标的值 我对javascript非常陌生,所以我假设其他javascript程序员可能已经知道了如何处理它。请帮忙 编辑:下面是我正在使用的一段代码示例 <script type="text/jav
<script type="text/javascript">
function getCurrentLocation() {
var currLocation;
if(navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
currLocation = new google.maps.LatLng(position.coords.latitude,position.coords.longitude);
});
}
return currLocation; // this returns undefined sometimes. I need help here
}
</script>
函数getCurrentLocation(){
风险价值定位;
if(导航器.地理位置){
navigator.geolocation.getCurrentPosition(函数(位置){
currLocation=new google.maps.LatLng(position.coords.latitude,position.coords.longitude);
});
}
return currLocation;//有时返回未定义的。我需要这里的帮助
}
编辑2:
谢谢大家的回答,我希望我可以选择所有的答案为“接受”,但不能这样做
现在我面临另一个问题。我每3秒调用navigator.geolocation.getCurrentPosition,但在10-15次回复后响应停止。有人知道吗
再次感谢是的,您对操作的回调性质有问题。您不能调用
getCurrentLocation()
函数并期望它同步返回。我甚至感到惊讶的是,它偶尔会起作用
在处理异步调用时,必须使用稍微不同的范例。您可能应该调用函数,plotCurrentLocation()
,并执行类似以下示例的操作:
function plotCurrentLocation(map) {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
var currLocation = new google.maps.LatLng(position.coords.latitude,position.coords.longitude);
// plot the currLocation on Google Maps, or handle accordingly:
new google.maps.Marker({ title: 'Current Location',
map: map,
position: currLocation });
map.setCenter(currLocation);
});
}
}
请注意传递给plotCurrentLocation()
函数的map
参数如何可用于中的内部函数。这之所以有效,是因为JavaScript具有
更新:
其他答案建议的回调方法是解决这个问题的另一种选择,它添加了另一层抽象。您试图使其同步,但它无法工作。如您所见,无法保证在函数返回时设置currLocation。您现在可能有以下情况:
var loc = getCurrentLocation();
//doSomethingWith loc
将您的功能更改为:
function getCurrentLocation(callback) {
if(navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
callback(new google.maps.LatLng(position.coords.latitude,
position.coords.longitude));
});
}
else {
throw new Error("Your browser does not support geolocation.");
}
}
和客户端代码,以:
getCurrentLocation(function(loc)
{
//doSomethingWith loc
});
更好地使用:
<script type="text/javascript">
function getCurrentLocation(callback) {
if(!navigator.geolocation) return;
navigator.geolocation.getCurrentPosition(function(position) {
var currLocation = new google.maps.LatLng(position.coords.latitude,position.coords.longitude);
callback(currLocation);
});
}
</script>
函数getCurrentLocation(回调){
如果(!navigator.geolocation)返回;
navigator.geolocation.getCurrentPosition(函数(位置){
var currLocation=new google.maps.LatLng(position.coords.latitude,position.coords.longitude);
回调(当前位置);
});
}
getCurrentLocation(函数(currLocMap){
//既然地图已经准备好了,就用它做点什么。。
});
您可以使用承诺:
var lat,lon;
var promise1 = new Promise(function(resolve, reject) {
navigator.geolocation.getCurrentPosition(function(pos){
lat = pos.coords.latitude
lon = pos.coords.longitude
resolve({lat,lon});
})
})
promise1.then(function(value) {
console.log(value.lat,value.lon)
});
您还可以为
getCurrentPosition
requestPosition() {
// additionally supplying options for fine tuning, if you want to
var options = {
enableHighAccuracy: true,
timeout: 5000, // time in millis when error callback will be invoked
maximumAge: 0, // max cached age of gps data, also in millis
};
return new Promise(function(resolve, reject) {
navigator.geolocation.getCurrentPosition(
pos => { resolve(pos); },
err => { reject (err); },
options);
});
}
这允许您选择如何处理它(async/await
,then()
,等等);比如说
async componentDidMount(){
position = await requestPosition();
}
那不是很漂亮吗:-)
(只是想添加到@AymanBakris的答案中,但在一条评论中,所有这些都会非常尴尬^^)嘿!谢谢你的回复。我解决了这个问题。现在我开始了解javascript的本质。我还有一个问题,一个人可以调用navigator.geolocation.getcurrentposition的次数是否有限制,因为我尝试每3秒调用一次,10或15次之后响应就会停止。有吗idea@Raja:不,我不知道有任何这样的限制。地理定位仍然是新事物。也许你可以发布另一个关于堆栈溢出的问题。很好的建议,应该被推上。
async componentDidMount(){
position = await requestPosition();
}