Vue+OpenLayers学习系列(四)OpenLayers 加载 WMS 地图服务(GeoServer)

16 篇文章 1 订阅

一、简单介绍

OpenLayers 读取 WMS 服务与 OpenLayers 读取 GeoServer 发布的地图服务 类似;

其中WMS介绍参见:

https://zhuanlan.zhihu.com/p/73973319;

https://zhuanlan.zhihu.com/p/74273452;

二、代码

本次 Demo 主要介绍读取 GeoServer 发布的 WMS 地图服务,并且绘制矢量

<template>
  <div>
    <div id="map" ref="rootmap">
    </div>
    <label>Shape type &nbsp;</label>
    <select id="selectType">
      <option value="Point">Point</option>
      <option value="LineString">LineString</option>
      <option value="Polygon">Polygon</option>
      <option value="Circle">Circle</option>
      <option value="Square">Square</option>
      <option value="Box">Box</option>
      <option value="None">None</option>
    </select>
  </div>
</template>
<script>
  import 'ol/ol.css';
  import { Map,View } from 'ol';
  import Feature from 'ol/Feature';
  import {Tile as TileLayer, Image as ImageLayer,Vector as VectorLayer} from 'ol/layer';
  import {Point, LineString, Polygon} from 'ol/geom'
  import {OSM, ImageArcGISRest,TileWMS,ImageWMS,Vector as VectorSource} from 'ol/source';
  import Draw from 'ol/interaction/Draw'
  import {createRegularPolygon,createBox} from 'ol/interaction/Draw'

  export default{
    name: 'OlGeoserveDraw',
    data(){
      return{
        map: null,
        typeSelect: null,
        draw: null,
        vectorSource: null
      };
    },
    mounted(){
      var url = 'http://localhost:8080/geoserver/ws-world/wms';
      this.vectorSource = new VectorSource();
      let vectorLayer = new VectorLayer({
        source: this.vectorSource
      });

      this.map = new Map({
        target: "map",
        layers: [
          new TileLayer({
            source: new OSM()   //这个会出现底图
          }),
          new ImageLayer({     //TileLayer、ImageLayer
            source: new ImageWMS({    //TileWMS、ImageWMS
              ratio: 1,
              params: {
                'FORMAT': 'image/jpeg',
                'VERSION': '1.1.0',
                'LAYERS': 'ws-world:ws-group',
                'STYLES': '',
              },
              url: url
            })
          }),
          vectorLayer
        ],
        view: new View({
          projection: 'EPSG:4326',    //当添加投影坐标时,无法进行绘制
          center: [0, 0],
          zoom: 4
        })
      });
      this.typeSelect = document.getElementById('selectType');

      this.typeSelect.addEventListener('change', () => {
        // 移除Draw绘图控件
        this.map.removeInteraction(this.draw);
        this.addInteraction();
      });

      this.addInteraction();

    },
    methods:{
      addInteraction(){
        let type = this.typeSelect.value;
        if(type !== 'None'){
          let geometryFunction;
          switch(type){
            case "Square":
              type = 'Circle';
              // 生成规则的四边形的图形函数
              geometryFunction = createRegularPolygon(4);
              break;
            case 'Box':
              type = 'Circle';
              // 生成盒形状的图形函数
              geometryFunction = createBox();
              break;
            default:break;
          }

          // 初始化Draw绘图控件
          this.draw = new Draw({
            source: this.vectorSource,
            type: type,
            geometryFunction: geometryFunction
          });
          // 将Draw绘图控件加入Map对象
          this.map.addInteraction(this.draw);
        }
      }
    }
  };

</script>

<style>
  #map{
    height:800px;
    width: 1400px;
  }
  /*隐藏ol的一些自带元素*/
  .ol-attribution,.ol-zoom { display: none;}

</style>

