单击当前标记时删除其他标记

时间:2018-11-08 21:59:53

标签: javascript jquery ruby-on-rails google-maps

当我单击一个标记时,同时我希望其他标记将从护目镜地图中删除。就像我对infowindow所做的一样,当我单击一个标记时,只会打开其信息窗口。

var locations = [
        [
        "Location 1",
         "215 West Girard Avenue 19123",
        "39.9695601",
        "-75.1395161"
        ],
        [
        "Location 2",
        "5360 Old York Road 19141",
        "40.034038",
        "-75.145223"
        ],
        [
        "Location 3",
        "1350 W Girard Avenue 19123",
        "39.9713524",
        "-75.1590360"
        ]
        ];
gmarkers = [];

var map = new google.maps.Map(document.getElementById('map'), {
    zoom: 12,
    center: new google.maps.LatLng(39.9995601, -75.1395161),
    mapTypeId: google.maps.MapTypeId.ROADMAP
});

var infowindow = new google.maps.InfoWindow();


function createMarker(latlng, html) {
    var marker = new google.maps.Marker({
        position: latlng,
        map: map
    });

    google.maps.event.addListener(marker, 'click', function() {
        infowindow.setContent(html);
        infowindow.open(map, marker);
    });
    return marker;
}

for (var i = 0; i < locations.length; i++) {
 alert(locations[i][0]);
    gmarkers[locations[i][0]] =
    createMarker(new google.maps.LatLng(locations[i][2], locations[i][3]), locations[i][0] + "<br>" + locations[i][1]);
}

1 个答案:

答案 0 :(得分:0)

您可以将此代码放入for循环中!

for (var i = 0; i < locations.length; i++) {

        markerd[i] = new google.maps.Marker({
                position: mark_1,
                map: map,
                zIndex: i
        });
        google.maps.event.addListener(markerd[i], 'click', function() {
                        for (i = 0; i < markerd.length; i++) {
                            if (this.index != i){
                                markerd[i].setMap(null);
                                markerp[i].setMap(null);
                                line[i].setMap(null);
                            }
                        }
        });
}