Filter GeometryCollection by condition

GeometryCollection.filter is used to filter geometries that fit for the test condition.

Test condition can be a function or a mapbox filter

index.html
<!DOCTYPE html>
<html>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Geometry - Filter GeometryCollection by condition</title>
  <style type="text/css">
    html,body{margin:0px;height:100%;width:100%}
    .container{width:100%;height:100%}
    .pane{background:#34495e;line-height:28px;color:#fff;z-index:10;position:absolute;top:20px;right:20px}
    .pane a{display:block;color:#fff;text-align:left;padding:0 10px;min-width:28px;min-height:28px;float:left}
  </style>
  <link rel="stylesheet" href="https://unpkg.com/maptalks/dist/maptalks.css">
  <script type="text/javascript" src="https://unpkg.com/maptalks/dist/maptalks.min.js"></script>
  <body>

    <div id="map" class="container"></div>
    <div class="pane"><a href="javascript:filter();">filter "foo == polygon"</a></div>

    <script>
      var map = new maptalks.Map('map', {
        center: [-0.113049, 51.498568],
        zoom: 14,
        baseLayer: new maptalks.TileLayer('base', {
          urlTemplate: 'https://{s}.basemaps.cartocdn.com/light_all/{z}/{x}/{y}.png',
          subdomains: ["a","b","c","d"],
          attribution: '&copy; <a href="http://osm.org">OpenStreetMap</a> contributors, &copy; <a href="https://carto.com/">CARTO</a>'
        })
      });

      var point = new maptalks.Marker([-0.113049 - 0.018, 51.498568 + 0.003],{
        symbol : {
          'textFaceName' : 'sans-serif',
          'textName' : 'MapTalks',
          'textFill' : '#34495e',
          'textHorizontalAlignment' : 'right',
          'textSize' : 40
        },
        properties : {
          'foo' : 'marker'
        }
      });
      var line = new maptalks.LineString([
        [-0.131049, 51.499568],
        [-0.107049, 51.499568]
      ],{
        symbol: {
          'lineColor' : '#1bbc9b',
          'lineWidth' : 3
        },
        properties : {
          'foo' : 'linestring'
        }
      });
      var polygon = new maptalks.Polygon([
        [-0.131049, 51.498568],
        [-0.107049, 51.498568],
        [-0.107049, 51.493568],
        [-0.131049, 51.493568],
        [-0.131049, 51.498568]
      ], {
        symbol: {
          'lineColor' : '#34495e',
          'lineWidth' : 2,
          'polygonFill' : 'rgb(135,196,240)',
          'polygonOpacity' : 0.6
        },
        properties : {
          'foo' : 'polygon'
        }
      });

      var collection = new maptalks.GeometryCollection([line, polygon, point], {
        visible : true,
        editable : true,
        cursor : null,
        draggable : false,
        dragShadow : false,
        drawOnAxis : null
      });

      new maptalks.VectorLayer('vector', collection)
          .addTo(map);

      // filter
      function filter() {
        // condition can be a mapbox filter or a function
        var filtered = collection.filter(['==', 'foo', 'polygon']);
        filtered.forEach(function (polygon) {
          polygon.updateSymbol({
            'polygonFill' : '#f00'
          });
        });
      }

    </script>
  </body>
</html>
Copied!