Select style

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">
  <vl-map ref="map">
    <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>
      <vl-source-vector ref="vectorSource">
        <vl-feature v-for="feature in geojsonObject.features" :key="feature.id"
                    :id="feature.id" :properties="feature.properties">
          <vl-geom-polygon :coordinates="feature.geometry.coordinates"></vl-geom-polygon>
          
          <vl-overlay :position="pointOnSurface(feature.geometry)" positioning="center-right">
            <div style="background: #fff; padding: 10px">
               This is popup
            </div>
          </vl-overlay>
        </vl-feature>
      </vl-source-vector>
      <vl-style-func :factory="styleFuncFactory"></vl-style-func>
    </vl-layer-vector>
  </vl-map>  
</div>

CSS

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

JavaScript

let geojsonObject = {
  'type': 'FeatureCollection',
  'crs': {
    'type': 'name',
    'properties': {
      'name': 'EPSG:3857'
    }
  },
  'features': [{
  	'id': 1,
    'type': 'Feature',
    'geometry': {
      'type': 'Polygon',
      'coordinates': [[[-5e6, 6e6], [-5e6, 8e6], [-3e6, 8e6],
                       [-3e6, 6e6], [-5e6, 6e6]]]
    }
  }]
};

new Vue({
	el: '#app',
  data () {
  	return {
    	zoom: 2,
      center: [0, 3000000],
      geojsonObject: geojsonObject,
      selectedFeatures: [],
    }
  },
  methods: {
    pointOnSurface: VueLayers.olExt.findPointOnSurface,
  	styleFuncFactory () {
    	const fill = new ol.style.Fill({
        color: 'rgba(0, 0, 255, 0.1)'
      })
      const fillSelected = new ol.style.Fill({
        color: 'rgba(50, 150, 255, 0.5)'
      })
      const stroke = new ol.style.Stroke({
        color: 'blue',
        width: 3
      })
      const circleStyle = new ol.style.Circle({
        radius: 5,
        fill: new ol.style.Fill({
          color: 'orange'
        })
      })
    
    	return feature => { 
      	const selected = this.selectedFeatures.filter(f => f.id === feature.getId()).length > 0
      
        const baseStyle = new ol.style.Style({
          stroke,
          fill: selected ? fillSelected : fill,
        })      
      	const verticiesStyle = new ol.style.Style({
          image: circleStyle,
          geometry: function(feature) {
            // return the coordinates of the first ring of the polygon
            var coordinates = feature.getGeometry().getCoordinates()[0];
            return new ol.geom.MultiPoint(coordinates);
          }
        })
        
        return [
          baseStyle,
          verticiesStyle,
        ];
      }
    },
  },
})