简体   繁体   English

不能使用openlayers 3在正确的坐标处放置标记

[英]Cannot place marker at the right coordinates using openlayers 3

I am working on openlayers 3 and want to implement a search functionality, which gets a name of the place and positions a marker on the map. 我正在研究openlayers 3并希望实现一个搜索功能,它获取地点的名称并在地图上定位标记。 I am able to get the coordinates but when I want to add it's marker on the map, I am always getting different locations for it. 我能够获得坐标但是当我想在地图上添加它的标记时,我总是得到不同的位置。 The marker of the input place is not being placed on actual coordinates of the map. 输入位置的标记未放置在地图的实际坐标上。

Here is the code on which I have been working: 这是我一直在工作的代码:

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 I have clearly explained my problem, looking forward for a solution. 我希望我已经清楚地解释了我的问题,期待找到解决方案。 Thank you very much in advance for your time and support. 非常感谢您的时间和支持。

The EPSG:4326 coordinate order lon, lat not lat, lon. EPSG:4326坐标顺序lon,lat not lat,lon。 So you should change the line that does the EPSG:4326 to EPSG:3857 transformation. 因此,您应该更改执行EPSG的行:4326到EPSG:3857转换。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM