gpt4 book ai didi

javascript - Google Maps API V3 标记循环函数闭包

转载 作者:行者123 更新时间:2023-12-02 19:13:26 25 4
gpt4 key购买 nike

我一直在阅读 stackoverflow 中的其他条目,但我真的无法理解它是如何工作的:

            for(a in elements){
var address = elements[a].location;

var contentString = '<table>';

for(b in elements[a].other_fields){
var current = elements[a].other_fields[b];

switch(current.type){
case "text":
contentString += "<tr><td class = 'the_title'>" + current.label + ":</td><td class = 'the_value'>" + current.values[0].value + "</td></tr>";
break;
case "date":
if(!current.values[0].end){
var end_date_output_string = "";
}else{
var end_date_output_string = " -> " + current.values[0].end;
}
contentString += "<tr><td class = 'the_title'>" + current.label + ":</td><td class = 'the_value'>" + current.values[0].start + end_date_output_string + "</td></tr>";
break;
}
}

contentString += "</table>";
contentString += "<input type = 'button' onclick = 'window.open(\"" + elements[a].url + "\");' value = 'Open in Podio'>";

geocoder.geocode( { 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
map.setCenter(results[0].geometry.location);
alert(contentString);
addMarker(results[0].geometry.location);
} else {
alert("Geocode was not successful for the following reason: " + status);
}
});
//open_info_window(a, contentString);
}



function addMarker(location) {
var marker_copy = new google.maps.Marker({
position: location,
map: map
});

marker = marker_copy;

markersArray.push({
"marker": marker,
"info_window": null
});

alert("marker added: " + markersArray.lenght);
}

// Opens info windows
function open_info_window(key, contentString) {
var infowindow = new google.maps.InfoWindow({
content: contentString
});

alert(key);

markersArray[key].info_window = infowindow;

google.maps.event.addListener(markersArray[key].marker, "click", function() {
markersArray[key].info_window.open(map, markersArray[key].marker);
});
}

我尝试alert(contentString)的部分不会提醒我所期望的内容,它与闭包有关,但我实际上以前从未遇到过这样的代码。如果有机会你可以给我一点帮助。

我想使用我要添加的标记的 contentString 创建一个信息窗口。

最佳答案

是的,我明白了

        function codeAddress(){
for(a in elements){
(function(a){
var address = elements[a].location;

geocoder.geocode( { 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
map.setCenter(results[0].geometry.location);
addMarker(results[0].geometry.location);
alert(a);
var the_string = generate_string(elements, a);

infowindow[a] = new google.maps.InfoWindow({
content: the_string
});

google.maps.event.addListener(markersArray[a], "click", function() {
infowindow[a].open(map, markersArray[a]);
});
} else {
alert("Geocode was not successful for the following reason: " + status);
}
});
})(a);
}
}

诀窍就是这样做,我不知道它是如何工作的,但它确实有效:

(function(a){   
})(a);

我假设 a 是该函数的构造函数,因此它不是由变量引用创建的,而是由实际迭代器本身创建的。天啊我不知道。耶皮耶

关于javascript - Google Maps API V3 标记循环函数闭包,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13415371/

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