【发布时间】:2015-02-02 08:36:02
【问题描述】:
我有一个关于 ol3 中矢量加载的问题。
我正在使用不处理 js 回调
的地理服务器为了加载我的功能,我必须将它们添加到加载器的 ajax done 函数中:
var buildLoader = function(vectorSource, $vector) {
return function(extent, resolution, projection) {
extent = ol.extent.applyTransform(extent, ol.proj.getTransform("EPSG:3857", "EPSG:4326"));
var url = 'https://myurl/' + $vector.attr("url") +
'?service=WFS' +
'&version=' + $vector.attr("version") +
'&request=GetFeature' +
'&typename=' + $vector.attr("typename") +
'&srs=' + $vector.attr("projection") +
'&bbox=' + extent +
'&outputFormat=json';
$.ajax({
url: url,
dataType: 'json',
xhrFields: {
withCredentials: true
}
})
.done(function(response) {
vectorSource.addFeatures(vectorSource.readFeatures(response));
});
}
}
然后我构建一个向量数组以添加到我的地图上。
for (index=0; index < vectors.length; index++) {
var $vector = $(vectors[index]);
var vectorSource = new ol.source.ServerVector({
format: new ol.format.GeoJSON(),
loader: buildLoader(vectorSource, $vector),
projection: 'EPSG:3857'
});
// ... //
datas[index] = new ol.layer.Vector({
source: vectorSource,
visible: 'false',
style: iconStyle
});
}
这里的问题是loader使用的vectorSource总是[index - 1]
我发现了一个Hack,通过在实例化之后定义loader函数:
var vectorSource = new ol.source.ServerVector({
format: new ol.format.GeoJSON(),
projection: 'EPSG:3857'
});
vectorSource.loader_ = buildLoader(vectorSource, $vector);
我觉得这很丑陋,但是没有可用于 ServerVector 类型的 setLoader()。您知道是否有其他解决方案不涉及使用其他地理服务器?
【问题讨论】:
标签: openlayers-3