vue+天地图+Openlayers


npm install ol

<template>
        <div id="geomap" style="width: 100%; height: 722px" class="map-container" ref="mapContainer"></div>
        <div class="mapIcon2">
          <div class="btn" @click="chgIcons">{{iconText}}图例</div>
          <div class="btn" @click="chgxj">{{xjText}}乡界</div>
          <div class="btn" @click="chghl">{{hlText}}河流</div>
        </div>
</template>

<script>

  //以下为 geomap 支持
  import ols from '@/utils/ols.js';
  import { get as getProjection } from 'ol/proj'; // 引入 useGeographic
  import jybjGeo from '@/assets/board/jiangyongxian.json'      //外缘地图包
  import jyxjGeo from '@/assets/board/jiangyongxiang.json'     //乡界地图包
   
 mounted() {
	      // 初始化 geomap
      this.initGeomap();
},
method:{

      //geomap
      initGeomap() {
        // 启用地理坐标系
        ols.useGeographic();

        // //添加天地图底图
        var tdTU = new ols.TileLayer({
          source: new ols.XYZ({
            url: 'http://webst0{1-4}.is.autonavi.com/appmaptile?lang=zh_cn&size=1&scale=1&style=7&x={x}&y={y}&z={z}',
          }),
        })

        // 天地图的卫星图
        var tiandi_img_w = new ols.Tile({
          title: "卫星图",
          source: new ols.XYZ({
            crossOrigin: "anonymous",
            url: "http://t7.tianditu.com/DataServer?T=img_w&x={x}&y={y}&l={z}&tk=" + this.tdtKey,
          }),
          visible: true, // 默认为true 展示
        });

        var tiandi_cia_w = new ols.Tile({
          title: "卫星图",
          source: new ols.XYZ({
            crossOrigin: "anonymous",
            url: "http://t7.tianditu.com/DataServer?T=cia_w&x={x}&y={y}&l={z}&tk=" + this.tdtKey,
          }),
          visible: true, // 默认为true 展示
        })

        this.map = new ols.olMap({
          target: this.$refs.mapContainer,
          layers: [
            //添加天地图底图
            //tdTU,

            // 添加天地图的卫星影像底图 + 影像注记
            tiandi_img_w,
            tiandi_cia_w,
          ],
          view: new ols.View({
            center: [111.3498, 25.1641], // 江永的中心点
            zoom: 10, // 初始缩放级别
            projection: 'EPSG:3857'
          }),
        });


        //创建蒙层,凸显区域
        this.showjyArea();
        //绘制乡界
        this.showjyxj();
        //绘制河流
        this.showjyhl()
        // initMap over
      },
      showjyhl() {
        this.hlWMS = new ols.TileLayer({
          source: new ols.TileWMS({
            url: 'http://XXXX:XXXX/geoserver/XXXX/wms',
            params: {
              LAYERS: 'jiangyong:jiangyongheliu',
              VERSION: '1.1.1',
              TILED: true,
            },
            serverType: 'geoserver',
            projection: 'EPSG:3857',
          }),
        })
        this.map.addLayer(this.hlWMS);
      },
      addGeomapinfo() {
        this.addIcons();
        this.addCircles();
        if (this.zoomLevel <= 10) {
          this.clustersState = false
          this.clusters.setVisible(this.clustersState);
          this.circlesState = true
          this.circles.setVisible(this.circlesState);
          this.miState = false
        } else {
          this.clustersState = true;
          this.clusters.setVisible(this.clustersState);
          this.circlesState = false
          this.circles.setVisible(this.circlesState);
          this.miState = true
        }
      },
      clearGeomapinfo() {
        if (this.circles) {
          this.circles.getSource().clear();
          this.circles = null
        }
        if (this.clusters) {
          this.clusters.getSource().clear();
          this.clusters = null
        }
      },
      addCircles(center, radius) {
        // 创建Feature对象集合
        const features2 = [];
        this.mapData.forEach((item) => {
          const point = new ols.Point([Number(item.ln[0]), Number(item.ln[1])]);
          const feature = new ols.Feature({
            geometry: point,
          });
          features2.push(feature);
        });
        // 矢量要素数据源
        const source2 = new ols.VectorSource({
          features: features2,
        });
        this.circles = new ols.VectorLayer({
          zIndex: 4,
          source: source2,
          style: (feature, resolution) => {
            return new ols.Style({
              image: new ols.Circle({
                radius: 5,
                fill: new ols.Fill({
                  color: '#06f106',
                }),
              }),
            });
          },
        });

        this.map.addLayer(this.circles);
      },
      addIcons() {
        // 创建Feature对象集合
        const features1 = [];
        this.mapData.forEach((item) => {
          const point = new ols.Point([Number(item.ln[0]), Number(item.ln[1])]);
          const feature = new ols.Feature({
            geometry: point,
            info: item,
            imgSrc: item.imgSrc, // 添加图标路径
          });
          features1.push(feature);
        });
        // 矢量要素数据源
        const source1 = new ols.VectorSource({
          features: features1,
        });
        this.clusters = new ols.VectorLayer({
          zIndex: 4,
          source: source1,
          style: (feature, resolution) => {
            const imgSrc = feature.get('imgSrc');
            return new ols.Style({
              image: new ols.Icon({
                opacity: 1,
                src: imgSrc,
                scale: 1, // 图标缩放比例
              }),
            });
          }
        });

        this.map.addLayer(this.clusters);
        this.bindClickEvent();
      },
      // 添加多边形
      showjyxj() {
        // 解析 GeoJSON 数据
        const features = new ols.GeoJSON().readFeatures(jyxjGeo);
        // 创建矢量源
        const vectorSource = new ols.VectorSource({
          features: features
        });
        this.vectorLayerXJ = new ols.VectorLayer({
          zIndex: 3,
          source: vectorSource,
          style: new ols.Style({
            stroke: new ols.Stroke({
              color: "#f4b49f",
              width: 3
            })
          })
        });
        this.map.addLayer(this.vectorLayerXJ);
      },
      // 添加多边形并填充外部区域
      showjyArea() {
        let initLayer = new ols.VectorLayer({
          zIndex: 3,
          source: new ols.VectorSource(),
          style: new ols.Style({
            fill: new ols.Fill({
              color: "rgba( 255, 255, 255, 0.7)",
            }),
            stroke: new ols.Stroke({
              color: "#f4b49f",
              width: 3
            })
          })
        });
        this.map.addLayer(initLayer);
        this.addConver(initLayer, jybjGeo);
      },
      //添加遮罩
      addConver(converLayer, data) {
        const fts = new ols.GeoJSON().readFeatures(data);
        const ft = fts[0];
        const converGeom = this.erase(ft.getGeometry());
        const convertFt = new ols.Feature({
          geometry: converGeom,
        });
        converLayer.getSource().addFeature(convertFt);
      },
      //擦除操作,生产遮罩范围
      erase(geom) {
        const extent = [-180, -90, 180, 90];
        const polygonRing = ols.fromExtent(extent);
        const coords = geom.getCoordinates();
        coords.forEach(coord => {
          const linearRing = new ols.LinearRing(coord[0]);
          polygonRing.appendLinearRing(linearRing);
        });
        return polygonRing;
      },
      addZoomListener() {
        // 监听鼠标滚轮事件
        this.map.getView().on('change:resolution', () => {
          this.zoomLevel = this.map.getView().getZoom();
          if (this.iconState) {
            if (this.zoomLevel <= 10) {
              this.clustersState = false
              this.clusters.setVisible(this.clustersState);
              this.circlesState = true
              this.circles.setVisible(this.circlesState);
              this.miState = false
            } else {
              this.clustersState = true;
              this.clusters.setVisible(this.clustersState);
              this.circlesState = false
              this.circles.setVisible(this.circlesState);
              this.miState = true
            }
          }
        });
      },
      bindClickEvent() {
        // 移除已有的点击事件监听器
        if (this.clickListener) {
          this.map.un('singleclick', this.clickListener);
        }

        // 绑定新的点击事件监听器
        this.clickListener = (e) => {
          let point = this.map.forEachFeatureAtPixel(e.pixel, (feature) => feature);
          if (point && point.get('info')) {
            let params = point.getProperties();

            let obj = point.get('info');
            // console.log("当前标点参数", obj);
            if (obj.attribute == '河道') {
              this.objshow = false
              this.rivershow = true
              this.riverData = obj
              this.getRiverArea(obj)
              setTimeout(() => {
                this.rHeight = this.$refs.vbody.offsetHeight + 50
              }, 200)
            } else {
              this.closeRiver()
              this.objshow = true
              this.objData = obj
            }
          }
        };

        this.map.on('singleclick', this.clickListener);
      },
      chgIcons() {
        this.iconState = !this.iconState
        this.iconText = this.iconState ? '隐藏' : '显示'
        if (this.zoomLevel <= 10) {
          this.circlesState = !this.circles.getVisible()
          this.circles.setVisible(this.circlesState);
        } else {
          this.clustersState = !this.clusters.getVisible()
          this.clusters.setVisible(this.clustersState);
        }
      },
      chgxj() {
        if (this.vectorLayerXJ) {
          this.vectorLayerXJState = !this.vectorLayerXJ.getVisible()
          this.vectorLayerXJ.setVisible(this.vectorLayerXJState);
          this.xjText = this.vectorLayerXJState ? '隐藏' : '显示'
        }
      },
      chghl() {
        if (this.hlWMS) {
          this.hlWMSState = !this.hlWMS.getVisible()
          this.hlWMS.setVisible(this.hlWMSState);
          this.hlText = this.hlWMSState ? '隐藏' : '显示'
        }
      },
}
</script>

