du13520157325 2017-03-28 13:57
浏览 37
已采纳

Google地图标记不会在复选框操作时移除

I was having some problem trying to filter the list fetched from database upon checkbox action and plot/clear markers onto map. Here is my checkbox declaration in HTML:

<div class="">
     <label>
          <input type="checkbox" class="js-switch" name="tags" value="3" onchange="filterList()" unchecked/> MRT Incidents
     </label>
</div>

When checkbox onchange, I am filtering the list fetched from database:

        function filterList(){
        var tags = document.getElementsByName('tags');
        var i = 0;
        {% for crisis in data %}  
            // code to store fields in database into local var

            for( ; i < tags.length; i++ )
            {
                if( tags[i].checked ) {
                    value = tags[i].value;
                    // I got 4 category 1-fire, 2-flood, 3-mrt, 4-dengue
                        // what I am doing here is check if the checkbox value is equal to the category ID from database, if equal, I push them into the filteredList

                    if(value == category){
                        filteredList.push({
                            // all the fields
                        });
                    }                        break;
                }else{
                        value = tags[i].value;
                        if(value == category){
                            filteredList = [];
                            setMapOnAll(null);
                        }
                    }
            }
        {% endfor %}

    addMarker(filteredList);
    }

Then in my addMarker with filteredList parameter:

function addMarker(filteredList){ 
        for(var i = 0; i < filteredList.length; i++){
        myLatLng = {lat: parseFloat(filteredList[i].lat), lng: parseFloat(filteredList[i].lng) };

        marker = new google.maps.Marker({
            position: myLatLng,
            map: map,
            clickable: true
        });
}

My plotting works weird as well. When I try to check multiple box, let's say I checked the first one, it plotted out. Then I proceed to check the second, it does not plot out but only plot out after I uncheck the first one.

When I try to uncheck the checkbox, the markers on the map are not removed. Instead, it just stay there forever and stacked more and more when I check/uncheck a few times.

Why is it so? Thanks in advance!

  • 写回答

3条回答 默认 最新

  • dqe9657 2017-03-28 15:04
    关注

    From what I can see in your code, you are adding the markers over and over again? This probably explains why you are having weird plotting (and you will run out of memory sooner or later). I think you are using the wrong approach though for marker filtering on a Google Map.

    You'll need to make changes to your code, here are the main guidelines :

    • You need an array/object to store all the marker objects you placed on the map
    • You can store the categorie(s) for each marker in that same object i.e. [{markerObj:marker, categories:[1,5,6]}]
    • When a checkbox changes (onchange) you loop through all the checkboxes and get their values then you loop through all the markers in your marker array/object and check if it needs to be filtered or not.
    • If you want to hide a marker just call the setVisible function i.e. markerObj.setVisible(false)

    Here's a working example! https://jsfiddle.net/fjqagp8o/1/

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么