在OpenLayers 3中修复了图标大小

瓦尔加河

我现在搜索了2个小时,但仍不清楚是否可以在OL3中进行搜索。我希望图标的大小固定(不是屏幕大小,而是我正在使用的图像地图)。我的意思是,即使我被缩小了,它也应该覆盖相同的区域,而不是覆盖地图的一半(就像我使用的是圆形多边形,但是我有复杂的Icon,所以必须将其用作点要素)。有什么解决办法吗?就像在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
                });
ho

是的,有解决方案。style图层功能中,您必须按参考分辨率除以渲染分辨率来缩放图标大小。

要即使在用户交互过程中也要更新样式,请使用updateWhileInteracting: true配置图层updateWhileAnimating: true这是整个代码:

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>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章