Javascript 基于id切换google地图标记

Javascript 基于id切换google地图标记,javascript,php,google-maps,laravel,google-maps-api-3,Javascript,Php,Google Maps,Laravel,Google Maps Api 3,我有一张地图,通过循环的显示标记 function map_view_initialize() { var properties = <?php echo is_array($properties) ? json_encode($properties) : $properties; ?>; var index; var mapOptions = { center: {lat: 32.752601, lng: 9.

我有一张地图,通过循环的
显示标记

function map_view_initialize() {
        var properties = <?php echo is_array($properties) ? json_encode($properties) : $properties; ?>;
        var index;
        var mapOptions = {
            center: {lat: 32.752601, lng: 9.801254},
            zoom: 12,
        };

        var map = new google.maps.Map(document.getElementById('map_view_canvas'),
                mapOptions);
        infowindow = new google.maps.InfoWindow();
        for (var index = 0; index < properties.length; index++) {

            var latlng = new google.maps.LatLng(properties[index].property_latitude, properties[index].property_longitude);
            var agent = properties[index].agent_firstname + ' ' + properties[index].agent_lastname;
            var propertyName = properties[index].property_name;
            var agentId = properties[index].agent_id;
            var propertyLocation = properties[index].location_name;
            var owner = properties[index].owner_firstname + ' ' + properties[index].owner_lastname;
            var ownerTel = properties[index].owner_telephone_number;
            var markerContent = "<div><h4>" + propertyName + "</h4>\n\
            <h5>Location: " + propertyLocation + "</h5>\n\
            <p>" + owner + " " + ownerTel + "</p>\n\
            </div>";

            var marker = new MarkerWithLabel({
                agent:agentId,
                position: latlng,
                map: map,
                labelContent: agent,
                labelAnchor: new google.maps.Point(22, 0),
                labelClass: "labels",
                labelStyle: {opacity: 0.75}
            });

            marker.content = markerContent;

            var infoWindow = new google.maps.InfoWindow();
            google.maps.event.addListener(marker, 'click', function () {
                infoWindow.setContent(this.content);
                infoWindow.open(this.getMap(), this);
            });
        }
 }
当我按下其中一个按钮时,此功能将运行

function filterMarkers(agentId){
                var element = document.getElementById(agentId);
                var cls = hasClass(element,'notActive');

                if(!cls){
                    element.classList.add("notActive");
                    element.style.opacity = 0.5;
                }
                else if(cls){
                    element.classList.remove("notActive");
                    element.style.opacity = 1;  
                }
}

我想使用我的按钮来切换每个标记的可见性(参见第二段代码)。例如,当我用
id=1按下按钮时,我需要隐藏/显示
agentId
属性等于1的标记。

您需要与agentId eg(在全局区域中定义)关联的标记索引:

在循环中,必须为每个agentId设置关联的标记

markerInded[agentId]=marker
对于切换标记,您需要隐藏和显示标记的函数,例如

toggleMarkerOff(agentId){
  markerIndex[agentId].setMap(null); 
}
toggleMarkerOff(agentId){
  markerIndex[agentId].setMap(map); 
}

然后,您可以在相关元素事件中调用prpoer函数

您需要与agentId eg(在全局区域中定义)关联的标记的索引:

在循环中,必须为每个agentId设置关联的标记

markerInded[agentId]=marker
对于切换标记,您需要隐藏和显示标记的函数,例如

toggleMarkerOff(agentId){
  markerIndex[agentId].setMap(null); 
}
toggleMarkerOff(agentId){
  markerIndex[agentId].setMap(map); 
}

然后,您可以在相关元素事件中调用prpoer函数

您需要与agentId eg(在全局区域中定义)关联的标记的索引:

在循环中,必须为每个agentId设置关联的标记

markerInded[agentId]=marker
对于切换标记,您需要隐藏和显示标记的函数,例如

toggleMarkerOff(agentId){
  markerIndex[agentId].setMap(null); 
}
toggleMarkerOff(agentId){
  markerIndex[agentId].setMap(map); 
}

然后,您可以在相关元素事件中调用prpoer函数

您需要与agentId eg(在全局区域中定义)关联的标记的索引:

在循环中,必须为每个agentId设置关联的标记

markerInded[agentId]=marker
对于切换标记,您需要隐藏和显示标记的函数,例如

toggleMarkerOff(agentId){
  markerIndex[agentId].setMap(null); 
}
toggleMarkerOff(agentId){
  markerIndex[agentId].setMap(map); 
}

然后您可以在相关元素事件中调用prpoer函数

您可以将其联机吗?fiddle,codepen…你能上网吗?fiddle,codepen…你能上网吗?fiddle,codepen…你能上网吗?小提琴,密码笔。。。