ol.js

// ----------------<<CSS文件>>----------------
import "ol/ol.css";
 
// ----------------<<常用模块>>----------------
import Map from "ol/Map.js";
import View from "ol/View.js";
import TileLayer from "ol/layer/Tile";
import Tile from 'ol/layer/Tile';
import XYZ from 'ol/source/XYZ'
import Fill from 'ol/style/Fill.js'
import Point from 'ol/geom/Point.js';
import Style from 'ol/style/Style.js';
import Circle from 'ol/style/Circle.js';
import Stroke from 'ol/style/Stroke.js';
import Feature from 'ol/Feature.js';
import VectorLayer from 'ol/layer/Vector.js';
import VectorSource from 'ol/source/Vector.js';
 
// ----------------<<其他模块>>----------------
 
import Icon from 'ol/style/Icon.js';
import GeoJSON from 'ol/format/GeoJSON.js';
import TileWMS from 'ol/source/TileWMS.js';
 import { useGeographic } from 'ol/proj.js'  
import { fromExtent } from "ol/geom/Polygon";
import LinearRing from 'ol/geom/LinearRing.js';

const ols = {
  olMap: Map,
  View: View,
  TileLayer: TileLayer,
  Tile: Tile,
  XYZ: XYZ,
  Icon: Icon,
  Fill: Fill,
  Point: Point,
  Style: Style,
  Circle: Circle,
  Stroke: Stroke,
  Feature: Feature,
  GeoJSON: GeoJSON,
  TileWMS: TileWMS,
  VectorLayer: VectorLayer,
  VectorSource: VectorSource,
  useGeographic: useGeographic,
  fromExtent:fromExtent,
  LinearRing:LinearRing
}
 
