Points lines switch 2

by Vladimir Vershinin

HTML

<script src="https://unpkg.com/[email protected]/lodash.js"></script>
<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://npmcdn.com/@turf/turf/turf.min.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>Current zoom {{ zoom }}</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-layer-tile>
    
    <vl-layer-vector id="points" ref="pointsLayer" render-mode="image" :visible="visibleLayer === 'points'" @created="onPointsLayerCreated">
      <vl-style-func :factory="pointsStyleFunc" />
    </vl-layer-vector>
    <vl-layer-vector id="lines" ref="linesLayer" render-mode="image" :visible="visibleLayer === 'lines'" @created="onLinesLayerCreated">
      <vl-style-func :factory="linesStyleFunc" />
    </vl-layer-vector>
    
    <vl-interaction-select :features.sync="selectedFeatures" />
    
    <vl-overlay v-for="feature in selectedFeatures" :key="feature.id"
                :id="'popup-' + feature.id"
                :position="findPointOnSurface(feature)">
      <div>
        ID {{ feature.id }}<br>
        Address {{ feature.properties.address }}
      </div>
    </vl-overlay>
  </vl-map>  
</div>

CSS

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

.vl-overlay {
  padding: 10px;
  box-shadow: 0 0 5px rgba(2, 2, 2, 0.2);
  background: #fff;
}

JavaScript

let colors = [
	'#F44336',
  '#9C27B0',
  '#3F51B5',
  '#8BC34A',
]

const points = _.range(0, 10000).map(i => {
      	let coordinate = [
        	_.random(-50, 50),
          _.random(-50, 50),
        ]
        let geometry = new ol.geom.Point(ol.proj.fromLonLat(coordinate))
        return new ol.Feature({
        	geometry,
          address: '1234567, Russia, Moscow, ...',
          color: colors[_.random(0, colors.length - 1)],
        })
      })
const lines = _.range(0, 1000).map(i => {
        let geometry = new ol.geom.LineString([
        	ol.proj.fromLonLat([_.random(-50, 50), _.random(-50, 50)]),
          ol.proj.fromLonLat([_.random(-50, 50), _.random(-50, 50)]),
          ol.proj.fromLonLat([_.random(-50, 50), _.random(-50, 50)]),
          ol.proj.fromLonLat([_.random(-50, 50), _.random(-50, 50)]),
        ])
        return new ol.Feature({
        	geometry,
          address: '1234567, Russia, Moscow, ...',
          color: colors[_.random(0, colors.length - 1)],
        })
      })

new Vue({
	el: '#app',
  data () {
  	return {
    	zoom: 2,
      center: [0, 0],
      selectedFeatures: [],
    }
  },
  computed: {
  	visibleLayer () {
    	return this.zoom <= 5 ? 'lines' : 'points'
    },
  },
  methods: {
  	findPointOnSurface (feature) {
    	const point = turf.pointOnFeature(feature)
      
      return point.geometry.coordinates
    },
    pointsStyleFunc () {
      // style function and styles using OpenLayers API
      // https://openlayers.org/en/latest/apidoc/module-ol_style_Style.html
      return feature => {
        let baseStyle = new ol.style.Style({
          image: new ol.style.Circle({
            radius: 10,
            stroke: new ol.style.Stroke({
              color: feature.get('color'),
            }),
          }),
        })
      	return [
        	baseStyle,
        ]
      }
    },
    linesStyleFunc () {
      // style function and styles using OpenLayers API
      //...