gpt4 book ai didi

javascript - 如何将对象传递给此js函数?

转载 作者:行者123 更新时间:2023-11-30 18:19:53 25 4
gpt4 key购买 nike

代码:

function setMaps() {
var geocoder = new google.maps.Geocoder();
var result = "";

$('.map_canvas').each(function(){

geocoder.geocode( {
'address': $(this).attr('address'),
'region': 'de'
}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
result += results[0].geometry.location.lng()+",";
result += results[0].geometry.location.lat();
} else {
result = "Unable to find address: " + status;
}
$(this).gmap({ 'center': result });
});
});
}

此方法应在一页上显示多个 map 。

HTML:

<div class="map_canvas" address="Berlin, Zoo">

</div>

问题是 $(this).gmap({ 'center': result }); 不起作用:

Uncaught TypeError: Cannot set property 'position' of undefined

知道如何将 map_canvas 对象传递给回调函数吗?

最佳答案

试试这个:

function setMaps() {
var geocoder = new google.maps.Geocoder();
var result = "";

$('.map_canvas').each(function(){
var _this = $(this); // <------ here
geocoder.geocode( {
'address': $(this).attr('address'),
'region': 'de'
}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
result += results[0].geometry.location.lng()+",";
result += results[0].geometry.location.lat();
} else {
result = "Unable to find address: " + status;
}
_this.gmap({ 'center': result }); // <---- and here
});
});
}

关于javascript - 如何将对象传递给此js函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12338580/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com