我正在努力构建OL3 Vector层BBOX策略加载。到目前为止,我可以轻松地使用有效的json语法加载Geojson文件,但这是一次性策略。我的另一种方法是使用ol.ServerVector,我的底层返回Javascript回调,但我不能让它工作。
使用简单的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
});
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响应示例:
{"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"}}}
]}
答案 0 :(得分:4)
为了使用最新版本的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
});
答案 1 :(得分:3)
您需要添加一个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
});