【问题标题】:Cannot place marker at the right coordinates using openlayers 3无法使用openlayers 3将标记放置在正确的坐标处
【发布时间】:2015-01-11 18:31:04
【问题描述】:

我正在开发 openlayers 3,并希望实现一个搜索功能,该功能可以获取地点名称并在地图上放置一个标记。我能够获得坐标,但是当我想在地图上添加它的标记时,我总是得到不同的位置。输入地点的标记未放置在地图的实际坐标上。

这是我一直在处理的代码:

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()

我希望我已经清楚地解释了我的问题,期待解决方案。非常感谢您的时间和支持。

【问题讨论】:

    标签: javascript openstreetmap openlayers-3


    【解决方案1】:

    EPSG:4326 坐标顺序 lon,lat 不是 lat,lon。因此,您应该将执行 EPSG:4326 转换为 EPSG:3857 转换的行更改。

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

    【讨论】:

    • 没有同样的问题,我正在进入慕尼黑(实际纬度/经度为:48.1372719、11.5754815)但它在纬度/经度处放置标记:11.557617187499996、-41.60722821271713
    • 但如果我写硬编码值,它就可以工作。如下:ol.proj.transform([11.5754815, 48.1372719], 'EPSG:4326', 'EPSG:3857')
    • 也许你的 lon、lat 值是字符串。将它们转换为数字,使用 +lon+lat,然后将它们传递给转换。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多