动态删除谷歌地图中的特定标记

时间:2015-12-08 20:00:14

标签: javascript google-maps

我遇到了问题,我遇到了这个问题,但我没有得到答案。我有重点,每个点都有一个类别。 我需要从过滤器显示这些数据:当我们来到页面加载 - 所有点时,当我们通过过滤器选择一个类别时 - 在地图上需要显示属于所选类别的点。我检查未选中的类别ID是否等同于标记类别ID。但标记不能删除。我做错了什么? 这是我的代码:

    function ajaxLoad(dataForm,urlAction,flag){
    $.ajax({
        data:dataForm,
        type: 'POST',
        url:urlAction,
        success:function(data){
            var data = JSON.parse(data);
            var i;
            var a;
            // get all unchecked categories
            var unchecked = ($("input:checkbox:not(:checked)"));
            markers = [];
            for (i = 0; i < data.length; i++) {

                point = new google.maps.LatLng(data[i][1], data[i][2]);
                marker = new google.maps.Marker({
                    position: point,
                    icon:data[i][4],
                    title:data[i][0],
                    //id category
                    id:data[i][5]

                });
                var content;
                if(data[i][3]) {
                    google.maps.event.addListener(marker, 'click', (function (marker, i) {
                        return function () {
                            infowindow.setContent(data[i][3]);
                            infowindow.open(map, marker);
                        }
                    })(marker, i));
                }

                marker.setMap(map);
                marker.setMap(null);
                markers[i] = marker;

                if(flag === true){
                    for (a = 0; a < unchecked.length; a++) {
                        var marker = markers[i];
                        //check if unchecked category id == markers category id
                        if(markers[i].id != unchecked[a].value){
                            marker.setMap(null);
                        }

                    }
                }


            }
            map.setCenter(marker.getPosition());
        },
        error:function(data){console.log(data)}
    });
}
$(document).ready(function(){
     map = new google.maps.Map(document.getElementById('map'), {
        zoom: 13,
        center: new google.maps.LatLng(50.7498176,25.3192401),
        mapTypeId: google.maps.MapTypeId.ROADMAP,
        scrollwheel: false,
    });
     infowindow = new google.maps.InfoWindow();
    //show all
    ajaxLoad(1,"<?php echo  Yii::app()->controller->createUrl('/advertise/default/getMap')?>",false);
   ($('input[type="checkbox"]').on('click',function(){
   var data = $('#form-filter').serialize();
   //filter category
   ajaxLoad(data,"<?php echo  Yii::app()->controller->createUrl('/advertise/default/filterMap')?>",true);

   } ));
    });

1 个答案:

答案 0 :(得分:0)

我解决了! 只需设置:

   for(i=0; i<markers.length; i++){
        markers[i].setMap(null);
    }
在ajax查询之前

并删除该部分:

            if(flag === true){
                for (a = 0; a < unchecked.length; a++) {
                    var marker = markers[i];
                    //check if unchecked category id == markers category id
                    if(markers[i].id != unchecked[a].value){
                        marker.setMap(null);
                    }

                }