无法将功能添加到WFS层

问题描述:

我尝试从Geoserver将某些功能加载到Openlayers 3.9.0中的矢量图层。无法将功能添加到WFS层

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

对WFS的请求看起来没问题,OK 200 status。如果我抓住请求的URL发送到Geoserver并打开它在一个新的选项卡我得到原始GeoJSON像{"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而不只是读取/添加功能。我不知道如何调试功能并将其添加到我的图层

+1

请参见[56,“问题包括‘标签’,在他们的头衔?”(http://meta.stackexchange.com/questions/19190/should - 问题 - 包括标签在他们的标题),其*识是“不,他们不应该”! –

您正在指示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 
});