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);
                                    });
                                }

alert(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 に答える