How to load Open layers 3 geojson vector layer with bbox?

前端 未结 2 1944
闹比i
闹比i 2020-12-19 19:23

I am struggling with building OL3 Vector layer BBOX strategy loading. So far I can easily load Geojson file with valid json syntax, however this is one time strategy. My ano

相关标签:
2条回答
  • 2020-12-19 19:39

    You need to add an Ajax callback that adds the features to the vector source:

    var vectorSource = new ol.source.Vector({
      format: new ol.format.GeoJSON(),
      loader: function(extent, resolution, projection) {
        var url = 'geojson2.php?p=' + extent.join(',');
        $.ajax({
          url: url,
          success: function(data) {
            vectorSource.addFeatures(vectorSource.readFeatures(data));
          }
        }); 
      },
      projection: 'EPSG:3857',
      strategy: ol.loadingstrategy.bbox
    });
    
    0 讨论(0)
  • 2020-12-19 19:40

    To get this working with the newest version of OL3 (v3.7.0) I had to read the features using the GeoJSON format class.

    var geoJSONFormat = new ol.format.GeoJSON();
    
    var vectorSource = new ol.source.Vector({
      loader: function(extent, resolution, projection) {
        var url = 'geojson2.php?p=' + extent.join(',');
        $.ajax({
          url: url,
          success: function(data) {
            var features = geoJSONFormat.readFeatures(data);
            vectorSource.addFeatures(features);
          }
        }); 
      },
      strategy: ol.loadingstrategy.bbox
    });
    
    0 讨论(0)
提交回复
热议问题