简体   繁体   English

iconFeature openlayers3的位置

[英]Location of iconFeature openlayers3

I have the following code for displaying map with a marker. 我有以下代码用于显示带有标记的地图。 I am using OpenLayers3. 我正在使用OpenLayers3。 The problem is that the Marker is not displayed in the correct position normally. 问题是标记无法正常显示在正确的位置。 It must be displayed in Canada but it is displayed in the middle of the map 它必须在加拿大显示,但必须显示在地图中间

    var iconFeature = new ol.Feature({
     geometry: new ol.geom.Point([-72.66, 45.04]),
     name: 'Null Island'
    });

    var iconStyle = new ol.style.Style({
     image: new ol.style.Icon(/** @type {olx.style.IconOptions} */ ({
     anchor: [0.5, 46],
     anchorXUnits: 'fraction',
     anchorYUnits: 'pixels',
     src: 'https://openlayers.org/en/v3.20.0/examples/data/icon.png'
     }))
    });

    iconFeature.setStyle(iconStyle);

    var vectorSource = new ol.source.Vector({
     features: [iconFeature]
    });

    var vectorLayer = new ol.layer.Vector({
     source: vectorSource
    });


    //map
   var map = new ol.Map({
    target: 'map',
    layers: [
      new ol.layer.Tile({
        source: new ol.source.OSM()
      }),vectorLayer
    ],
    view: new ol.View({
      center: ol.proj.fromLonLat([-72, 45]),
      zoom: 6
    })
  });

How i can make the marker in the correct location. 我如何在正确的位置制作标记。 Thanks. 谢谢。

OSM layer is projected in EPSG:3857 projection system. OSG层投影在EPSG:3857投影系统中。 But the coordinates passed to iconFeature is in WGS84 projection system. 但是传递给iconFeature的坐标在WGS84投影系统中。 While setting the center the coordinates are converted. 设置中心时,将转换坐标。 Need to apply same for iconFeature Object too. 同样需要为iconFeature Object应用相同的对象。

Transform iconFeature feature objects's coordinates like did for center. 像对中心一样,变换iconFeature特征对象的坐标。

var iconFeature = new ol.Feature({
     geometry: new ol.geom.Point(ol.proj.fromLonLat([-72.66, 45.04])),
     name: 'Null Island'
});

See the updated code in this plunker link 在此插件链接中查看更新的代码

.

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

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