简体   繁体   中英

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. 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:

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

                }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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