get coordinates event map openlayers 4.6.5 ~ 5
Okay, turns out it just works with event.coordinate
, displaying coordinates in the SRC defined in the layer
This code will work. Note that coordinates you will get are in EPSG:3857
projection use ol.proj.transform()
to convert them in to EPSG:4326
projection. To know more about projections visit https://lyzidiamond.com/posts/4326-vs-3857. You must read this if you are going to work on openlayers frequently.
const localmap = new ol.Map({
layers: [
new ol.layer.Tile({
source: new ol.source.OSM()
})
],
target: 'map',
view: new ol.View({
center: [0, 0],
zoom: 2
})
});
localmap.on('singleclick', function (evt) {
console.log(evt.coordinate);
// convert coordinate to EPSG-4326
console.log(ol.proj.transform(evt.coordinate, 'EPSG:3857', 'EPSG:4326'));
});