我尝试将Geoserver中的一些功能加载到Openlayers 3.9.0中的矢量图层。
var url = 'http://localhost:5550/geoserver/mymap/wfs?service=WFS&'+'version=1.0.0&request=GetFeature&typeName=mymap:layer&'+'outputFormat=application/json&maxFeatures=50';
var projection = ol.proj.get('EPSG:3857');
var extent = [2297128.5, 4618333, 2459120.25, 4763120];
var amir = new ol.source.Vector({
format: new ol.format.GeoJSON(),
loader: function (extent) {
$.ajax(url, {type: 'GET'})
.done(loadFeatures)
.fail(function () {alert("error");});
},
strategy: ol.loadingstrategy.bbox
});
function loadFeatures(response) {
formatWFS = new ol.format.WFS();
var features = formatWFS.readFeatures(response);
amir.addFeatures(features);
//-----------OR---------------------
var features = amir.readFeatures(response);
amir.addFeatures(features);
}
var fill = new ol.style.Fill({
color: 'rgba(0,0,0,0.2)'
});
var stroke = new ol.style.Stroke({
color: 'rgba(0,0,0,0.4)'
});
var circle = new ol.style.Circle({
radius: 6,
fill: fill,
stroke: stroke
});
jake = new ol.layer.Vector({
source: amir,
style: new ol.style.Style({
fill: fill,
stroke: stroke,
image: circle
})
});
如果我使用
,则在loadFeatures
函数中
formatWFS = new ol.format.WFS();
var features = formatWFS.readFeatures(response);
amir.addFeatures(features);
我Uncaught AssertionError: Failure: Unknown source type
指向一个openlayers行,它会抛出错误并导致我的代码行var features = formatWFS.readFeatures(response);
。
如果我使用
var features = amir.readFeatures(response);
amir.addFeatures(features);
我Uncaught TypeError: sourceVector.readFeatures is not a function
指向var features = amir.readFeatures(response);
。
使用OK 200 status
对WFS的请求看起来不错。如果我抓住发送给Geoserver的请求的URL并在新标签中打开它,我会获得像{"type":"FeatureCollection","totalFeatures":422,"features":[{"type":"Feature","id":"layer.709","geometry":{"type":"Point","coordinates":[2391735.8907621,4695330.8039257005]},"geometry_name":"l_geom","properties":{"l_name":"Leeron"}},....//next feature
所以它的FeatureCollection
不仅仅是一个数组。不是我知道如何处理这个
我不明白为什么要设置ol.format.WFS
而不只是阅读/添加功能。我不知道如何调试并将功能添加到我的图层
答案 0 :(得分:2)
您正在指示GeoServer使用GeoJSON作为输出格式,因此您需要在OpenLayers中使用GeoJSON格式来解析这些功能。您应该能够将源配置简化为
var url = 'http://localhost:5550/geoserver/mymap/wfs?service=WFS&' +
'version=1.0.0&request=GetFeature&typeName=mymap:layer&' +
'outputFormat=application/json&maxFeatures=50';
var amir = new ol.source.Vector({
format: new ol.format.GeoJSON(),
url: function(extent, projection) {
return url + '&bbox=' + extent.join(',') +
'&srsName=' + projection.getCode();
},
strategy: ol.loadingstrategy.bbox
});