三、最后结果如下图所示

 

 

 

 

 

 

  • 4
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
### 回答1: 要在OpenLayers加载GeoServerWMS服务,需要按照以下步骤操作: 1. 创建一个OpenLayers地图对象。 2. 创建一个WMS图层对象,并指定GeoServerWMS服务地址、图层名称、投影方式等参数。 3. 将WMS图层对象添加到地图对象中。 4. 设置地图的中心点和缩放级别。 5. 在HTML页面中显示地图。 具体的代码实现可以参考OpenLayers的官方文档或者其他相关教程。 ### 回答2: OpenLayers是一个流行的前端JavaScript库,用于展示Web地图,可以通过加载GeoserverWMS服务显示地图WMS服务为Web开发者提供了一个基于标准HTTP的图像地图服务Geoserver是一个开源的地图服务器,可以提供WMS服务。在使用OpenLayers加载GeoserverWMS时,需要执行以下步骤: 1. 引入OpenLayers的JavaScript库文件和样式表。 ``` <script src="https://openlayers.org/en/v4.6.5/build/ol.js"></script> <link rel="stylesheet" href="https://openlayers.org/en/v4.6.5/css/ol.css" type="text/css"> ``` 2. 创建一个容器,用于展示地图。 ``` <div id="map" class="map"></div> ``` 3. 创建一个OpenLayers地图对象,并指定地图容器。 ``` var map = new ol.Map({ target: 'map', layers: [], view: new ol.View({ center: [0, 0], zoom: 2 }) }); ``` 4. 创建一个WMS图层,并添加到地图中。 ``` var wmsLayer = new ol.layer.Image({ source: new ol.source.ImageWMS({ url: 'http://localhost:8080/geoserver/wms', params: { 'LAYERS': 'topp:states' }, ratio: 1, serverType: 'geoserver' }) }); map.addLayer(wmsLayer); ``` 其中,url为Geoserver WMS服务的地址,params为WMS服务中需要展示的图层。 5. 刷新地图。 ``` map.updateSize(); ``` 6. 运行完整代码。 ``` <!DOCTYPE html> <html> <head> <title>OpenLayers with Geoserver WMS</title> <link rel="stylesheet" href="https://openlayers.org/en/v4.6.5/css/ol.css" type="text/css"> <style> .map { height: 500px; } </style> <script src="https://openlayers.org/en/v4.6.5/build/ol.js"></script> </head> <body> <div id="map" class="map"></div> <script> var map = new ol.Map({ target: 'map', layers: [], view: new ol.View({ center: [0, 0], zoom: 2 }) }); var wmsLayer = new ol.layer.Image({ source: new ol.source.ImageWMS({ url: 'http://localhost:8080/geoserver/wms', params: { 'LAYERS': 'topp:states' }, ratio: 1, serverType: 'geoserver' }) }); map.addLayer(wmsLayer); map.updateSize(); </script> </body> </html> ``` 通过以上步骤,就可以成功加载GeoserverWMS服务,展示地图。需要注意的是,在使用GeoserverWMS服务时,需要确保服务是运行正常的,并且所使用的图层是存在的。 ### 回答3: OpenLayers是WebGIS开发中常用的一种开发工具,它可以通过加载各种数据源实现空间数据的可视化呈现,其中WMS是一种常用的数据源。 要加载geoserver发布的WMS,需要使用OpenLayersWMS图层类(ol.layer.Tile),它通过向geoserver发送WMS请求,在地图上绘制WMS图层。 下面是实现的步骤: 1. 引入OpenLayers库和样式文件: ``` <link rel="stylesheet" href="https://openlayers.org/en/v4.6.5/css/ol.css"> <script src="https://cdn.jsdelivr.net/npm/ol/dist/ol.js"></script> ``` 2. 创建地图容器和地图对象: ``` var map = new ol.Map({ target: 'map', // 地图容器id layers: [], // 初始图层为空 view: new ol.View({ center: [0, 0], // 初始中心点坐标 zoom: 0 // 初始缩放等级 }) }); ``` 3. 创建WMS图层: ``` var layers = [ // 创建geoserver发布的WMS图层(注意url和params的配置) new ol.layer.Tile({ source: new ol.source.TileWMS({ url: 'http://localhost:8080/geoserver/wms', params: { 'LAYERS': 'topp:states', // 图层名称 'TILED': true } }) }) ]; ``` 4. 添加图层到地图: ``` map.addLayers(layers); ``` 5. 在html文件中添加地图容器: ``` <div id="map"></div> ``` 可以根据需要修改各个参数的值,从而实现自己所需的WMS图层加载效果。同时,需要注意geoserver发布的WMS服务需要在geoserver中配置,确保参数设置正确无误。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值