调用javascript函数的正确方法
我试图通过添加国家/地区名称的提取来扩展此网站中的谷歌地图标记示例。
由于谷歌地图返回一个包含每个地址详细信息的数组,因此我需要迭代该数组以查找“国家/地区”等条目。由于我想重用这段代码,因此我创建了一个函数。
但我不确定如何正确调用这个函数(这里调用 find(components, item))。
我没有从 find 函数中得到任何返回。 如何从选择函数中正确调用查找函数? *是否还有其他错误导致我收到空退货?*
感谢您的想法和建议!
$(document).ready(function() {
initialize();
$(function() {
$("#address").autocomplete({
//This bit uses the geocoder to fetch address values
source: function(request, response) {
geocoder.geocode( {'address': request.term }, function(results, status) {
response($.map(results, function(item) {
return {
label: item.formatted_address,
value: item.formatted_address,
components: item.address_components,
latitude: item.geometry.location.lat(),
longitude: item.geometry.location.lng()
}
}));
})
},
// address_component selection
find: function(components, item) {
for(var j=0;j < components.length; j++){
for(var k=0; k < components[j].types.length; k++){
if(components[j].types[k] == "country"){
return components[j].long_name;
}
}
}
},
//This bit is executed upon selection of an address
select: function(event, ui) {
$("#latitude").val(ui.item.latitude);
$("#longitude").val(ui.item.longitude);
$("#country").val(this.find(ui.item.components, "country"));
var location = new google.maps.LatLng(ui.item.latitude, ui.item.longitude);
marker.setPosition(location);
map.setCenter(location);
}
});
});
I am trying to extend the google maps marker example from the this website by adding the extraction of the country name.
Since google maps returns an array with details to each address, I need to iterate over the array to find the entry e.g. for "country". Since I would like to reuse the piece of code, I create a function.
But I am not sure how to call this function (here call find(components, item)) correctly.
I do not get any return from the find function. How do I call the find function correctly from within the select function?
*Could there be a different mistake why I get an empty return?*
Thank you for your thoughts and suggestions!
$(document).ready(function() {
initialize();
$(function() {
$("#address").autocomplete({
//This bit uses the geocoder to fetch address values
source: function(request, response) {
geocoder.geocode( {'address': request.term }, function(results, status) {
response($.map(results, function(item) {
return {
label: item.formatted_address,
value: item.formatted_address,
components: item.address_components,
latitude: item.geometry.location.lat(),
longitude: item.geometry.location.lng()
}
}));
})
},
// address_component selection
find: function(components, item) {
for(var j=0;j < components.length; j++){
for(var k=0; k < components[j].types.length; k++){
if(components[j].types[k] == "country"){
return components[j].long_name;
}
}
}
},
//This bit is executed upon selection of an address
select: function(event, ui) {
$("#latitude").val(ui.item.latitude);
$("#longitude").val(ui.item.longitude);
$("#country").val(this.find(ui.item.components, "country"));
var location = new google.maps.LatLng(ui.item.latitude, ui.item.longitude);
marker.setPosition(location);
map.setCenter(location);
}
});
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
find()
函数在autocomplete
选项对象中定义。如果您想访问 find 函数,则必须在自动完成选项之外定义它或获取指向它的指针:
这是因为 jQuery 使用附加到侦听器的元素更改函数的上下文。
The
find()
function is defined in theautocomplete
options object.If you want to access your find function you'll have to define it outside the autocomplete options or to get a pointer to it :
This is because jQuery changes the context of functions with the element that is attached to the listener.