代码之家  ›  专栏  ›  技术社区  ›  Reka Varga

OpenLayers 3中的固定图标大小

  •  2
  • Reka Varga  · 技术社区  · 7 年前

    我希望我的图标大小是固定的(不是在屏幕上,而是在我使用的图像地图上)。我的意思是,即使缩小,它也应该覆盖相同的区域,并且不覆盖地图的一半(就像我使用的是圆形多边形,但我有复杂的图标,所以我必须将其用作点特征)。有什么解决办法吗? 比如QGIS:地图单位。

    我已经有了这些:

    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
                    });
    
    1 回复  |  直到 7 年前
        1
  •  10
  •   ahocevar    7 年前

    是的,有一个解决方案。在一个 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>