JSFiddle - React, Tailwind, and code Playground
by ahocevar
HTML
<script src="https://openlayers.org/en/v3.19.1/build/ol.js"></script>
<link rel="stylesheet" href="https://openlayers.org/en/v3.19.1/css/ol.css">
<div id="map" class="map"></div>
JavaScript
var raster = new ol.layer.Tile({
source: new ol.source.OSM()
});
var map = new ol.Map({
layers: [raster],
target: 'map',
view: new ol.View({
center: [-11000000, 4600000],
zoom: 4
})
});
var features = new ol.Collection();
var featureSource = new ol.source.Vector({features: features});
var featureOverlay = new ol.layer.Vector({
source: featureSource,
style: new ol.style.Style({
fill: new ol.style.Fill({
color: 'rgba(255, 255, 255, 0.2)'
}),
stroke: new ol.style.Stroke({
color: '#ffcc33',
width: 2
}),
image: new ol.style.Circle({
radius: 7,
fill: new ol.style.Fill({
color: '#ffcc33'
})
})
})
});
map.addLayer(featureOverlay);
var modify = new ol.interaction.Modify({
features: features,
// the SHIFT key must be pressed to delete vertices, so
// that new vertices can be drawn at the same position
// of existing vertices
deleteCondition: function(event) {
return ol.events.condition.shiftKeyOnly(event) &&
ol.events.condition.singleClick(event);
}
});
map.addInteraction(modify);
var draw = new ol.interaction.Draw({
features: features,
type: 'Point'
});
map.addInteraction(draw);
var id = 0;
featureSource.on(['addfeature', 'changefeature'], function(e) {
if (!e.feature.getId()) {
e.feature.setId(++id);
}
console.log(e.type, e.feature.getId(), e.feature.getGeometry().getCoordinates());
});