使用google geocoder的Javascript闭包

使用google geocoder的Javascript闭包,javascript,closures,geocoding,google-geocoder,Javascript,Closures,Geocoding,Google Geocoder,我在javascript闭包和输入/输出变量方面仍然存在一些问题 我在玩谷歌地图api是为了一个无利可图的项目:用户将把标记放在gmap中,我必须在数据库中保存位置(带坐标) 当我需要进行第二次地理编码以获得一个位置的lat和lng的唯一对时,问题就来了:假设两个用户将标记放置在同一个城镇但不同的地方,我不想在数据库中使用不同的坐标重复使用同一个位置 我知道我可以在用户选择地点后进行第二次地理编码,但我想知道我在这里错了什么: // First geocoding, take the marke

我在javascript闭包和输入/输出变量方面仍然存在一些问题

我在玩谷歌地图api是为了一个无利可图的项目:用户将把标记放在gmap中,我必须在数据库中保存位置(带坐标)

当我需要进行第二次地理编码以获得一个位置的lat和lng的唯一对时,问题就来了:假设两个用户将标记放置在同一个城镇但不同的地方,我不想在数据库中使用不同的坐标重复使用同一个位置

我知道我可以在用户选择地点后进行第二次地理编码,但我想知道我在这里错了什么:

// First geocoding, take the marker coords to get locality.
geocoder.geocode(
  {
    'latLng': new google.maps.LatLng($("#lat").val(), $("#lng").val()),
    'language': 'it'
  },
  function(results_1, status_1){
    // initialize the html var inside this closure
    var html = '';
    if(status_1 == google.maps.GeocoderStatus.OK)
    {
      // do stuff here
      for(i = 0, geolen = results_1[0].address_components.length; i != geolen)
      {
        // Second type of geocoding: for each location from the first geocoding,
        // i want to have a unique [lat,lan]
        geocoder.geocode(
          {
            'address': results_1[0].address_components[i].long_name
          },
          function(results_2, status_2){
            // Here come the problem. I need to have the long_name here, and
            // 'html' var should increment.
            coords = results_2[0].geometry.location.toUrlValue();
            html += 'some html to let the user choose the locality';
          }
        );
      }
      // Finally, insert the 'html' variable value into my dom... 
      //but it never gets updated!
    }
    else
    {
      alert("Error from google geocoder:" + status_1)
    }
  }
);
我试着:

  // Second type of geocoding: for each location from the first geocoding, i want
  // to have a unique [lat,lan]
  geocoder.geocode(
    {
      'address': results_1[0].address_components[i].long_name
    },
    (function(results_2, status_2, long_name){
      // But in this way i'll never get results_2 or status_2, well, results_2 
      // get long_name value, status_2 and long_name is undefined.
      // However, html var is correctly updated.
      coords = results_2[0].geometry.location.toUrlValue();
      html += 'some html to let the user choose the locality';
    })(results_1[0].address_components[i].long_name)
  );
以及:

  // Second type of geocoding: for each location from the first geocoding, i want to have
  // a unique [lat,lan]
  geocoder.geocode(
    {
      'address': results_1[0].address_components[i].long_name
    },
    (function(results_2, status_2, long_name){
      // But i get an obvious "results_2 is not defined" error (same for status_2).
      coords = results_2[0].geometry.location.toUrlValue();
      html += 'some html to let the user choose the locality, that can be more than one';
    })(results_2, status_2, results_1[0].address_components[i].long_name)
  );
有什么建议吗

编辑:

我的问题是如何将附加参数传递给geocoder内部函数:

function(results_2, status_2, long_name){
    //[...]
}

因为如果我用clousure这样做,我会弄乱原始参数(结果和状态)

如果我理解正确:

第一个示例中的问题是,当您到达带有注释的行时,第二个(最内层)地理代码的回调函数(将字符串附加到
html
)将不会执行。最后,将“html”变量值插入我的dom…

您实际上是在启动第二个地理编码请求,然后在操作完成之前插入
html


关于向回调传递额外参数,您始终可以创建一个创建并返回函数的函数:

function(results_2, status_2, long_name){
    //[...]
}
例如


函数(我的参数)
{
返回(函数(cb1,cb2){…});
}(我的参数值)

然后,您可以为
my_argument\u value
传入您想要的任何内容,最里面的代码(
)将看到它以及两个回调参数


此函数的返回值是作为对geocode调用的回调传递的值。

如果我理解正确:

第一个示例中的问题是,当您到达带有注释的行时,第二个(最内层)地理代码的回调函数(将字符串附加到
html
)将不会执行。最后,将“html”变量值插入我的dom…

您实际上是在启动第二个地理编码请求,然后在操作完成之前插入
html


关于向回调传递额外参数,您始终可以创建一个创建并返回函数的函数:

function(results_2, status_2, long_name){
    //[...]
}
例如


函数(我的参数)
{
返回(函数(cb1,cb2){…});
}(我的参数值)

然后,您可以为
my_argument\u value
传入您想要的任何内容,最里面的代码(
)将看到它以及两个回调参数


此函数的返回值是作为对geocode调用的回调传递的值。

首先想到的是确保使用console.log调用第二组回调。此外,即使您正在按顺序运行第二组地理代码,也不能保证函数回调会按顺序运行。如果附加到html变量起作用,它可能会出现问题。只是想一想。是的,我在发布代码之前删除了所有的console.log。不管怎样,你说的订单问题是什么意思?我知道请求是异步的,但是(我猜)第二个地理代码进入第一个,所以它不应该在第一个之前执行。我搞错了吗?我想到的第一件事是确保第二组回调甚至是使用console.log调用的。此外,即使您正在按顺序运行第二组地理代码,也不能保证函数回调会按顺序运行。如果附加到html变量起作用,它可能会出现问题。只是想一想。是的,我在发布代码之前删除了所有的console.log。不管怎样,你说的订单问题是什么意思?我知道请求是异步的,但是(我猜)第二个地理代码进入第一个,所以它不应该在第一个之前执行。我弄错了吗?不,最里面的地理代码执行正确,问题是让它填充它之外的
html
变量(我用clousure来实现);但是,问题是将
results\u 1[0]。address\u components[i]。long\u name
变量传递给它。确定-我编辑了答案。。。希望这就是您要寻找的。不,最内部的地理代码执行正确,问题是让它填充它外部的
html
变量(我使用clousure执行此操作);但是,问题是将
results\u 1[0]。address\u components[i]。long\u name
变量传递给它。确定-我编辑了答案。。。希望这就是你要找的。