javascript - 如何在openlayer上的 map 上添加标记?

标签 javascript openlayers-3

我正在使用 openlayer,但我无法在 map 上添加带有纬度/经度的标记。 您能否在我的代码示例下方指出正确的方向:

let map = new ol.Map({
  target: 'map',
  layers: [
    new ol.layer.Tile({
      source: new ol.source.OSM()
    })
  ],
  view: new ol.View({
    center: ol.proj.transform([37.41, 8.82], 'EPSG:4326', 'EPSG:3857'),
    zoom: 4
  })
})
let layerCloud = new ol.layer.Tile({
  source: new ol.source.XYZ({
    url: api.mapTemperature()
  })
})
map.addLayer(layerCloud)

最佳答案

您可以通过添加新的矢量图层来添加标记,并将其样式设置为显示图钉图像。下面的代码将在 map 中心添加一个图钉。

const center = map.getView().getCenter();
const pinnedLocation = ol.proj.transform(center, 'EPSG:3857', 'EPSG:4326');
const feature = new ol.Feature(new ol.geom.Point(center));
const pinLayer = new ol.layer.Vector ({
  source: new ol.source.Vector ({
    features: [feature]
  }),
  style: new ol.style.Style ({
    image: new ol.style.Icon({
      src: 'http://openlayers.org/en/v3.8.2/examples/data/icon.png'
    })
  })
});
map.addLayer (pinLayer);

关于javascript - 如何在openlayer上的 map 上添加标记?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45401647/

相关文章:

javascript - 开放层 3 : Omit SelectInteraction after DrawInteraction

font-awesome - 使用Semantic-UI(或Font Awesome)图标作为OpenLayers3中的标记

javascript - 为什么 Math.min() 从 [+0, 0, -0] 返回 -0

javascript - 单击链接外部关闭 jquery 下拉菜单

javascript - jquery单击时更改图像src,并在第二次单击时恢复为原始图像

javascript - 如何在openlayers中获取当前的缩放

javascript - 为什么 openlayers 在 map 单击时返回投影坐标?

javascript - 动态共享点面包屑路径编辑

javascript - 在 ReactJs 中使用 react-router 获取事件路由

javascript - CORS 策略已阻止从源 'null' 访问图像