![](/img/trans.png)
[英]how to pass JavaScript variable value to another JavaScript function?
[英]How to pass value to another function in Javascript?
我正在尝试获取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)
时测试alert(ui.country)
。 但是,我只是变得undefined
。 没有通过 我做错了什么?
谢谢。
检查属性ui.country是否存在FireBug或Chrome检查脚本
如果项目中存在财产国家,则只需:
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
}
}));
添加
response($.map(results, function(item) {
return {
country: item.country
终于在朋友的帮助下完成了。
这是完整的代码:
$(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);
}
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.