简体   繁体   English

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

[英]delete specific marker in google map dynamically

I have faced to the problem and I was suffering this issue, but I didn't get an answer. 我已经面对了这个问题,并且正在遭受这个问题的困扰,但是我没有得到答案。 I have the point and each point has a category. 我有要点,每个要点都有一个类别。 And I need do display this data from the filter: when we came at page loaded -all points, when we select a category through the filter - on the map need display points that belong selected categories. 我需要显示来自过滤器的数据:当我们到达页面加载页面时-所有点,当我们通过过滤器选择类别时-在地图上需要显示属于所选类别的显示点。 I check if unchecked category id equivalent to markers category id. 我检查未经检查的类别ID是否等于标记的类别ID。 But markers not delete. 但是标记不会删除。 What I doing wrong? 我做错了什么? Here is my code: 这是我的代码:

    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);

   } ));
    });

I resolved it! 我解决了! Just set: 刚刚设置:

   for(i=0; i<markers.length; i++){
        markers[i].setMap(null);
    }

before ajax query and remove that piece: 在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);
                    }

                }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM