数组中的Ovewriting标记

时间:2015-02-27 19:07:52

标签: javascript jquery google-maps google-maps-api-3

我试图用infowindows显示所有标记,然后用另一次点击隐藏它们,但我只能访问最后一个入口,我不知道该怎么做才能在循环中停止覆盖。 我尝试在循环后创建一个全局数组markersArray而不是markersArray.push(marker),但没有成功。 我认为这是一个愚蠢的错误,但我正在查看代码并且不知道它在哪里。

JSFiddle

2 个答案:

答案 0 :(得分:0)

您需要跟踪已放置标记的数组,然后重置该标记,然后将标记值设置为undefined,以便正确切换点击。我用工作的例子分配了小提琴。 http://jsfiddle.net/xo5pw6q3/

var marker;
var infowindow = new google.maps.InfoWindow({});

function buttonCarros(buttonCarrosDiv, map_canvas) {

    // Control border
    var buttonCarrosUI = document.createElement('div');
    buttonCarrosUI.style.backgroundColor = '#fff';
    buttonCarrosUI.style.border = '1px solid #717B87';
    buttonCarrosUI.style.borderRadius = '3px';
    buttonCarrosUI.style.boxShadow = '0 2px 6px rgba(0,0,0,.3)';
    buttonCarrosUI.style.cursor = 'pointer';
    buttonCarrosUI.style.marginBottom = '5px';
    buttonCarrosUI.style.textAlign = 'center';
    buttonCarrosUI.title = 'Clique para ver os carros';
    buttonCarrosDiv.appendChild(buttonCarrosUI);


    // Control interior
    var buttonCarrosText = document.createElement('div');
    buttonCarrosUI.style.color = 'rgb(25,25,25)';
    buttonCarrosUI.style.fontFamily = 'Roboto,Arial,sans-serif';
    buttonCarrosUI.style.fontSize = '13px';
    buttonCarrosUI.style.lineHeight = '38px';
    buttonCarrosUI.style.paddingLeft = '5px';
    buttonCarrosUI.style.paddingRight = '5px';
    buttonCarrosUI.innerHTML = 'Carros';
    buttonCarrosUI.appendChild(buttonCarrosText);



    google.maps.event.addDomListener(buttonCarrosUI, 'click', function () {
        var data = JSON;
        var myLatLng;
        if (marker && marker.setMap) {
            $.each(mapMarkers, function (i, markerInfo) {
                var marker = mapMarkers[i];
                if (marker.getMap() != null) marker.setMap(null);
                else marker.setMap(map_canvas);
            });
            mapMarkers = [];
           marker = undefined;
        } else {
            $.each(data.markers, function (i, markerInfo) {
                myLatLng = new google.maps.LatLng(markerInfo.latitude, markerInfo.longitude);
                marker = new google.maps.Marker({
                    position: myLatLng,
                    map: map_canvas,
                    title: markerInfo.title
                });
                mapMarkers.push(marker);
                google.maps.event.addListener(marker, 'click', function () {
                    infowindow.setContent(marker.getTitle());
                    infowindow.open(map_canvas, marker);
                });
            });

        }
    });
}
var mapMarkers = [];
var JSON = {
    "markers": [{
        "longitude": "-46.763117",
            "latitude": "-23.540187",
            "title": "title1"
    },
                {
        "longitude": "41.763117",
            "latitude": "28.540187",
            "title": "title2"
    }
               ]
};

var map_canvas;
var line;
var santaCatarina = new google.maps.LatLng(-27.666885236556496, -51.15083105000002);

function initialize(url) {
    var myOptions = {
        zoom: 3,
        center: santaCatarina,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    map_canvas = new google.maps.Map(document.getElementById("map_canvas"), myOptions);

    var buttonCarrosDiv = document.createElement('div');
    var carros = new buttonCarros(buttonCarrosDiv, map_canvas);

    buttonCarrosDiv.index = 1;
    map_canvas.controls[google.maps.ControlPosition.TOP_RIGHT].push(buttonCarrosDiv);


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

答案 1 :(得分:0)

  1. 添加全局markersArray以保留对标记的引用
  2. 使用createMarker函数在点击侦听器的标记上保持函数关闭:
  3. function createMarker(markerInfo) {
        myLatLng = new google.maps.LatLng(markerInfo.latitude, markerInfo.longitude);
        var marker = new google.maps.Marker({
            position: myLatLng,
            map: map_canvas,
            title: markerInfo.title
        });
        google.maps.event.addListener(marker, 'click', function () {
            infowindow.setContent(marker.getTitle());
            infowindow.open(map_canvas, marker);
        });
        markersArray.push(marker);
    }
    

    这样称呼:

    google.maps.event.addDomListener(buttonCarrosUI, 'click', function () {
        var data = JSON;
        var myLatLng;
        if (markersArray && markersArray[0] && markersArray[0].setMap) {
            for (var i = 0; i < markersArray.length; i++) {
                if (markersArray[i].getMap() != null) {
                    markersArray[i].setMap(null);
                } else {
                    markersArray[i].setMap(map_canvas);
                }
            }
        } else {
            $.each(data.markers, function (i, markerInfo) {
                createMarker(markerInfo);
            });
    
        }
    });
    

    working fiddle

    (从您的previous question更新)