设置样式缩放级别openlayers 3

Fun*_*bby 2 javascript openlayers openlayers-3

在Openlayers中,可以根据缩放级别打开或关闭某些功能.尽管查看了文档,我还没有在OpenLayers 3中找到相同的功能.有谁知道如何做到这一点?这是我放在地图上的功能ol.style.Text,只有在用户放大到特定缩放级别后才能显示.

var geoJsonObj = {
  'type': 'Feature',
  'geometry': JSON.parse(response.FieldList[key].Shape)
}
var vectorSource = new ol.source.Vector({
  features: (new ol.format.GeoJSON()).readFeatures(geoJsonObj)
});
Fields[Field.FieldID] = new ol.layer.Vector({
  projection: 'EPSG:4269',
  source: vectorSource,
  style: new ol.style.Style({
    stroke: new ol.style.Stroke({
      color: 'yellow',
      width: 1
    }),
    fill: new ol.style.Fill({
      color: rcisWebMapUtilities.convertHex(response.FieldList[key].Shade, '0.5')
    }),
    text: new ol.style.Text({
      textAlign: 'Center',
      text: response.FieldList[key].Acres,
      scale: 1
    })
  })
});
Run Code Online (Sandbox Code Playgroud)

Tim*_*aub 8

矢量图层示例演示了使用样式函数进行分辨率相关的样式设置:http://openlayers.org/en/latest/examples/vector-layer.html

这是一个简化版本:

var layer = new ol.layer.Vector({
  source: new ol.source.Vector(),
  style: function(feature, resolution) {
    var text = resolution < 5000 ? feature.get('name') : '';
    return new ol.style.Style({
      text: new ol.style.Text({
        text: text,
        fill: new ol.style.Fill({
          color: '#000'
        }),
        stroke: new ol.style.Stroke({
          color: '#f00',
          width: 3
        })
      })
    });
  }
});
Run Code Online (Sandbox Code Playgroud)

上面的图层将以name低于每个像素5000个地图单​​位的分辨率渲染要素.

上面的向量层示例有一些额外的代码可以在可能的情况下重用样式.如果你有很多功能,你可以通过为每个渲染帧创建新的样式实例来对垃圾收集器施加过大的压力.