我刚刚开始使用 OpenLayers 3,我正在尝试使用坐标动态更新要素几何属性,显然我错过了一些东西,因为要素没有移动。到目前为止,这是我的上帝:
Socket.IO
socket.on('mapData', function(mapData) {
if (mapisloaded) {
latLon = ol.proj.transform([mapData.lon, mapData.lat], 'EPSG:4326', 'EPSG:3857');
// Initiate latlong object with mapData
if (centerIsRequested) {
//Center map with mapData
};
// Update marker with latlong from mapData
};
});
OpenLayers 3 基于 Vector Icon Example
var latLon = ol.proj.transform([10.904108, 59.788187], 'EPSG:4326', 'EPSG:3857');
var iconFeature = new ol.Feature({
geometry: new ol.geom.Point(latLon),
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: 'imgs/lc.png'
}))
});
iconFeature.setStyle(iconStyle);
var vectorSource = new ol.source.Vector({
features: [iconFeature]
});
var vectorLayer = new ol.layer.Vector({
source: vectorSource
});
var baseLayer = new ol.layer.Tile({
source: new ol.source.OSM()
});
var view = new ol.View({
center: latLon,
zoom: 18,
});
var map = new ol.Map({
target: 'map-canvas',
layers: [ baseLayer, vectorLayer ],
view: view
});
这些变化显然没有改变,但我知道魔法并不存在,它只是把一些东西写下来。
我将如何完成这个简单的任务?我唯一想要的是当 socket.io 检测到新的 map 数据(mapData.lat、mapData.lon)时图标更新其在 map 上的位置。
我尝试深入研究不同的对象并在控制台和文档中读取它们的属性,并且我在 Stackoverflow 上进行了搜索,但遗憾的是没有运气。我是否要连接到 iconFeature,还是必须以其他方式执行此操作?也许内置了一些非常简单的东西?非常感谢任何帮助。
最佳答案
如果您想在 map 上移动图标,最好使用 ol.Overlay
为了这。您可以在每次更改时使用 marker.setPosition(coord)
。
A working fiddle 。单击 map 以更改标记的位置。
关于javascript - 我如何动态更新 OpenLayers 3 中的 ol.Features 几何属性(坐标),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31942389/