【发布时间】:2014-06-19 20:35:16
【问题描述】:
我正在尝试在 OpenLayers 3 地图上添加制造商。
我发现的唯一示例是this 中的一个OpenLayers 示例list。
但该示例使用ol.Style.Icon 而不是OpenLayers 2 中的OpenLayers.Marker。
第一个问题
唯一的区别是您必须设置图像 URL,但这是添加标记的唯一方法吗?
另外,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
});
谢谢
【问题讨论】:
-
为了性能避免一一添加功能,使用批量添加
vectorSource.addFeatures(features); -
您找到的示例的链接已失效。