I'm trying to add point (marker) on map click with custom style using OpenLayers 3. I tried this code, but nothing happens when user click on the map:
var map = new ol.Map({
layers: [
new ol.layer.Tile({
source: new ol.source.MapQuest({layer: 'sat'})
}),
new ol.layer.Vector({
source: vectorSource
})
],
target: 'map',
view: new ol.View({
center: [0, 0],
zoom: 2
})
});
var styles = {
'square': new ol.style.Style({
image: new ol.style.RegularShape({
fill: fillColor,
stroke: stroke,
points: 4,
radius: 10,
angle: Math.PI / 4
})
}),
'triangle': new ol.style.Style({
image: new ol.style.RegularShape({
fill: fillColor,
stroke: stroke,
points: 3,
radius: 10,
rotation: Math.PI / 4,
angle: 0
})
})
};
var stroke,
fillColor;
map.on("click", function(evt) {
stroke = new ol.style.Stroke({color: 'black', width: 1});
var coords = evt.coordinate;
var addedMarker = new ol.geom.Point( [
ol.proj.transform(coords, 'EPSG:4326', 'EPSG:3857')]);
fillColor = 'blue';
var featurething = new ol.Feature({
name: "Marker 01",
geometry: addedMarker
});
featurething.setStyle(styles['triangle']);
vectorSource.addFeature( featurething );
});