OpenLayers 搭建跨域代理(WFS)
1、下载proxy.cgi文件,url:http://svn.openlayers.org/trunk/openlayers/examples/proxy.cgi。
2、安装python,url:http://python.org/。
3、在地图项目的web.xml中添加如下配置:
<servlet> <servlet-name>cgi</servlet-name> <servlet-class>org.apache.catalina.servlets.CGIServlet</servlet-class> <init-param> <param-name>debug</param-name> <param-value>0</param-value> </init-param> <init-param> <param-name>cgiPathPrefix</param-name> <param-value>WEB-INF/cgi</param-value> </init-param> <init-param> <param-name>executable</param-name> <!-- your python install path--> <param-value>c:/Python27/python.exe</param-value> </init-param> <init-param> <param-name>passShellEnvironment</param-name> <param-value>true</param-value> </init-param> <load-on-startup>5</load-on-startup> </servlet> <!-- The mapping for the default servlet--> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>cgi</servlet-name> <url-pattern>/cgi/*</url-pattern> </servlet-mapping>
4、在Tomcat的配置文件中找到context.xml做如下修改:
<Context privileged="true"></Context>
或者copy 该文件至META-INF下做如上修改。
5、在WEB-INF下创建文件夹cgi,将下载的proxy.cgi copy到此文件夹。
6、修改proxy.cgi文件,设置代理的服务器IP和端口:
allowedHosts = [ 'openstreetmap.cz', 'www.openstreetmap.cz', 'www.openlayers.org', 'openlayers.org', 'labs.metacarta.com', 'world.freemap.in', 'prototype.openmnnd.org', 'geo.openplans.org', 'sigma.openplans.org', 'www.openstreetmap.org', 'toolserver.org', "osmxapi.hypercube.telascience.org", "nominatim.openstreetmap.org", "open.mapquestapi.com", "routes.cloudmade.com", "192.168.0.32:8990" ]
7、验证测试是否成功:
在python安装目录中找到并启动python.exe, 访问:http://localhost:8080/cgi-bin/proxy.cgi 出现home page 则说明搭建代理成功。
8、使用远程访问的代理:
map.events.register('click', map, function (e) { OpenLayers.Util.getElement('nodelist').innerHTML = "Loading... please wait..." + map.layers[0].name; var url = map.layers[0].getFullRequestString({ REQUEST: "GetFeatureInfo", EXCEPTIONS: "application/vnd.ogc.se_xml", BBOX: map.getExtent().toBBOX(), X: e.xy.x, Y: e.xy.y, INFO_FORMAT: 'text/html', QUERY_LAYERS: map.layers[0].params.LAYERS, FEATURE_COUNT: 50, layers: 'topp:danwei_font_point', styles: '', srs: 'EPSG:4326', WIDTH: map.size.w, HEIGHT: map.size.h}, http://192.168.0.32:8990/geoserver/wms ); OpenLayers.loadURL(url, '', this, setHTML, setHTML); Event.stop(e); });
其中192.168.0.32::8990/geoserver/wms为获取代理服务器地址。
纸上得来终觉浅,绝知此事要躬行。