2012-01-31 116 views
0

我只是想分配position.coords.latitude和经度来经纬度和经度,但似乎我失去了一些东西,因为控制台总是说纬度经纬度未定义。卡住的JavaScript变量关闭

function init() 
{ 
    var lat; 
    var lon; 

    if (navigator.geolocation) { 

     navigator.geolocation.getCurrentPosition(function(position) 
     { 
      lat = position.coords.latitude; 
      lon = position.coords.longitude;  
     }); 
    } 

    console.log(lat); 
    console.log(lon); 

} 
+0

是getCurrentPosition执行异步? (setTimeout/setInterval/AJAX) – TimWolla 2012-01-31 12:11:32

+0

是异步。对不起,因为noob,但我怎么能让它同步? – 2012-01-31 12:15:37

+0

当没有参数时:除修改脚本外没有办法。但可能这是第三方库,对吧? – TimWolla 2012-01-31 12:20:51

回答

2

的问题是,当你喜欢你先做定义与var变量,它们的值是不确定的。现在,当您调用getCurrentPosition函数时,它可能是异步的,这意味着console.logs在您为其分配任何值之前被调用。请尝试以下变化

function init() 
{ 
    var lat; 
    var lon; 

    if (navigator.geolocation) { 

     navigator.geolocation.getCurrentPosition(function(position) 
     { 
      lat = position.coords.latitude; 
      lon = position.coords.longitude; 

      // Log them in the callback function 
      console.log(lat); 
      console.log(lon); 
     }); 
    } 

} 

既然你想你有COORDS后执行实际代码,这里是你如何可以改变你的初始化函数,以适应异步架构。

// New function argument callbackFn - the function to call after 
// coords are loaded 
function init(callbackFn) 
{ 
    // Removed local variables, no need for them here 

    if (navigator.geolocation) { 

     navigator.geolocation.getCurrentPosition(function(position) 
     { 
      var lat = position.coords.latitude; 
      var lon = position.coords.longitude; 

      // This will be logged after the async call completes, probably after 
      // the console.log call below 
      console.log("finished async call"); 

      // Execute the callback with coords 
      callbackFn(lat, lon); 
     }); 
    } 

    // This will be called as soon as JS steps over previous lines, 
    // but before the asynchronous request has completed 
    console.log("finished init"); 
} 

让我们假装你的实际程序开始于你执行一个叫做经度和纬度开始的函数。在这种情况下,你可以使用下面的启动程序:

function start(latitude, longitude) { 
    // ... SNIP ... DO SOMETHING COOL 
} 

init(start); 
+0

是的函数getCurrentPosition是异步的,但我想要实现的是将坐标分配给纬度和经度变量,以便之后使用它们 – 2012-01-31 12:14:18

+0

@AssadUllah异步架构是Javascript的基本思想之一,试图强制它进入同步执行是一个糟糕的主意,所以你总是必须使用回调函数。用例子更新了答案,你可以做到这一点。 – zatatatata 2012-01-31 12:33:04

+0

@AssadUllah如果你愿意,你也可以通过将'var lat'改成'window.lat'(或者只是'lat',但我建议使用'window'来防止混淆)将变量写成全局变量。请记住,在异步回调已经返回之前,您仍然无法使用它们,所以您应该继续沿着我展示的方向行事。 – zatatatata 2012-01-31 12:37:55

1

我会想象navigator.geolocation.getCurrentPosition()是异步的,所以它不会有通过你尝试写到控制台时执行。

尝试移动你安慰写入回调函数:

function init() 
{ 
    if (navigator.geolocation) { 

     navigator.geolocation.getCurrentPosition(function(position) 
     { 
      var lat = position.coords.latitude; 
      var lon = position.coords.longitude; 

      console.log(lat); 
      console.log(lon); 
     }); 
    } 
} 
+0

是的函数getCurrentPosition是异步的,但我想要实现的是将坐标分配给纬度和经度变量,所以我可以在以后使用它们 – 2012-01-31 12:16:57

+0

这很好,只需在全局范围声明lat和lon,并将它们的值分配给回电话。您可能需要从回调方法引导任何进一步的逻辑 - 也许让它调用一个名为handleGeolocation()或类似的方法。 – 2012-01-31 12:23:33