修复了OpenLayers 3中的图标大小 [英] Fixed icon size in OpenLayers 3
问题描述
我现在搜索了2个小时,但是仍然不清楚OL3中是否可能。
我希望我的图标固定大小(不是屏幕,而是我正在使用的图像地图)。我的意思是,它应该覆盖相同的区域,即使我缩小了,而不是覆盖地图的一半(就像我使用圆形多边形,但我有复杂的图标,所以我必须使用它作为点要素)。有什么解决方案吗?
与QGIS相似:MAP UNITS。
I searched for 2 hours now, but it's still not clear if it's possible or not in OL3. I would like my icons to be fixed size (not to the screen, but to the image map I'm using). I mean, it should cover the same area even if I'm zoomed out, and not covering the half of the map (like I was using a circle polygon, but I have complex Icons so I have to use it as point features). Is there any solution to it? Like in QGIS: MAP UNITS.
我已经拥有这些:
var jelekStyle = function(feature, resolution) {
if(feature.get('tipus')=== 'falu') {
icon = '00_ikonok/falu.png',
size = [115, 233],
scale = 0.05,
anchor = [0.5,46];
} else if(feature.get('tipus')=== 'puszta') {
image = '00_ikonok/puszta.png';
} ...
}
return [new ol.style.Style({
image: new ol.style.Icon({
src: icon,
scale: scale,
size: size,
anchor: anchor,
anchorXUnits: 'fraction',
anchorYUnits: 'pixels'
})
})];
};
...
var jelek = new ol.layer.Vector({
source: new ol.source.Vector({
url: 'sopron_honlap/json/Gorog-Kerekes_Sopron_jelek_GeoJSON.geojson',
format: new ol.format.GeoJSON()
}),
updateWhileAnimating: true,
updateWhileInteracting: true,
style: jelekStyle
});
推荐答案
是的,有一个解决方案。在图层上的样式
函数中,您必须按参考分辨率除以渲染分辨率来缩放图标大小。
Yes, there is a solution. In a style
function on the layer, you have to scale your icon size by a reference resolution divided by the render resolution.
要在用户交互期间更新样式,请使用 updateWhileInteracting配置图层:true
和 updateWhileAnimating:true
。以下是整个代码:
To update the style even during user interaction, configure your layer with updateWhileInteracting: true
and updateWhileAnimating: true
. Here is the whole code:
var iconFeature = new ol.Feature(new ol.geom.Point([0, 0]));
var iconStyle = new ol.style.Style({
image: new ol.style.Icon({
anchor: [0.5, 46],
anchorXUnits: 'fraction',
anchorYUnits: 'pixels',
src: 'https://openlayers.org/en/v4.3.2/examples/data/icon.png'
})
});
var vectorSource = new ol.source.Vector({
features: [iconFeature]
});
var vectorLayer = new ol.layer.Vector({
source: vectorSource,
updateWhileAnimating: true,
updateWhileInteracting: true,
style: function(feature, resolution) {
iconStyle.getImage().setScale(map.getView().getResolutionForZoom(3) / resolution);
return iconStyle;
}
});
var rasterLayer = new ol.layer.Tile({
source: new ol.source.TileJSON({
url: 'https://api.tiles.mapbox.com/v3/mapbox.geography-class.json?secure',
crossOrigin: ''
})
});
var map = new ol.Map({
layers: [rasterLayer, vectorLayer],
target: 'map',
view: new ol.View({
center: [0, 0],
zoom: 3
})
});
html, body, #map {
margin: 0;
width: 100%;
height: 100%;
}
<!DOCTYPE html>
<head>
<link rel="stylesheet" href="https://openlayers.org/en/v4.3.2/css/ol.css">
<script src="https://openlayers.org/en/v4.3.2/build/ol.js"></script>
</head>
<body>
<div id="map" class="map"></div>
</body>
这篇关于修复了OpenLayers 3中的图标大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!