-1

在这个小代码片段中,我遇到了一个小问题。问题是如果我在页面完全加载后关闭所有标记。当我再次单击标记时,所有标记上都会显示相同的详细信息。这是我的代码:

var map, infoBubble;

function onload() {

    var mapCenter = new google.maps.LatLng(13.0162476, 77.5073893);
    map = new google.maps.Map(document.getElementById('dvMap'), {
        zoom: 8,
        center: mapCenter,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    });

    for (i = 0; i < markers.length; i++) {
        //icons[i];
        //alert("undefined icons" + icons[i]);
        var data = markers[i]
        //var myLatlng = new google.maps.LatLng(13.4162691, 77.5064153);
        var myLatlng = new google.maps.LatLng(data.lat, data.lng);
        var marker = new google.maps.Marker({
            position: myLatlng,
            map: map,
            title: 'Par Id  ' + data.ParId,
            icon: 'Google Marker/In/' + data.title + '.png'

        });

        if (data.RANK == "1") {
            data.RANK = "Primary";
        } else {
            data.RANK = "Secondary";
        }
        // alert(data.IMAGEPATH);
        var contentString = '<div id="content" align="left" >' +
            '<h1>Parcel Details</h1>' +
            '<h3>Parcel Id:' + data.ParId + '</h3>' +
            '<h3>Rank:' + data.RANK + '</h3>' +
            '<h3>Captured by:' + data.Capturedby + '</h3>' +
            '<h3>Captured Date:' + data.Capturedate + '</h3>' +
            '</div>';

        infoBubble = new InfoBubble({
            maxWidth: 300,
            maxHeight: 200,
            backgroundColor: 'rgb(236,255,255)'
        });

        infoBubble.open(map, marker);

        var div = document.createElement('DIV');
        div.align = "left";
        div.innerHTML = '<IMG BORDER="0" ALIGN="Left" width=97% height=200px alt="Pulpit rock1" SRC="' + data.IMAGEPATH + '"/>';

        infoBubble.addTab('Photo', div);
        infoBubble.addTab('Details', contentString);

        (function (marker, data) {
            google.maps.event.addListener(marker, 'click', function () {
                // if (!infoBubble.isOpen()) {
                infoBubble.open(map, marker);
                alert("Par Id:" + data.ParId);
                //alert("2");
                //}
                //infoWindow.open(map);
            });
            // alert(data.title);
            //google.maps.event.trigger(marker, 'click'); //stil

        }(marker, data));
    }
}

google.maps.event.addDomListener(window, 'load', onload);

4

1 回答 1

0

- 变量将在infoBubble循环内被覆盖。

解决方案:infoBubbleas 参数传递给添加click-listener 的函数:

    (function (marker, data , infoBubble) {
        google.maps.event.addListener(marker, 'click', function () {
            infoBubble.open(map, marker);
        });
    }(marker, data, infoBubble));
于 2015-01-31T08:20:31.757 回答