OpenLayers+天地图+Geoserver+WMS图层服务

本文介绍如何使用OpenLayers框架集成天地图作为底图,并展示如何接入GeoServer提供的WMS图层服务。文章通过示例代码演示了天地图不同图层(路网、卫星影像、文字标注)的加载过程,以及如何配置GeoServer WMS服务参数实现自定义图层的显示。

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

OpenLayers+天地图+Geoserver+WMS图层服务

1.WEB项目集成OpenLayers

  版本v3.19.0,下载 v3.19.0-dist.zip;最新版本为v4.0.1

  网址: http://openlayers.org/


2.样例代码:

<script type="text/javascript" src="../js/ol-debug.js"></script>
<link rel="stylesheet" type="text/css" href="../css/ol.css" />


function init(){
            var layers=[      
                    new ol.layer.Tile({      
                         title: "天地图路网",  
                         source: new ol.source.XYZ({  
                             url: "http://t4.tianditu.com/DataServer?T=vec_w&x={x}&y={y}&l={z}"  
                         })     
                  })      
              ];  
             var map=new ol.Map({  
                  layers: layers,  
                  target:'map',      
                  view:new ol.View({      
                      projection: 'EPSG:4326',   
                      center:[-124.73142200000001,24.955967],        
                      zoom:5     
                  }) ,    
                controls: ol.control.defaults({    
                    attributionOptions: {    
                        collapsible: false    
                        }    
                    })    
              });

          /*   var tian_di_tu_road_layer = new ol.layer.Tile({  
                title: "天地图路网",  
                source: new ol.source.XYZ({  
                    url: "http://t4.tianditu.com/DataServer?T=vec_w&x={x}&y={y}&l={z}"  
                })  
            });  
            map.addLayer(tian_di_tu_road_layer);  */
            
          /*   var tian_di_tu_satellite_layer = new ol.layer.Tile({
                title: "天地图卫星影像",
                source: new ol.source.XYZ({
                    url: 'http://t3.tianditu.com/DataServer?T=img_w&x={x}&y={y}&l={z}'
                })
            });
            map.addLayer(tian_di_tu_satellite_layer);*/

            var tian_di_tu_annotation = new ol.layer.Tile({
                title: "天地图文字标注",
                source: new ol.source.XYZ({
                    url: 'http://t3.tianditu.com/DataServer?T=cva_w&x={x}&y={y}&l={z}'
                })
            });
            map.addLayer(tian_di_tu_annotation);

            var test =  new ol.layer.Tile({      
                source:new ol.source.TileWMS({      
                    url:'http://192.168.0.XX:8080/geoserver/topp/wms',      
                    params:{      
                        'LAYERS':'topp:states',//此处可以是单个图层名称,也可以是图层组名称,或多个图层名称  
                        'TILED':false      
                    },      
                    serverType:'geoserver'    //服务器类型  
                })     
            });   
            map.addLayer(test);
    }

<body onload='init();'>  
    <div style="position: absolute; top: 50px; left: 80px; width: 300px; height: 100px;">  
        <div id='map' style='width:1200px;height:500px;'></div>   
    </div>  
</body> 


3.执行效果



说明:

1).样例以天地图为低图,前端框架使用OpenLayers。

2).GeoServer WMS图层服务和上个样例一样为自带样例。

3).天地图低图图层分天地图路网图层,天地图卫星影像图层,天地图文字标注图层。

4),图层添加时注意图层叠加的先后顺序。

5),同上片,加载WMS图层服务注意图层中心点的和地图级别设置。



<think>好的,我需要帮用户找到关于如何使用OpenLayersGeoServer实现地图交互功能的教程或示例。用户提到了引用[1]和[2],其中引用[2]提到了使用WFS服务实现图层新增功能,这应该和交互相关。首先,我应该回顾用户提供的引用内容,确保答案准确。 用户的需求是地图交互功能,比如点击、绘制、编辑等。GeoServer通常提供WMS和WFS服务WMS用于显示地图,而WFS支持事务操作,适合交互。所以需要结合OpenLayers的交互控件和GeoServer的WFS服务。 接下来,我需要整理实现步骤。首先,发布图层GeoServer,确保启用WFS。然后,在OpenLayers中加载WMS作为底图,同时使用WFS-T(事务支持)来允许数据编辑。需要示例代码,展示如何初始化地图,添加交互控件如Select、Draw、Modify等。 然后,考虑用户可能需要的相关功能,比如点击查询、要素编辑、数据保存等。引用[2]已经提到了新增功能,可能包括绘制新要素并提交到GeoServer。需要确保代码示例正确,使用正确的WFS端点,并处理事务请求。 还要检查是否需要处理跨域问题,比如配置GeoServer的CORS设置。另外,用户可能需要了解如何配置GeoServer的数据存储,以及OpenLayers中如何与WFS服务交互。 最后,生成相关问题,帮助用户深入理解,比如性能优化、高级交互功能、安全设置等。确保回答结构清晰,分步骤说明,并提供可运行的代码示例,引用用户提供的参考资料。</think>以下是结合OpenLayersGeoServer实现地图交互功能的完整实现方案,包含核心代码示例和关键配置步骤: ### 一、基础环境搭建 1. 在GeoServer发布WMS/WFS服务 - 确保数据存储已正确配置 - 发布图层时勾选WFS服务选项 - 在"服务"→"WFS"设置中启用事务支持(WFS-T) 2. OpenLayers基础地图初始化 ```javascript import Map from 'ol/Map'; import View from 'ol/View'; import TileLayer from 'ol/layer/Tile'; import OSM from 'ol/source/OSM'; const map = new Map({ target: 'map', layers: [ new TileLayer({ source: new OSM() }) ], view: new View({ center: [0, 0], zoom: 2 }) }); ``` ### 二、核心交互功能实现 #### 1. 要素点击查询(GetFeatureInfo) ```javascript import WMSGetFeatureInfo from 'ol/format/WMSGetFeatureInfo'; map.on('singleclick', function(evt) { const view = map.getView(); const url = 'http://localhost:8080/geoserver/wms'; const params = { INFO_FORMAT: 'application/json', FEATURE_COUNT: 10, QUERY_LAYERS: 'workspace:layername' }; const parser = new WMSGetFeatureInfo(); fetch(url + '?REQUEST=GetFeatureInfo' + '&SERVICE=WMS' + '&VERSION=1.3.0' + '&CRS=' + view.getProjection().getCode() + '&WIDTH=' + map.getSize()[0] + '&HEIGHT=' + map.getSize()[1] + '&BBOX=' + view.calculateExtent().join(',') + '&I=' + Math.round(evt.pixel[0]) + '&J=' + Math.round(evt.pixel[1]) + formatParams(params)) .then(response => response.json()) .then(data => { console.log('查询结果:', parser.readFeatures(data)); }); }); ``` #### 2. 要素编辑(WFS-T) ```javascript import VectorLayer from 'ol/layer/Vector'; import VectorSource from 'ol/source/Vector'; import { Circle as CircleStyle, Fill, Stroke, Style } from 'ol/style'; import { Draw, Modify, Select } from 'ol/interaction'; import GeoJSON from 'ol/format/GeoJSON'; // 创建矢量图层 const vectorSource = new VectorSource({ format: new GeoJSON(), url: function(extent) { return 'http://localhost:8080/geoserver/wfs?service=WFS&' + 'version=1.1.0&request=GetFeature&typeName=workspace:layername&' + 'outputFormat=application/json&srsname=EPSG:3857'; }, strategy: 'all' }); const vectorLayer = new VectorLayer({ source: vectorSource, style: new Style({ /* 样式配置 */ }) }); // 添加交互控件 const draw = new Draw({ source: vectorSource, type: 'Polygon' }); const select = new Select(); const modify = new Modify({ features: select.getFeatures() }); map.addInteraction(draw); map.addInteraction(select); map.addInteraction(modify); // 保存修改 document.getElementById('save').addEventListener('click', function() { const format = new GeoJSON(); const features = vectorSource.getFeatures(); const serializer = new XMLSerializer(); const transaction = '<wfs:Transaction xmlns:wfs="http://www.opengis.net/wfs" ' + 'xmlns:gml="http://www.opengis.net/gml" ' + 'xmlns:workspace="http://workspace" ' + 'service="WFS" version="1.1.0">' + '<wfs:Update typeName="workspace:layername">' + // 生成更新内容 '</wfs:Update>' + '</wfs:Transaction>'; fetch('http://localhost:8080/geoserver/wfs', { method: 'POST', body: transaction }).then(response => { // 处理响应 }); }); ``` ### 三、关键配置注意事项 1. GeoServer端需要: - 开启CORS支持(jetty-web.xml添加跨域头) - 设置数据存储的写权限 - 配置用户权限(如有安全需求) 2. OpenLayers开发需要: ```bash npm install ol @geoapify/geocoder-autocomplete ``` ### 四、进阶交互功能扩展 1. 实时要素过滤 ```javascript vectorSource.setUrl(function(extent) { return 'http://geoserver/wfs?filter=' + encodeURIComponent( '<Filter xmlns="http://www.opengis.net/ogc">' + '<PropertyIsEqualTo>' + '<PropertyName>status</PropertyName>' + '<Literal>active</Literal>' + '</PropertyIsEqualTo>' + '</Filter>' ); }); ``` 2. 空间分析交互 ```javascript import { buffer } from 'ol/extent'; const bufferInteraction = new Draw({ type: 'Circle', geometryFunction: function(coordinates, geometry) { if (!geometry) geometry = new Polygon([]); const center = fromLonLat(coordinates[0]); const radius = coordinates[1][0] - coordinates[0][0]; return buffer(center, radius); } }); ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值