Как передать значение другой функции в Javascript? - PullRequest
1 голос
/ 08 ноября 2011

Я пытаюсь получить Google Maps 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 голосов
/ 08 ноября 2011

Добавить к

      response($.map(results, function(item) {
        return {
          country: item.country
0 голосов
/ 09 ноября 2011

Наконец-то все сделано с помощью друга.

Вот полный код:

$(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);
      }
    });
  });
0 голосов
/ 08 ноября 2011

Проверьте, существует ли свойство ui.country FireBug или Chrome Inspect Scripts

И если страна свойства существует в item , тогда просто:

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
            }
          }));
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...