Fixed icon size in OpenLayers 3

2019-06-06 16:06发布

问题:

I searched for 2 hours now, but it's still not clear if it's possible or not in OL3. I would like my icons to be fixed size (not to the screen, but to the image map I'm using). I mean, it should cover the same area even if I'm zoomed out, and not covering the half of the map (like I was using a circle polygon, but I have complex Icons so I have to use it as point features). Is there any solution to it? Like in QGIS: MAP UNITS.

I already have these:

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:

Yes, there is a solution. In a style function on the layer, you have to scale your icon size by a reference resolution divided by the render resolution.

To update the style even during user interaction, configure your layer with updateWhileInteracting: true and updateWhileAnimating: true. Here is the whole code:

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>