简体   繁体   English

对Mapbox中的标记进行多重过滤

[英]Multiple Filtering on markers in mapbox

I am trying to load a geojson data in mapbox, where I want to apply multiple filtering using check-box on the markers. 我正在尝试在mapbox中加载geojson数据,我想在其中使用标记上的复选框应用多个过滤。 I tried looking at the example given here: https://www.mapbox.com/mapbox.js/example/v1.0.0/non-exclusive-markers/ 我尝试着看这里给出的示例: https : //www.mapbox.com/mapbox.js/example/v1.0.0/non-exclusive-markers/

So the filtering here is based on the health and the asset code of each marker. 因此,此处的过滤基于每个标记的健康状况和资产代码。 Whether 'Healthy','Warning','Stopped' or '123125','123135','123145'. 是否“健康”,“警告”,“停止”或“ 123125”,“ 123135”,“ 123145”。 The code works to load the map and also place the markers based on the color of its health. 该代码用于加载地图,并根据其运行状况的颜色放置标记。 The filter menu also gets the six filter options. 过滤器菜单还获得六个过滤器选项。 But when selecting two filters with same marker the marker disappears from the screen. 但是,当选择两个具有相同标记的过滤器时,标记将从屏幕上消失。

For example, when selecting '123125' and then selecting 'Warning'. 例如,当选择“ 123125”然后选择“警告”时。 The yellow marker appears on the screen but when deselecting any one of the filters it disappears. 黄色标记出现在屏幕上,但是当取消选择任何一个过滤器时,它会消失。 I am not getting how to prevent this. 我没有得到如何防止这种情况。 Same thing happens with '123135', 'Stopped' and for '123145','Healthy'. '123135','Stopped'和'123145','Healthy'也会发生相同的情况。

