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

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

问题描述

我在firefox 3.6中使用navigator.geolocation.getCurrentPosition(function)api。当我尝试重复调用这个方法时,我发现它有时会起作用,有时却不起作用。我认为这个问题是因为它的异步回调性质。我可以看到回调函数在某个时候被调用,但是我的外部函数已经退出,所以我无法捕捉位置坐标的值。



我很漂亮新的JavaScript,所以我假设其他JavaScript代码可能已经想出了如何处理它。请帮助。



编辑:这是我使用的示例代码片段

 < script type =text / javascript> 
函数getCurrentLocation(){
var currLocation;
if(navigator.geolocation){
navigator.geolocation.getCurrentPosition(function(position){
currLocation = new google.maps.LatLng(position.coords.latitude,position.coords.longitude );
});
}
返回currLocation; //这有时会返回undefined。我需要帮助
}
< / script>

编辑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);

//在Google地图上绘制currLocation或进行相应处理:

new google.maps.Marker( {title:'当前位置',
map:map,
position:currLocation});

map.setCenter(currLocation);
});


请注意 map 参数传递给 plotCurrentLocation()函数可用于内部函数。这是有效的,因为JavaScript有关闭





UPDATE:

其他回答建议的回调方法是另一种方法是通过增加另一层抽象来解决这个问题。


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.

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.

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

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

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 ?

thanks again

解决方案

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.

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

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


UPDATE:

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天全站免登陆