我有一个矢量图层,其样式当前定义为:
var styles = new ol.style.Style({
image: new ol.style.Circle({
radius: 4,
fill: new ol.style.Fill({color: 'red'}),
stroke: new ol.style.Stroke({color: 'black', width: 1})
})
我想根据地图缩放级别动态更改半径 - 例如:
半径:(变焦/ 2)+1
我将如何这样做?
更新:Jonatas'评论帮助引导我朝着正确的方向发展。我最终使用了以下内容:
map.getView().on('change:resolution', function(evt) {
var zoom = map.getView().getZoom();
var radius = zoom / 2 + 1;
var newStyle = new ol.style.Style({
image: new ol.style.Circle({
radius: radius,
fill: new ol.style.Fill({color: 'red'}),
stroke: new ol.style.Stroke({color: 'black', width: 1})
})
})
vectorLayer.setStyle(newStyle);
});
答案 0 :(得分:4)
您可以收听分辨率更改:
map.getView().on('change:resolution', function(evt){
//according to http://openlayers.org/en/v3.6.0/apidoc/ol.View.html
// I think this is not true for any scenario
//40075016.68557849 / 256 / Math.pow(2, 28) = 0.0005831682455839253
var resolution = evt.target.get(evt.key),
resolution_constant = 40075016.68557849,
tile_pixel = 256;
var result_resol_const_tile_px = resolution_constant / tile_pixel / resolution;
var currentZoom = Math.log(result_resol_const_tile_px) / Math.log(2);
console.info(currentZoom, resolution);
//now find your features and apply radius
feature.getStyle().getGeometry().setRadius(radius);
});
请注意,我正在将分辨率转换为缩放,但这只是一种好奇心。您可以摆脱它并根据分辨率设置半径。
答案 1 :(得分:1)
缩放时使用缩放基础调整半径。
map.getCurrentScale = function () {
//var map = this.getMap();
var map = this;
var view = map.getView();
var resolution = view.getResolution();
var units = map.getView().getProjection().getUnits();
var dpi = 25.4 / 0.28;
var mpu = ol.proj.METERS_PER_UNIT[units];
var scale = resolution * mpu * 39.37 * dpi;
return scale;
};
map.getView().on('change:resolution', function(evt){
var divScale = 60;// to adjusting
var radius = map.getCurrentScale()/divScale;
feature.getStyle().getGeometry().setRadius(radius);
});