简体   繁体   English

如何使用bbox加载Open Layers 3 geojson矢量图层?

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

I am struggling with building OL3 Vector layer BBOX strategy loading. 我正在努力构建OL3 Vector层BBOX策略加载。 So far I can easily load Geojson file with valid json syntax, however this is one time strategy. 到目前为止,我可以轻松地使用有效的json语法加载Geojson文件,但这是一次性策略。 My another approach was to use ol.ServerVector which to my understading returns Javascript with callback, but I can't make it work. 我的另一种方法是使用ol.ServerVector,它对我的​​理解很容易返回带有回调的Javascript,但是我无法使其工作。

Working simple Geojson layer: 工作简单的Geojson层:

var vectorSource = new ol.source.GeoJSON(
({
  projection: 'EPSG:3857',
  preFeatureInsert: function(feature) {
    feature.geometry.transform('EPSG:4326', 'EPSG:3857');
  },
  url: 'geojson2.json'
}));

var vectorLayer = new ol.layer.Vector({ source: vectorSource, style: styleFunction }); var vectorLayer = new ol.layer.Vector({ source: vectorSource, style: styleFunction });

BBOX attempt (This is returning json while moving , however features are not loading to the map ) : BBOX尝试(这是在移动时返回json,但是功能没有加载到地图上):

    var vectorSource = new ol.source.ServerVector({
  format: new ol.format.GeoJSON(),
  loader: function(extent, resolution, projection) {
    var url = 'geojson2.php?p='+
        extent.join(',');
    $.ajax({
      url: url
    });
  },
  strategy: ol.loadingstrategy.bbox,
  projection: 'EPSG:3857',

});
// callback ?
var loadFeatures = function(response) {
  vectorSource.addFeatures(vectorSource.readFeatures(response));
};

JSON response example: JSON响应示例:

{"type":"FeatureCollection","features":[
{"type":"Feature","geometry":{"type":"Point","coordinates":[0,0]},"properties":{"label":"122.234-10/163"}},
{"type":"Feature","geometry":{"type":"Point","coordinates":[1,1],"properties":{"label":"132.222-1126"}}}
]}

To get this working with the newest version of OL3 (v3.7.0) I had to read the features using the GeoJSON format class. 为了使用最新版本的OL3(v3.7.0),我必须使用GeoJSON格式类来阅读这些功能。

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
});

You need to add an Ajax callback that adds the features to the vector source: 您需要添加一个Ajax回调,以将功能部件添加到矢量源中:

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
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM