首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在OpenLayers 3中固定图标大小

在OpenLayers 3中固定图标大小
EN

Stack Overflow用户
提问于 2017-09-05 11:16:46
回答 1查看 4.2K关注 0票数 2

我搜索了2个小时,但是还不清楚在OL3中是否有可能。我希望我的图标是固定的大小(不是到屏幕上,而是在我使用的图像地图上)。我的意思是,它应该覆盖相同的区域,即使我放大了,而不是覆盖一半的地图(就像我使用的是一个圆形多边形,但我有复杂的图标,所以我必须使用它作为点特征)。有什么解决办法吗?就像QGIS:地图单位。

我已经有了这些:

代码语言:javascript
复制
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'
                        })
                  })];
                };

..。

代码语言:javascript
复制
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
                });
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-09-07 00:00:08

是的,有个解决办法。在该层的style函数中,您必须通过引用分辨率除以呈现分辨率来缩放图标大小。

若要在用户交互期间更新样式,请使用updateWhileInteracting: trueupdateWhileAnimating: true配置您的层。以下是整个代码:

代码语言:javascript
复制
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
  })
});
代码语言:javascript
复制
html, body, #map {
  margin: 0;
  width: 100%;
  height: 100%;
}
代码语言:javascript
复制
<!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>

票数 11
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46053640

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档