【问题标题】:ol3 / Openlayers3: change radius of circle when zoomedol3 / Openlayers3:缩放时改变圆的半径
【发布时间】:2015-06-24 10:56:30
【问题描述】:

我有一个矢量图层,其样式当前定义为:

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})
})

我希望根据地图缩放级别动态更改半径 - 类似于:

半径:(zoom/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);
});

【问题讨论】:

    标签: openlayers-3


    【解决方案1】:

    您可以收听分辨率变化:

    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);
    });
    

    请注意,我正在将分辨率转换为缩放,但这只是好奇。您可以摆脱它并根据分辨率设置半径。

    【讨论】:

    • 谢谢——这帮助我开始了。
    【解决方案2】:

    缩放时使用比例尺调整半径大小。

    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);
    });
    

    【讨论】:

    • scale 计算中的 39.37 是多少?
    • 39.37 获得每米英寸数
    • @Tareqboudalia 效果很好,完全符合预期。
    • @formatc 谢谢,如果你对答案投票,我会很高兴
    猜你喜欢
    • 2016-03-18
    • 1970-01-01
    • 1970-01-01
    • 2018-07-26
    • 1970-01-01
    • 2019-08-20
    • 1970-01-01
    • 2016-08-17
    • 1970-01-01
    相关资源
    最近更新 更多