无法应对 navigator.geolocation 的异步特性 [英] unable to cope with the asynchronous nature of navigator.geolocation

查看:44
本文介绍了无法应对 navigator.geolocation 的异步特性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 firefox 3.6 中使用 navigator.geolocation.getCurrentPosition(function) api.当我尝试反复调用此方法时,我发现有时它有效,有时则无效.我认为问题在于它的异步回调性质.我可以看到回调函数在某个时候被调用,但我的外部函数已经退出,所以我无法捕获位置坐标的值.

I'm using the navigator.geolocation.getCurrentPosition(function) api in firefox 3.6. When i try to call this method repeatedly I see that sometimes it works and sometimes it doesn't. I figured that the problem is because of its asynchronous callback nature. I can see that the callback function is being called at some point but my outer function is already exiting so i cannot catch the values of the position coordinates.

我对 javascript 很陌生,所以我假设其他 javascript 编码人员可能已经想出了如何处理它.请帮忙.

I'm pretty new to javascript so i'm assuming other javascript coders might have already figured out how to deal with it. Please help.

这是我正在使用的一段示例代码

here's a sample piece of code i'm using

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

编辑 2:感谢大家的回答,我希望我可以将所有答案都选为已接受",但不能这样做.

Edit 2: Thanks everyone for answering, I wish i could choose all the answers as "accepted" but cannot do so.

现在我面临另一个问题.我每 3 秒调用一次 navigator.geolocation.getCurrentPosition,但响应在 10 - 15 次回复后停止.有人知道吗?

Now I'm facing another problem. I'm calling the navigator.geolocation.getCurrentPosition every 3 seconds, but the responses stop after 10 - 15 replies. Any one got any idea ?

再次感谢

推荐答案

是的,您对操作的回调性质有疑问.您不能调用 getCurrentLocation() 函数并期望它会同步返回.我什至很惊讶它偶尔会起作用.

Yes you have a problem with the callback nature of the operation. You cannot call the getCurrentLocation() function and expect that it will return synchronously. I am even surprised that it did work occasionally.

在处理异步调用时,您必须使用稍微不同的范例.您可能应该调用您的函数 plotCurrentLocation() 并执行类似以下示例的操作:

You have to use a slightly different paradigm when working with asynchronous calls. You should probably call your function, plotCurrentLocation() and do something like the following example:

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 有 闭包.

Note how the map parameter passed to the plotCurrentLocation() function is available to the inner functions within. This works because JavaScript has closures.

更新:

其他答案建议的回调方法是解决此问题的另一种选择,方法是添加另一层抽象.

The callback method suggested by the other answers is another option to tackle this, by adding another layer of abstraction.

这篇关于无法应对 navigator.geolocation 的异步特性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