JSFiddle - React, Tailwind, and code Playground
HTML
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/openlayers/4.6.5/ol.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/openlayers/4.6.5/ol.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="zoomLevel">
Current zoom level: 3 (images dates will start to load at level 12)
</div>
<div id="map">
</div>
CSS
body {
padding: 0;
margin: 0;
}
html, body {
height: 100%;
width: 100%;
}
#map{
width:100%;
height:95%;
top:5%;
position:absolute
}
JavaScript
var map = new ol.Map({
target: 'map',
layers: [
new ol.layer.Tile({
source: new ol.source.XYZ({
url: 'https://server.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer/tile/{z}/{y}/{x}',
wrapX:false,
crossOrigin: 'anonymous'
})
}),
new ol.layer.Tile({
source: new ol.source.XYZ({
url: 'https://services.arcgisonline.com/ArcGIS/rest/services/Reference/World_Boundaries_and_Places/MapServer/tile/{z}/{y}/{x}',
wrapX:false,
crossOrigin: 'anonymous'
})
})
],
view: new ol.View({
center: ol.proj.fromLonLat([37.41, 8.82]),
zoom: 3
})
});
function polygonStyleFunction(feature) {
var fp = feature.getProperties()
var date = fp.SRC_DATE.toString();
var date_splitted = date.split('')
var date_text = date_splitted[0]+date_splitted[1]+date_splitted[2]+date_splitted[3]+"-"+date_splitted[4]+date_splitted[5]+"-"+date_splitted[6]+date_splitted[7] // UGLY AF
console.log(date_text)
return new ol.style.Style({
stroke: new ol.style.Stroke({
color: 'rgba(255, 255, 0, 0.9)',
width: 1
}),
fill: new ol.style.Fill({
color: 'rgba(0, 0, 255, 0)'
}),
text: new ol.style.Text({
text: date_text,
overflow:true,
font: '14px sans-serif',
color: 'rgba(255, 255, 255, 1)',
stroke: new ol.style.Stroke({
color: 'rgba(255, 255, 0, 1)',
width: 5
})
})
});
}
var vectorSource = new ol.source.Vector(),
vectorLayer = new ol.layer.Vector({
source: vectorSource,
style: polygonStyleFunction
});
vectorLayer.setMap(map)
...