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>

CSS

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

JavaScript

var lowMagnitude = new ol.style.Style({
  image: new ol.style.Icon({
    src: 'https://rawgit.com/mapbox/maki/master/icons/triangle-15.svg'
  })
});

var highMagnitude = new ol.style.Style({
  image: new ol.style.Icon({
    src: 'https://rawgit.com/mapbox/maki/master/icons/danger-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) {
        var title = feature.get('title');
        var magnitude = parseFloat(title.replace(/[^0-9\.]/g, ''));
        return magnitude >= 5 ? highMagnitude : lowMagnitude;
      }
    })
  ],
  view: new ol.View({
    center: [0, 0],
    zoom: 1
  })
});