Cannot place marker at correct coordinates using open layers 3

I am working on openlayers 3 and want to implement a search function that gets the name of the place and positions the marker on the map. I can get the coordinates, but when I want to add a marker to the map, I always get different places for this. The entry marker does not fit in the actual map coordinates.

Here is the code I was working on:

function addmarker(lat, long, pointerimgsrc){ var iconFeature = new ol.Feature({ geometry: new ol.geom.Point(ol.proj.transform([lat, long], 'EPSG:4326', 'EPSG:3857')), name: 'NULL' }); var iconStyle = new ol.style.Style({ image: new ol.style.Icon(({ anchor: [0.5, 46], anchorXUnits: 'fraction', anchorYUnits: 'pixels', opacity: 0.75, //src: 'data/icon.png' src: pointerimgsrc })) }); iconFeature.setStyle(iconStyle); vectorSource = new ol.source.Vector({ features: [iconFeature] }); vectorLayer = new ol.layer.Vector({ source: vectorSource }); map.addLayer(vectorLayer); }// END addmarkerr() 

I hope that I clearly explained my problem, awaiting a solution. Thank you so much for your time and support.

+5
source share
1 answer

EPSG coordinate order: 4326 lon, lat not lat, lon. Therefore, you should change the line that converts EPSG: 4326 to EPSG: 3857.

 ol.proj.transform([lon, lat], 'EPSG:4326', 'EPSG:3857') 
+7
source

Source: https://habr.com/ru/post/1210864/


All Articles