2016-08-02 113 views
0

我正在从服务器加载功能并将它们添加为矢量图层的源。功能未显示

var map = new ol.Map({ 
    target: 'map', 
    interactions: ol.interaction.defaults().extend([ 
     new ol.interaction.DragRotate() 
    ]), 
    controls: ol.control.defaults().extend([ 
     new ol.control.FullScreen(), 
     new ol.control.ScaleLine() 
    ]), 
    layers: [ 
     new ol.layer.Tile({ 
      source: new ol.source.OSM() 
     }), 
     new ol.layer.Tile({ 
      source: new ol.source.TileWMS({ 
       url: 'https://server.com/wms?', 
       params: { 
        'LAYERS': 'operational_structures', 
        'TILED': true 
       } 
      }), 
      maxResolution: 500, 
      opacity: 0.3 
     }), 
     new ol.layer.Vector({ 
      source: new ol.source.Vector({ 
       url: function (extent, resolution, projection) { 
        return 'https://server/wms?Request=GetFeature&BBOX=' 
         + extent.join(','); 
       }, 
       format: new ol.format.GeoJSON(), 
       strategy: ol.loadingstrategy.bbox 
      }), 
      style: new ol.style.Style({ 
       image: new ol.style.Circle({ 
        radius: 10, 
        fill: new ol.style.Fill({color: 'rgba(255, 0, 0, 0.1)'}), 
        stroke: new ol.style.Stroke({color: 'red', width: 1}) 
       }) 
      }) 
     }) 
    ], 
    view: new ol.View({ 
     center: ol.proj.fromLonLat([0, 0]), 
     zoom: 4 
    }) 
}); 

一切工作正常,取得所有功能。我可以使用layer.getSource().getFeatures()访问它们。我的数据是这样的:

{ 
    "type": "FeatureCollection", 
    "features": [ 
    { 
     "type": "Feature", 
     "properties": { 
      "id": 1, 
      "ref": "GIS_af05" 
     }, 
     "geometry": { 
      "type": "Point", 
      "coordinates": [ -119383.2138463442, 7156374.7828825945 ] 
     } 
    }, 
    { 
     "type": "Feature", 
     "properties": { 
      "id": 2, 
      "ref": "GIS_af06" }, 
     "geometry": { 
      "type": "Point", 
      "coordinates": [ -117573.06816312684, 7163838.359699009 ] 
     } 
    }, 
    { 
     "type": "Feature", 
     "properties": { 
      "id": 3, 
      "ref": "GIS_af21" }, 
     "geometry": { 
      "type": "Point", 
      "coordinates": [ -128431.22137966838, 7169061.1280527115 ] 
     } 
    }] 
} 

但是因为不明原因他们不会出现。我错过了什么吗?


编辑:

所以我发现它搞乱与预测。它会尝试将要素坐标从ESPG:4326转换为ESPG:3857。问题是,他们已经是ESPG:3857。我怎样才能防止呢?

+0

你能提供的代码的其余部分?特别是地图初始化。 – ohvitorino

+0

你不应该在你的矢量图层中调用http:// server/wfs而不是wms吗? –

+0

@HichamZouarhi这只是一个API名称。其实我打电话给'server/wmf?Service = wfs&...',但是我把它丢掉了,因为它不值一提。正如我所说:数据被认为是geoJSON,我可以访问从它加载的'ol.Feature'对象。 – gerric

回答

1

我终于把它运行了。

问题是,从GIS服务器返回的数据没有指定其投影。所以Openlayers3只使用EPSG:4326作为默认值。

经过对文档的长时间搜索后,我发现,我可以在ol.format.GeoJSON中设置默认投影(没想到在那里)。

该解决方案是这样的:

new ol.layer.Vector({ 
    source: new ol.source.Vector({ 
     url: function (extent, resolution, projection) { 
      return 'https://server/wms?Request=GetFeature&BBOX=' 
       + extent.join(','); 
     }, 
     format: new ol.format.GeoJSON({ 
      defaultDataProjection: 'EPSG:3857' // added line 
     }), 
     strategy: ol.loadingstrategy.bbox 
    }), 
    ... 
})