简体   繁体   English

卡在javascript变量闭包中

[英]Stuck in javascript variables closures

I just wanted to assign position.coords.latitude and longitude to lat and lon vars but it seems that I am missing something because Console always says lat lon to be undefined. 我只是想为lat和lon vars分配position.coords.latitude和经度,但由于控制台始终说lat lon是未定义的,所以我似乎缺少了一些东西。

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

}

The problem is that when you define variables with var like you do at first, their values are undefined. 问题在于,当像一开始一样用var定义变量时,它们的值是不确定的。 Now when you call the getCurrentPosition function, it's probably asynchronous, which means that console.logs are called before you actually assign them any values. 现在,当您调用getCurrentPosition函数时,它可能是异步的,这意味着在实际为它们分配任何值之前,先调用console.logs Try the following change 尝试以下更改

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

}

Since you want to execute your actual code after you have the coords, here's how you could change your init function to accommodate asynchronous architecture. 由于您希望在拥有坐标之后执行实际的代码,因此可以通过以下方法更改init函数以适应异步体系结构。

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

Let's pretend your actual program begins when you execute a function called start with latitude and longitude. 让我们假设您的实际程序是在执行一个名为“以纬度和经度开始”的函数时开始的。 In that case, you would use the following to start your program: 在这种情况下,您将使用以下代码启动程序:

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

init(start);

I would imagine that navigator.geolocation.getCurrentPosition() is asynchronous, so it wont have executed by the time you try writing to the console. 我可以想象navigator.geolocation.getCurrentPosition()是异步的,因此在您尝试写入控制台时它不会执行。

Try moving you console writes into the callback function: 尝试将控制台写入到回调函数中:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM