openlayer常见问题

本文介绍了如何解决使用OpenLayers在Java Web项目中跨域访问Geoserver WFS服务时遇到的CORS问题,通过设置过滤器配置允许所有域的跨域资源共享,并提供了OpenLayers中不同版本的优化方法,如地图范围放大和基于WFS的模糊查询优化。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.跨域访问WFS问题

找到的OpenLayers跨域访问WFS服务出现No 'Access-Control-Allow-Origin' header is present...错误的一种解决办法

在eclipse中部署了一个使用OpenLayers的Java web项目CrossDomainTest,容器为Tomcat 7.0.56.(http://localhost:8080/CrossDomainTest/index.html)

利用Geoserver 2.5.2使用内置容器jetty 6.18发布WFS服务.(http://localhost:8888/geoserver/wfs).

从localhost:8080访问localhost:8888的WFS无法得到预期效果,通过chrome浏览器开发者工具发现错误:No 'Access-Control-Allow-Origin' header is present on the requested resource. 查阅发现是所谓的Javascript安全性导致的“跨域问题”,找了很多的解决方法,比如设置cgi代理,CORS(跨域资源共享)等我都尝试了但不知为何没有成功,限于时间关系没有仔细追究,最后找到[Geoserver-users] CORS for jetty 6.1.8 (Geoserver 2.x), solved这篇文章,按其设置解决了问题:


解决方案步骤为:
1.从这里下载ZIP文件,解压后放到<Geoserver>\webapps\geoserver\WEB-INF\classes文件夹中。
2.向<Geoserver>\webapps\geoserver\WEB-INF\文件夹中的web.xml文件中增加如下配置文件允许所有域的跨域资源共享

<filter>  
     <filter-name>cross-origin</filter-name>  
     <filter-class>org.mortbay.servlets.CrossOriginFilter</filter-class>  
 <init-param>  
<param-name>allowedOrigins</param-name>  
<param-value>*</param-value>  
 </init-param>  
     <init-param>  
      <param-name>allowedMethods</param-name>  
      <param-value>GET,POST</param-value>  
     </init-param>  
     <init-param>  
      <param-name>allowedHeaders</param-name>  
      <param-value>x-requested-with,content-type</param-value>  
     </init-param>  
   </filter>  
<filter-mapping>  
       <filter-name>cross-origin</filter-name>  
       <url-pattern>/*</url-pattern>  
   </filter-mapping>  

详见:http://blog.youkuaiyun.com/shulitang/article/details/41520353

http://sourceforge.net/p/geoserver/mailman/message/32391594/

2.以某个要素(feature)的范围extent放大

openlayers2 中的方法为 map.zoomToExtent(extent);
ol3 中不在使用以上方法,改用
var extent = source.getExtent();
map.getView().fitExtent(extent, map.getSize());

3.vector source returning empty in source.getFeatures()

When you pass an url param, ol.source.Vector is asynchronously (AJAX) loaded so you have to wait until it's fully loaded with:
vectorSource.on('change', function(evt){
    if(source.getState() === 'ready'){
        console.info(vectorSource.getFeatures());
    }
});
详见: http://stackoverflow.com/questions/32561718/vector-source-returning-empty-in-source-getfeatures

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值