我想知道是否有人可以指出我正确的方向。我尝试使用OpenLayers 4.6.4向OSM地图添加多个标记/图标。
以下代码可以毫无问题地渲染地图:
<script src="https://openlayers.org/en/v4.6.4/build/ol.js"></script>
<script type='text/javascript'>
var map = new ol.Map({
target: 'mapdiv',
layers: [
new ol.layer.Tile({
source: new ol.source.OSM()
})
],
view: new ol.View({
center: ol.proj.fromLonLat([-0.1526069, 51.4790309]),
zoom: 14
})
});
</script>
我已尝试在OpenLayers网站上搜索有关如何添加标记的信息,但它已声明标记已弃用!我想要做的就是在地图上添加一些图钉/标记来显示网站位置。
我还尝试过其他版本的OpenLayers 2.13.1,2.14和3.0,但没有成功。
非常感谢任何协助。
答案 0 :(得分:5)
旧的标记已弃用。您应该将几何Points与Icon style一起使用。 Icon Symbolizer Example是您正在寻找的那个:
var iconFeature1 = new ol.Feature({
geometry: new ol.geom.Point(ol.proj.fromLonLat([-0.1526069, 51.4790309]),),
name: 'Somewhere',
});
var iconFeature2 = new ol.Feature({
geometry: new ol.geom.Point(ol.proj.fromLonLat([-0.1426069, 51.4840309])),
name: 'Somewhere else'
});
// specific style for that one point
iconFeature2.setStyle(new ol.style.Style({
image: new ol.style.Icon({
anchor: [0.5, 46],
anchorXUnits: 'fraction',
anchorYUnits: 'pixels',
src: 'https://upload.wikimedia.org/wikipedia/commons/thumb/9/93/Map_marker_font_awesome.svg/200px-Map_marker_font_awesome.svg.png',
})
}));
const iconLayerSource = new ol.source.Vector({
features: [iconFeature1, iconFeature2]
});
const iconLayer = new ol.layer.Vector({
source: iconLayerSource,
// style for all elements on a layer
style: new ol.style.Style({
image: new ol.style.Icon({
anchor: [0.5, 46],
anchorXUnits: 'fraction',
anchorYUnits: 'pixels',
src: 'https://openlayers.org/en/v4.6.4/examples/data/icon.png'
})
})
});
const map = new ol.Map({
target: 'map',
layers: [
new ol.layer.Tile({
source: new ol.source.OSM(),
}),
iconLayer
],
view: new ol.View({
center: ol.proj.fromLonLat([-0.1526069, 51.4790309]),
zoom: 14
})
});
<link href="https://openlayers.org/en/v4.6.4/css/ol.css" rel="stylesheet"/>
<script src="https://openlayers.org/en/v4.6.4/build/ol-debug.js"></script>
<div id="map" class="map"></div>