1

我仍然对 javascript 闭包和输入/输出变量有一些问题。

我正在使用 google maps 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)
  );

有什么建议吗?

编辑:

我的问题是如何将附加参数传递给地理编码器内部函数:

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

因为如果我这样做的话,我会弄乱原始参数(results_2 和 status_2)

4

1 回答 1

1

如果我对您的理解正确:

您在第一个示例中的问题是,当您到达带有注释的行时,第二个(最里面的)地理编码的回调函数(将字符串附加到html)将不会执行Finally, insert the 'html' variable value into my dom...

您正在有效地启动第二个地理编码请求,然后html在操作完成之前插入。


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

例如。

function(my_argument)
{
return(function(cb1,cb2) { ... });
}(my_argument_value);

然后你可以传入任何你想要的my_argument_value,最里面的代码(...)会看到它以及两个回调参数。

此函数的返回值是您作为回调传递给地理编码调用的值。

于 2011-02-23T22:10:20.070 回答