Kindly have a look at the code below and help me out. 请查看下面的代码,帮帮我。

 <script src='https://code.jquery.com/jquery-1.11.0.min.js'></script> <script src="/js/leaflet-0.7.2/leaflet.ajax.min.js"></script> <script> L.mapbox.accessToken = 'pk.eyJ1IjoiZG9zcyIsImEiOiI1NFItUWs4In0.-9qpbOfE3jC68WDUQA1Akg'; var map = L.mapbox.map('map', 'mapbox.streets'); var featureLayerGeoJSON = { "type": "FeatureCollection", "features": [ { "type": "Feature", "properties": { "title":"Volkswagen Group", "healthy":false, "warning":true, "stopped":false, "marker-color":"#FFFF00", "marker-size":"medium", "Asset_Code": "123125", "Health_Code": "Warning" }, "geometry": { "type": "Point", "coordinates": [ -43.172502,-22.965881 ] } }, { "type": "Feature", "properties": { "title":"Volkswagen Group", "healthy":false, "warning":true, "stopped":false, "marker-color":"#FF0000", "marker-size":"medium", "Asset_Code": "123135", "Health_Code": "Stopped" }, "geometry": { "type": "Point", "coordinates": [ -8.990956,38.581054 ] } }, { "type": "Feature", "properties": { "title":"Volkswagen Group", "healthy":false, "warning":true, "stopped":false, "marker-color":"#1EB01E", "marker-size":"medium", "Asset_Code": "123145", "Health_Code": "Healthy" }, "geometry": { "type": "Point", "coordinates": [ 139.740667,35.626567 ] } } ] }; map.featureLayer.setGeoJSON(featureLayerGeoJSON); // Find and store a variable reference to the list of filters. var filters = document.getElementById('filters'); // Wait until the marker layer is loaded in order to build a list of possible // types. If you are doing this with another featureLayer, you should change // map.featureLayer to the variable you have assigned to your featureLayer. var makeCheckboxes1 = function() { // Collect the types of symbols in this layer. you can also just // hardcode an array of types if you know what you want to filter on, // like var types = ['foo', 'bar']; var typesObj = {}, types = []; map.featureLayer.eachLayer(function (entity) { typesObj[entity.feature.properties['Asset_Code']] = true; }) for (var k in typesObj) types.push(k); var checkboxes = []; // Create a filter interface. for (var i = 0; i < types.length; i++) { // Create an an input checkbox and label inside. var item = filters.appendChild(document.createElement('div')); var checkbox = item.appendChild(document.createElement('input')); var label = item.appendChild(document.createElement('label')); checkbox.type = 'checkbox'; checkbox.id = types[i]; checkbox.checked = true; // create a label to the right of the checkbox with explanatory text label.innerHTML = types[i]; label.setAttribute('for', types[i]); // Whenever a person clicks on this checkbox, call the update(). checkbox.addEventListener('change', update); checkboxes.push(checkbox); } // This function is called whenever someone clicks on a checkbox and changes // the selection of markers to be displayed. function update() { var enabled = {}; // Run through each checkbox and record whether it is checked. If it is, // add it to the object of types to display, otherwise do not. for (var i = 0; i < checkboxes.length; i++) { if (checkboxes[i].checked) enabled[checkboxes[i].id] = true; } map.featureLayer.setFilter(function(feature) { // If this symbol is in the list, return true. if not, return false. // The 'in' operator in javascript does exactly that: given a string // or number, it says if that is in a object. // 2 in { 2: true } // true // 2 in { } // false return (feature.properties['Asset_Code'] in enabled); }); } } var makeCheckboxes2 = function() { // Collect the types of symbols in this layer. you can also just // hardcode an array of types if you know what you want to filter on, // like var types = ['foo', 'bar']; var typesObj = {}, types = []; map.featureLayer.eachLayer(function (entity) { typesObj[entity.feature.properties['Health_Code']] = true; }) for (var k in typesObj) types.push(k); var checkboxes = []; // Create a filter interface. for (var i = 0; i < types.length; i++) { // Create an an input checkbox and label inside. var item = filters.appendChild(document.createElement('div')); var checkbox = item.appendChild(document.createElement('input')); var label = item.appendChild(document.createElement('label')); checkbox.type = 'checkbox'; checkbox.id = types[i]; checkbox.checked = true; // create a label to the right of the checkbox with explanatory text label.innerHTML = types[i]; label.setAttribute('for', types[i]); // Whenever a person clicks on this checkbox, call the update(). checkbox.addEventListener('change', update); checkboxes.push(checkbox); } // This function is called whenever someone clicks on a checkbox and changes // the selection of markers to be displayed. function update() { var enabled = {}; // Run through each checkbox and record whether it is checked. If it is, // add it to the object of types to display, otherwise do not. for (var i = 0; i < checkboxes.length; i++) { if (checkboxes[i].checked) enabled[checkboxes[i].id] = true; } map.featureLayer.setFilter(function(feature) { // If this symbol is in the list, return true. if not, return false. // The 'in' operator in javascript does exactly that: given a string // or number, it says if that is in a object. // 2 in { 2: true } // true // 2 in { } // false return (feature.properties['Health_Code'] in enabled); }); } } makeCheckboxes1(); makeCheckboxes2(); </script> 
 <style> body { margin:0; padding:0; } #map { position:absolute; top:0; bottom:0; width:100%; } .filter-ui { background:#fff; position:absolute; top:10px; right:10px; z-index:100; padding:10px; border-radius:3px; } </style> 
 <!DOCTYPE html> <html> <head> <meta charset=utf-8 /> <title>Multiple filters on markers</title> <meta name='viewport' content='initial-scale=1,maximum-scale=1,user-scalable=no' /> <script src='https://api.mapbox.com/mapbox.js/v2.3.0/mapbox.js'></script> <link href='https://api.mapbox.com/mapbox.js/v2.3.0/mapbox.css' rel='stylesheet' /> </head> <body> <nav id='filters' class='filter-ui'> <h4 align='center'>Filter based on Health</h4> </nav> <div id='map'></div> </body> </html> 

Kindly help me with this. 请帮助我。

You defined two append function in your code; 您在代码中定义了两个append函数;

checkboxes.push(checkbox); //this is first when makeCheckboxes1() call.
checkboxes.push(checkbox); //this is first when makeCheckboxes2() call.

When a checkbox list is empty it will clear all sections. 复选框列表为空时,将清除所有部分。 You need to create a checkbox object which contains these two list and then you can append. 您需要创建一个包含这两个列表的复选框对象,然后可以追加。

Fiddle Example 小提琴的例子

Problem: Now you have duplicate objects. 问题:现在您有重复的对象。 You need to control types, if exist don't push object to types. 您需要控制类型,如果存在则不要将对象推入类型。

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

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