gpt4 book ai didi

javascript - 如何使用 bbox 加载 Open layers 3 geojson 矢量图层?

转载 作者:行者123 更新时间:2023-12-01 09:24:53 24 4
gpt4 key购买 nike

我正在努力构建 OL3 矢量层 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({
来源:vectorSource,
风格:styleFunction
});

BBOX 尝试(这是在移动时返回 json,但功能未加载到 map ):

    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"}}}
]}

最佳答案

为了让它与最新版本的 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
});

关于javascript - 如何使用 bbox 加载 Open layers 3 geojson 矢量图层?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26844412/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com