我正在尝试在OpenLayers 3
地图上添加制造商。
我发现的唯一示例是示例列表中的这个示例。OpenLayers
但是该示例使用ol.Style.Icon而不是OpenLayers.Marker in之类的东西OpenLayers 2
。
第一个问题
唯一的区别是您必须设置图像网址,但这是添加标记的唯一方法吗?
似乎也OpenLayers 3
没有附带标记图像,所以如果没有其他方法,那就有意义了ol.Style.Icon
第二个问题
如果有人能给我一个在地图加载后添加标记或图标的函数示例,那就太好了。
根据我在示例中的理解,他们为图标创建了一个图层
var iconFeature = new ol.Feature({
geometry: new ol.geom.Point(ol.proj.transform([-72.0704, 46.678], 'EPSG:4326', 'EPSG:3857')),
name: 'Null Island',
population: 4000,
rainfall: 500
});
var iconStyle = new ol.style.Style({
image: new ol.style.Icon(/** @type {olx.style.IconOptions} */ ({
anchor: [0.5, 46],
anchorXUnits: 'fraction',
anchorYUnits: 'pixels',
opacity: 0.75,
src: 'data/icon.png'
}))
});
iconFeature.setStyle(iconStyle);
var vectorSource = new ol.source.Vector({
features: [iconFeature]
});
var vectorLayer = new ol.layer.Vector({
source: vectorSource
});
然后他们在初始化地图时设置图标层
var map = new ol.Map({
layers: [new ol.layer.Tile({ source: new ol.source.OSM() }), vectorLayer],
target: document.getElementById('map'),
view: new ol.View2D({
center: [0, 0],
zoom: 3
})
});
如果我想添加许多标记,是否必须为每个标记创建 1 层?
我怎样才能在一个图层上添加许多标记?我不知道这部分会是什么样子
var vectorSource = new ol.source.Vector({
features: [iconFeature]
});
var vectorLayer = new ol.layer.Vector({
source: vectorSource
});
谢谢