ol.layer.Vector

by ahocevar

HTML

<script src="https://cdnjs.cloudflare.com/ajax/libs/ol3/3.19.1/ol.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/ol3/3.19.1/ol.css">
<div id="map"></div>
<form>
  <input type="checkbox" checked name="labels">
  <label for="labels">Labels (Zoom >= 7)</label>
</form>

CSS

#map {
  width: 512px;
  height: 256px;
}

JavaScript

var form = document.forms[0];

var style = new ol.style.Style({
  text: new ol.style.Text({
    text: '',
    font: '12px Arial 700',
    stroke: new ol.style.Stroke({
      color: 'white',
      width: 1
    }),
    fill: new ol.style.Fill({
      color: 'navy'
    })
  }),
  image: new ol.style.Icon({
    src: 'https://rawgit.com/mapbox/maki/master/icons/triangle-15.svg'
  })
});

var map = new ol.Map({
  target: 'map',
  layers: [
    new ol.layer.Tile({
      source: new ol.source.OSM()
    }),
    new ol.layer.Vector({
      title: 'Earthquakes',
      source: new ol.source.Vector({
        format: new ol.format.GeoJSON(),
        url: 'https://rawgit.com/openlayers/workshop/v3.16.0/src/en/data/layers/7day-M2.5.json'
      }),
      style: function(feature, resolution) {
        style.getText().setText(
          resolution < 2000 && form['labels'].checked ?
          feature.get('title').split(',').join('\n\n') :
          '');
        return style;
      }
    })
  ],
  view: new ol.View({
    center: [0, 0],
    zoom: 1
  })
});

form.onclick = function() {
debugger
  map.getLayers().item(1).changed();
}