export default ols
相关推荐
王哈哈^_^1 小时前
【数据集】【YOLO】【目标检测】交通事故识别数据集 8939 张,YOLO道路事故目标检测实战训练教程!
前端·人工智能·深度学习·yolo·目标检测·计算机视觉·pyqt
cs_dn_Jie2 小时前
钉钉 H5 微应用 手机端调试
前端·javascript·vue.js·vue·钉钉
开心工作室_kaic2 小时前
ssm068海鲜自助餐厅系统+vue(论文+源码)_kaic
前端·javascript·vue.js
有梦想的刺儿3 小时前
webWorker基本用法
前端·javascript·vue.js
cy玩具3 小时前
点击评论详情,跳到评论页面,携带对象参数写法:
前端
customer083 小时前
【开源免费】基于SpringBoot+Vue.JS周边产品销售网站(JAVA毕业设计)
java·vue.js·spring boot·后端·spring cloud·java-ee·开源
qq_390161774 小时前
防抖函数--应用场景及示例
前端·javascript
John.liu_Test4 小时前
js下载excel示例demo
前端·javascript·excel
Yaml44 小时前
智能化健身房管理:Spring Boot与Vue的创新解决方案
前端·spring boot·后端·mysql·vue·健身房管理
PleaSure乐事4 小时前
【React.js】AntDesignPro左侧菜单栏栏目名称不显示的解决方案
前端·javascript·react.js·前端框架·webstorm·antdesignpro