openlayers-3 - ol3/Openlayers3 : change radius of circle when zoomed

标签 openlayers-3

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

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

我希望半径根据 map 缩放级别动态变化 - 例如:

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

最佳答案

您可以收听分辨率更改:

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

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

关于openlayers-3 - ol3/Openlayers3 : change radius of circle when zoomed,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31024715/

相关文章:

html5-canvas - 开放层 3 : Scaling canvas with text with 125% DPI since v4. x

javascript - 如何在 Openlayers 3 中启用 "Snapping"与 "Select"交互

javascript - 如何在功能悬停时获得指针光标,但前提是未被弹出窗口覆盖?

google-maps - 打开第 3 层搜索功能以查找 map 位置?

openlayers-3 - 开放层 3 : Change the vectors layers when the zoom level change

twitter-bootstrap - 从 openlayers3 检测鼠标悬停在弹出窗口上

javascript - 具有自定义切片服务器的 Openlayers3

javascript - 转换 ol.geom.Geometry 的布局

javascript - 来自 geojson 的 Openlayers 3 圆不适用于与 TileWMS 不同的图层图 block 源

javascript - 修改交互时取消监听事件并在监听后重新激活