如何在Javascript中将值传递给另一个函数?

时间:2011-11-08 16:09:49

标签: javascript jquery google-maps

我正在尝试获取Google地图address_components并且只设法执行以下操作:

  $(function() {
    $("#spot_address").autocomplete({
      //This bit uses the geocoder to fetch address values
      source: function(request, response) {
        geocoder.geocode( {'address': request.term }, function(results, status) {
          for (var i = 0; i < results[0].address_components.length; i++)
          {
              var addr = results[0].address_components[i];
              if (addr.types[0] == 'country') 
                  country: addr.long_name;
          }
          response($.map(results, function(item) {
            return {
              label:  item.formatted_address,
              value: item.formatted_address,
              latitude: item.geometry.location.lat(),
              longitude: item.geometry.location.lng()
            }
          }));
        })
      },

  //This bit is executed upon selection of an address

  select: function(event, ui) {
    alert(ui.country);
    $("#spot_lat").val(ui.item.latitude);
    $("#spot_lng").val(ui.item.longitude);
    var location = new google.maps.LatLng(ui.item.latitude, ui.item.longitude);
    marker.setPosition(location);
    map.setCenter(location);
  }
});

});

在这里,我试图在从自动完成列表中选择项目时测试alert(ui.country)。但是,我只获得了undefined。它没有通过。我做错了什么?

感谢。

3 个答案:

答案 0 :(得分:0)

检查属性ui.country是否存在FireBug或Chrome Inspect Scripts

如果属性国家/地区存在项目,则只需:

response($.map(results, function(item) {
            return {
              label:  item.formatted_address,
              value: item.formatted_address,
              latitude: item.geometry.location.lat(),
              longitude: item.geometry.location.lng(),
              country: item.country, // Or other location of this property
            }
          }));

答案 1 :(得分:0)

添加到

      response($.map(results, function(item) {
        return {
          country: item.country

答案 2 :(得分:0)

最后在朋友的帮助下完成了它。

这是完整的代码:

$(function() {
    $("#address").autocomplete({
      //This bit uses the geocoder to fetch address values
      source: function(request, response) {
        geocoder.geocode( {'address': request.term }, function(results, status) {
      for (var i = 0; i < results[0].address_components.length; i++)
              {
                  var addr = results[0].address_components[i],
                      country1;
                  if (addr.types[0] == 'country') 
                      country1= addr.long_name;
              }
          response($.map(results, function(item) {
            return {
              label:  item.formatted_address,
              value: item.formatted_address,
              latitude: item.geometry.location.lat(),
              longitude: item.geometry.location.lng(),
              country: country1
            }
          }));
        })
      },
      //This bit is executed upon selection of an address
      select: function(event, ui) {
        alert(ui.item.country);
        $("#latitude").val(ui.item.latitude);
        $("#longitude").val(ui.item.longitude);
        var location = new google.maps.LatLng(ui.item.latitude, ui.item.longitude);
        marker.setPosition(location);
        map.setCenter(location);
      }
    });
  });