openlayer常见问题

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.csdn.net/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


转载自:https://blog.csdn.net/xk_zhang/article/details/50435270

You may also like...