【发布时间】:2011-12-26 07:31:13
【问题描述】:
我有以下代码解析自动完成列表时解析@ 987654321:
$('#spot_address').autocomplete({
// This bit uses the geocoder to fetch address values
source: function(request, response) {
geocoder.geocode( {'address': request.term }, function(results, status) {
// Get address_components
for (var i = 0; i < results[0].address_components.length; i++)
{
var addr = results[0].address_components[i];
var getCountry;
if (addr.types[0] == 'country')
getCountry = 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: getCountry
}
}));
})
},
// This bit is executed upon selection of an address
select: function(event, ui) {
// Get values
$('#spot_country').val(ui.item.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);
},
// Changes the current marker when autocomplete dropdown list is focused
focus: function(event, ui) {
var location = new google.maps.LatLng(ui.item.latitude, ui.item.longitude);
marker.setPosition(location);
map.setCenter(location);
}
});
但是,上面的代码不起作用,并且在解析国家时,无论如何只解析自动完成的第一个结果,这对于数组results[0]很重要,因为它只获取第一个结果。
我尝试将其移至select 函数,但select 中的ui 仅包含formatted_address、longitude 和latitude,但不包含address_components。
在选择自动完成列表项时,我必须做的是什么来发送正确的country
非常感谢。
【问题讨论】:
标签: javascript jquery google-maps google-maps-api-3