Select use case

by Vladimir Vershinin

HTML

<script src="https://unpkg.com/vue@latest/dist/vue.min.js"></script>
<script src="https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io/en/v5.3.0/build/ol.js"></script>
<script src="https://unpkg.com/[email protected]/lib/index.umd.js"></script>
<link rel="stylesheet" href="https://unpkg.com/[email protected]/lib/style.css">
<div id="app">
  <div>Selected features: {{ selectedFeatures }}</div>
  <vl-map ref="map" data-projection="EPSG:4326">
    <vl-view ref="mapView" :zoom.sync="zoom" :center.sync="center"></vl-view>
    
    <vl-layer-tile>
      <vl-source-osm></vl-source-osm>
    </vl-layer-tile>
    
    <vl-layer-vector id="points">
      <vl-source-vector ref="pointsSource" :features="points"></vl-source-vector>
    </vl-layer-vector>
    
    <vl-layer-vector id="polygons">
      <vl-source-vector ref="polygonsSource" :features="polygons"></vl-source-vector>
    </vl-layer-vector>
    
    
    <vl-interaction-select :features.sync="selectedFeatures"></vl-interaction-select>
    
    <vl-overlay v-for="feature in selectedPoints" :key="feature.id" :id="feature.id + '-popup'" :position="feature.geometry.coordinates">
      <div style="background: #fff">
        Feature {{ feature.id }}
      </div>
    </vl-overlay>
  </vl-map>
</div>

CSS

html, body, #app {
  width: 100%;
  height: 100%;
  margin: 0;
  padding: 0;
}

JavaScript

new Vue({
	el: '#app',
  data () {
  	return {
    	zoom: 2,
      center: [0, 0],
      points: [
      	{
        	type: 'Feature',
        	id: 'one',
          properties: {},
          geometry: {
          	type: 'Point',
            coordinates: [10, 10],
          },
        },
      ],
      polygons: [
      	{
        	type: 'Feature',
          id: 'two',
          properties: {},
          geometry: {
          	type: 'Polygon',
            coordinates: [[
            	[-20, -20],
              [-20, -10],
              [-10, -10],
              [-10, -20],
              [-20, -20],
            ]],
          },
        },
      ],
      selectedFeatures: [],
    }
  },
  computed: {
    selectedPoints () {
    	return this.selectedFeatures.filter(feature => {
      	return this.$refs.pointsSource.featureIds.includes(feature.id)
      })
    },
    selectedPolygons () {
      return this.selectedFeatures.filter(feature => {
      	return this.$refs.polygonsSource.featureIds.includes(feature.id)
      })
    },
  },
  watch: {
    selectedPolygons (polygons) {
    	console.log('selected polygons', polygons)
    },
  },
})