0

我一直在阅读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);
                                    });
                                }

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

我想为要添加的标记创建一个带有 contentString 的信息窗口。

4

2 回答 2

1

是的,我想通了

        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 是函数的构造函数,因此它不是由变量引用创建的,而是由实际的迭代器本身创建的。天哪,我不知道。伊皮

于 2012-11-16T13:08:23.180 回答
1

我假设你会在 中看到最后一次迭代的结果,这在这篇博alert文中有详细解释。

在您的情况下,我会切换执行顺序:您有一个外部循环,它调用geocoder.geocode

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

带功能doAlert

var doAlert = function(element) {
            var contentString = '<table>';

            for(b in element.other_fields){
                var current = element.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'>";

            alert(contentString);
      }
于 2012-11-16T12:25:49.303 回答