OpenLayers基本标记

问题描述 投票:0回答:1

我正在使用OpenLayers的最新CDN版本,而我正在努力为我的地图添加一个标记。这是我到目前为止所拥有的....(为客户的隐私省略了一些变量)

var map = new ol.Map({
  target: 'map',
  layers: [
    new ol.layer.Tile({
      source: new ol.source.OSM()
    })
  ],
  view: new ol.View({
    center: ol.proj.fromLonLat([long, lat]),
    zoom: 14
  })
});

//Adding a marker on the map
var marker = new ol.Feature({
  geometry: new ol.geom.Point(
    ol.proj.fromLonLat([long, lat])
  ),
  name: name,
});

var iconStyle = new Style({
  image: new Icon(({
    anchor: [0.5, 46],
    anchorXUnits: 'fraction',
    anchorYUnits: 'pixels',
    src: icon_src
  }))
});

marker.setStyle(iconStyle);

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

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

map.addLayer(markerVectorLayer);
<div id="map" class="map" style="width:1022px;height:240px;margin-top:15px;"></div>

我在控制台中得到“样式未定义”,如果我尝试导入样式中显示的样式,它会提到导入只能在模块顶部完成...我认为它需要webpack,或者类似的东西,我很确定我的网站没有运行。我只需要一个非常基本的地图标记,比如红色的Google地图图标,没什么特别的。

openlayers
1个回答
1
投票

CD5版本OL5的语法与OL4相同

var iconStyle = new ol.style.Style({
  image: new ol.style.Icon(({

为版本4 API添加书签可能很有用,但如果发生其他更改,请始终与版本5进行交叉检查http://openlayers.org/en/v4.6.5/apidoc/index.html

© www.soinside.com 2019 - 2024. All rights reserved.