0

I am using GeoJSON Vector layer that is loaded dynamically through a call to a server api method. This basically works, but it potentially loads a lot of features, so I want to limit the zoom level so it does not load when zoomed too far out.

I try to set maxResolution on the config, but this does not seem to have any effect.

Here is my layer source definition:

  var vectorFormat = new ol.format.GeoJSON()
  var vectorSource = new ol.source.Vector({
    maxResolution: 1.2,
    strategy: ol.loadingstrategy.bbox,
    loader: function (extent, resolution, projection) {
      var transformedExtent = ol.proj.transformExtent(extent, projection, 'EPSG:4326')
      self.loadGeoHashesJson(transformedExtent).then(function (response) {
        console.log('loadGeoHashesJson response:', response)
        var features = vectorFormat.readFeatures(response.body, { dataProjection: 'EPSG:4326', featureProjection: projection })
        vectorSource.addFeatures(features)
      }, function (response) {
        console.error('error getting data from server', response)
      })
    }
  })

I also tried to check on the resolution inside the loader method, and skip loading the data if the resolution is larger than the maxResolution. This works initially, but it does not trigger load immediately when zoomed in to a valid resolution. I have to pan about a bit to get to an area that has not been touched earlier for it to load again.

How can I get this to work so that the maxResolution is actively stopping the layer from loading when resolution is above maxResolution, and actually loading when the resolution goes below maxResolution?

0

The maxResolution setting should be on the layer config, NOT the source config!

So when I moved the maxResolution setting to the initialization of the vector layer, it worked perfectly:

var vectorLayer = new ol.layer.Vector({
    title: 'GeoHash',
    maxResolution: 1.2,
    source: vectorSource
  })

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.