问题描述
我正在使用geoserver和openlayers 3,我在geoserver中有一个托管层,我想使用ol3作为wfs层进行显示.它没有显示任何错误,但是我没有得到地图.它适用于wms,但不适用于wfs.我正在使用Wamp服务器托管我的应用程序.
I'm working with geoserver and openlayers 3, i have a hosted layer in geoserver that i want display using ol3 as a wfs layer. it doesn't show any errors, but i dont get the map. it works with wms, but not wfs.I'm using wamp server to host my application.
请有人能帮我解决这个问题吗?这是我的代码:
Please can anyone help me to fix that? This is my code :
var v=new ol.layer.Vector({
source: new ol.source.Vector({
format: new ol.format.WFS({
version: '1.1.0',
url: 'http://localhost:8080/geoserver/wfs',
featurePrefix: 'opengeo',
featureType: 'comgeo',
featureNS: 'http://localhost:8080/opengeo',
geometryName: 'geom'
})
})
});
var map = new ol.Map({
target: 'map-id',
layers: [v],
view: new ol.View({
projection: 'EPSG:4326',
center: [0, 0],
zoom: 1
})
});
我在论坛上阅读了我需要设置代理的信息,我做到了,并且我的代理工作正常,但我不知道如何在我的代码中使用它(Openlayers.ProxyHost).当我使用Firebug时,在网络/图像"选项卡下,我可以看到wms请求,但不适用于wfs.预先感谢.
i read in forums that i need to set proxy, i did that and my proxy works fine, but i dont know how to it use in my code (Openlayers.ProxyHost).When i use firebug, under network/images tab, i can see wms requests, but not for wfs.Thanks in advance.
推荐答案
最后,我解决了我的问题,所以我想与您分享解决方案.
Finally, I solved my problem, so I want to share the solution with you.
第一件事,在我之前的代码中有一些错误,这是设置wfs
层的正确方法:
First thing there were some mistakes in my previous code, this the right way to set a wfs
layer :
var u = "http://localhost/geoserver/wfs?&service=wfs&version=1.1.0&request=GetFeature&typeNames=opengeo:comgeo";
var v = new ol.layer.Vector({
title: 'comgeo',
source: new ol.source.Vector({
url: '/cgi-bin/proxy.cgi?url='+ encodeURIComponent(u),
format: new ol.format.WFS({
})
})
});
u
是wfs请求,参数值opengeo
是geoserver中的工作空间,而comgeo
是图层.
u
is the wfs request, the paramvalue opengeo
is the workspace in geoserver, and comgeo
is the layer.
您需要使用encodeURIComponent
函数转义此url,这样您就不会像这样出现著名的错误:
You need to escape this url by using encodeURIComponent
function, so you don't get the famous error like :
我的网址/cgi-bin/proxy.cgi?url=
的第一部分是因为如果您的应用程序是从另一个位置运行的,则需要一个代理(我使用的是wamp
来托管我的).
The first part of my url /cgi-bin/proxy.cgi?url=
is that because you need a proxy if your application is running from another location (I'm using wamp
to host mine).
要使您的代理正常工作,请尝试以下教程:设置代理
To get your proxy working try this tutorial:set proxy
使用代理,就像在我的代码中一样
Use the proxy like in my code, this
OpenLayers.ProxyHost = "/cgi-bin/proxy.cgi?url=";
在ol3
中不可用.
这篇关于在ol3中显示WFS图层的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!