39 openlayers 对接地图图层 绘制点线面圆

news/2024/7/10 1:56:22 标签: openlayers, 前端, vue, map

前言

这里主要是展示一下 openlayers 的一个基础的使用 

主要是设计 接入地图服务器的 卫星地图, 普通的二维地图, 增加地区标记 

增加 省市区县 的边界标记

基础绘制 点线面园 等等

测试用例

<template>
  <div style="width: 1920px; height:1080px;" >
    <div class="olMapUsageClass"></div>

    <div class="overdelay1" ref="overdelay1" >
      this is over delay1
    </div>
  </div>

</template>

<script>

  import Map from 'ol/Map'
  import View from 'ol/View'
  import DragPan from 'ol/interaction/DragPan'
  import MouseWheelZoom from 'ol/interaction/MouseWheelZoom'
  import PointerInteraction from 'ol/interaction/Pointer'
  import GeoJSON from 'ol/format/GeoJSON'
  import {Tile as TileLayer} from 'ol/layer'
  import {Vector as VectorLayer} from 'ol/layer'
  import {Image as ImageLayer} from 'ol/layer'
  import {Vector as VectorSource} from 'ol/source'
  import {Feature as Feature} from 'ol'
  import Point from 'ol/geom/Point'
  import LineString from 'ol/geom/LineString'
  import Polygon from 'ol/geom/Polygon'
  import CircleGeo from 'ol/geom/Circle'
  import XYZ from "ol/source/XYZ"
  import ImageStatic from "ol/source/ImageStatic"
  import {Circle, Fill, Icon, RegularShape, Stroke, Style, Text} from 'ol/style'
  import Overlay from 'ol/Overlay'
  import {transformExtent, transform} from "ol/proj"

  let sichuanJson = require(`../../public/json/sichuan.json`)

  export default {
    name: "olMapUsage",
    components: {},
    props: {},
    data() {
      return {
        map: null,
        tdtImgLayer: null,
        labelLayer: null,
        overlay: null,
      };
    },
    computed: {},
    watch: {},
    created() {

    },
    mounted() {

      this.initMap()

      this.test01AddBoundsLayer()
      this.test02AddDtImageLayer()
      // this.test03AddDtTDLayer()
      this.test04AddDtLabelLayer()

      this.test05AddImageLayer()
      this.test06AddCircleLayer([104.065735, 30.359462])
      this.test06AddCircleLayer([104.565735, 30.859462], "red")
      this.test07AddLineLayer()
      this.test08AddAreaLayer()
      this.test09AddCircleLayer()

      // this.test12SetCenterThenAddOverlay()

    },
    methods: {
      initMap() {
        let center = [104.065735, 30.659462]
        let projection = "EPSG:4326"
        let zoom = 10
        let minZoom = 5
        let maxZoom = 20
        const layer = []
        const view = new View({
          ...(this.viewOptions || {}),
          projection,
          center,
          zoom,
          minZoom,
          maxZoom
        })

        this.map = new Map({
          ...(this.mapOptions || {}),
          layers: [].concat(layer),
          view: view,
          target: this.$el,
          controls: [],
          interactions: [
            new DragPan(),
            new MouseWheelZoom(),
            new PointerInteraction({
              handleEvent: this.handleEvent
            })
          ]
        })
      },
      test01AddBoundsLayer() {
        const source = new VectorSource({})
        const style = {color: 'rgba(75,165,234,1)', width: '3'}
        const fill = 'rgba(255,255,255,0)'
        let parsedStyle = this.parseStyle({style, fill})
        const boundsLayer = new VectorLayer({
          zIndex: 1,
          source,
          parsedStyle
        })
        this.appendBounds2VectorLayer(boundsLayer, sichuanJson)
        this.map.addLayer(boundsLayer)
      },
      test02AddDtImageLayer() {
        this.tdtImgLayer = new TileLayer({
          zIndex: 2,
          source: new XYZ({
            projection: "EPSG:4326",
            url: "http://192.168.1.111:8888/tianditu/servlet/GoogleSatelliteMap?x={x}&y={y}&z={z}",
          }),
        });
        this.map.addLayer(this.tdtImgLayer);
      },
      test03AddDtTDLayer() {
        this.tdtImgLayer = new TileLayer({
          zIndex: 2,
          source: new XYZ({
            projection: "EPSG:4326",
            url: "http://192.168.1.111:8888/tianditu/servlet/GoogleTDMap?x={x}&y={y}&z={z}",
          }),
        });
        this.map.addLayer(this.tdtImgLayer);
      },
      test04AddDtLabelLayer() {
        this.labelLayer = new TileLayer({
          zIndex: 2,
          source: new XYZ({
            projection: "EPSG:4326",
            url: "http://192.168.1.111:8888/tianditu/servlet/GoogleTransparentMap?x={x}&y={y}&z={z}",
          }),
        });
        this.map.addLayer(this.labelLayer);
      },
      test05AddImageLayer() {
        // let extent = transformExtent([104.065735, 30.659462, 104.165735, 30.759462], "EPSG:4326", "EPSG:4326")
        let extent = [104.065735, 30.659462, 104.165735, 30.759462]
        let imageLayer = new ImageLayer({
          zIndex: 20,
          source: new ImageStatic({
            url: "/img/theme/desktop/17.jpg",
            projection: "EPSG:4326",
            imageExtent: extent
          }),
        });
        this.map.addLayer(imageLayer);
      },
      test06AddCircleLayer(coord, color) {
        color = color || 'green'
        let style = new Style({
          image: new Circle({
            radius:20,
            fill: new Fill({
              color: color
            })
          })
        })
        let feature = new Feature({
          geometry: new Point(coord)
        })
        feature.setStyle(style)
        let source = new VectorSource()
        source.addFeature(feature)

        let layer = new VectorLayer({
          zIndex: 20,
          source: source
        })
        this.map.addLayer(layer);
      },
      test07AddLineLayer() {
        let style = new Style({
          stroke: new Stroke({
            color: "blue",
            width: 3
          })
        })
        let feature = new Feature({
          geometry: new LineString([
            [104.065735, 30.359462],
            [104.165735, 30.359462],
            [104.265735, 30.459462],
          ])
        })
        feature.setStyle(style)
        let source = new VectorSource()
        source.addFeature(feature)

        let layer = new VectorLayer({
          zIndex: 20,
          source: source
        })
        this.map.addLayer(layer);
      },
      test08AddAreaLayer() {
        let style = new Style({
          fill: new Fill({
            color: "#ff0000"
          }),
          stroke: new Stroke({
            color: "blue",
            width: 3
          })
        })
        let feature = new Feature({
          geometry: new Polygon([[
            transform([104.065735, 30.559462], "EPSG:4326", "EPSG:4326"),
            transform([104.165735, 30.559462], "EPSG:4326", "EPSG:4326"),
            transform([104.165735, 30.659462], "EPSG:4326", "EPSG:4326"),
          ]])
        })
        feature.setStyle(style)
        let source = new VectorSource()
        source.addFeature(feature)

        let layer = new VectorLayer({
          zIndex: 20,
          source: source
        })
        this.map.addLayer(layer);
      },
      test09AddCircleLayer() {
        let style = new Style({
          fill: new Fill({
            color: "#ffff00"
          }),
          stroke: new Stroke({
            color: "#00ffff",
            width: 3
          })
        })
        let feature = new Feature({
          geometry: new CircleGeo(transform([104.665735, 30.559462], "EPSG:4326", "EPSG:4326"), 0.2)
          // geometry: new Circle([104.265735, 30.559462], 300)
        })
        feature.setStyle(style)
        let source = new VectorSource()
        source.addFeature(feature)

        let layer = new VectorLayer({
          zIndex: 20,
          source: source
        })
        this.map.addLayer(layer);
      },
      test10SetCenter(coord, color) {
        this.map.getView().setCenter(coord)
        this.test06AddCircleLayer(coord, color)
      },
      test11AddOverlay(coord) {
        this.overlay && this.map.removeOverlay(this.overlay)
        this.overlay = new Overlay({
          element: this.$refs.overdelay1,
          position: coord,
          positioning: "bottom-center",
          offset: [0, 0],
          autoPan: true,
          autoPanMargin: 200,
          autoPanAnimation: {
            duration: 1000
          },
          map: this.map
        })
        this.map.addOverlay(this.overlay)
      },
      test12SetCenterThenAddOverlay() {
        // refer cycle
        this.test06AddCircleLayer([10.265735, 10.659462], "#007f5a")
        this.test06AddCircleLayer([105.565735, 30.759462], "#0039ff")

        let _this = this
        // use this for map.addOverlay's animation update
        setTimeout(function() {
          _this.test11AddOverlay([10.065735, 10.459462])
          _this.test10SetCenter([10.065735, 10.459462], "yellow")
        }, 2000)

        // the core case, normal or exception or compensated
        setTimeout(function() {

          // case1. function of addOverlay
          _this.test11AddOverlay([105.065735, 30.259462])

          // case2. normal case
          // _this.test11AddOverlay([105.065735, 30.259462])
          // _this.test10SetCenter([105.065735, 30.259462], "red")

          // case3. exception case
          // _this.test10SetCenter([105.065735, 30.259462], "red")
          // _this.test11AddOverlay([105.065735, 30.259462])

          // case4. compensated case
          // _this.test10SetCenter([105.065735, 30.259462], "red")
          // setTimeout(function() {
          //   _this.test11AddOverlay([105.065735, 30.259462])
          // }, 1000)
        }, 5000)

      },
      appendBounds2VectorLayer(layer, json) {
        const geoJson = this.geoJsonDecode(json);
        let features = new GeoJSON().readFeatures(geoJson) || [];
        features = features.map(feature => {
          feature.__vm__ = this;
          return feature
        });
        const source = layer.getSource();
        source.addFeatures(features)
      },
      handleEvent(e) {
        if (e.type === "pointermove") {
          return true
        }
        console.log(" handle event : ", e)
        return true
      },
      geoJsonDecode(json) {
        const features = json.features || []
        features.forEach(feature => {
          const geometry = feature.geometry || {}
          const {coordinates, encodeOffsets} = geometry
          if (!encodeOffsets) return
          geometry.coordinates = coordinates.map((coordinate, i) => {
            if (Array.isArray(coordinate)) {
              return coordinate.map((item, j) => {
                return this.decodePolygon(item, encodeOffsets[i][j])
              })
            } else {
              return this.decodePolygon(coordinate, encodeOffsets[i])
            }
          })
          geometry.encodeOffsets = null
        })
        return json
      },
      decodePolygon(coordinate, encodeOffsets) {
        const result = [];
        let prevX = encodeOffsets[0];
        let prevY = encodeOffsets[1];
        for (let i = 0; i < coordinate.length; i += 2) {
          let x = coordinate.charCodeAt(i) - 64;
          let y = coordinate.charCodeAt(i + 1) - 64;
          x = (x >> 1) ^ (-(x & 1));
          y = (y >> 1) ^ (-(y & 1));
          x += prevX;
          y += prevY;

          prevX = x;
          prevY = y;
          result.push([x / 1024, y / 1024]);
        }

        return result;
      },
      parseStyle(settings, StyleModel) {
        const PROPS_MAP = {
          fill: Fill,
          text: Text,
          stroke: Stroke,
          circle: Circle,
          icon: Icon,
          regularShape: RegularShape,
          backgroundFill: Fill,
          backgroundStroke: Stroke
        }
        const IMAGE_PROPS = [Circle, Icon, RegularShape]

        const opts = {};
        Object.entries(settings).forEach(([key, value]) => {
          const Model = PROPS_MAP[key];
          if (key === 'font') {
            value = `${value} sans-serif`
          }
          opts[IMAGE_PROPS.includes(Model) ? 'image' : key] = this.parseValue(Model, key, value)
        });
        return new (StyleModel || Style)(opts)
      },
      parseValue(Model, key, value) {
        if (value === undefined || value === null) return;
        if (!Model) return value;
        if (['fill', 'backgroundFill'].includes(key)) return this.parseFill(value);
        if (key === 'text') {
          return isObject(value) ? parse(value, Model) : value
        }
        return parse(value, Model)
      },
      parseFill(fill) {
        const opts = this.isObject(fill) ? fill : {color: fill}
        return new Fill(opts)
      },
      isObject(value) {
        return typeof value === 'object'
      }

    }
  };
</script>

<style lang="scss">
  .olMapUsageClass {

  }
  .overdelay1 {
    position: absolute;
    border: 1px greenyellow solid;
    width: 200px;
    height: 50px;
  }
</style>

绘制卫星地图 + 地图标注

执行效果如下 

二维地图 + 地图标注

执行效果如下 

二维地图 + 地图边界

执行效果如下 

绘制点线面园 

执行效果如下 

卫星地图 + 地图标注 + 点线面园 

执行效果如下 

完 


http://www.niftyadmin.cn/n/5449746.html

相关文章

尽可能使用清晰、统一的方式初始化所有对象:列表初始化。【C++】

不管是为了统一性&#xff0c;还是避免发生窄化转换&#xff0c;尽可能使用初始化列表。 说明哪些对象可以使用列表初始化&#xff1f;代码演示 说明 C11 引入了列表初始化&#xff08;也称为统一初始化或初始化列表&#xff09;&#xff0c;它是一种使用花括号 {} 来初始化对…

【XR806开发板试用】使用PWM模块模拟手机呼吸灯提示功能

一般情况下&#xff0c;我们的手机在息屏状态&#xff0c;当收到消息处于未读状态时&#xff0c;会有呼吸灯提醒&#xff0c;这次有幸抽中XR806开发板的试用&#xff0c;经过九牛二虎之力终于将环境搞好了&#xff0c;中间遇到各种问题&#xff0c;在我的另一篇文章中已详细描述…

【云开发笔记No.7】敏捷开发

敏捷开发是一种以人为核心、迭代、循序渐进的软件开发方法。它起源于20世纪90年代初期&#xff0c;由一些软件行业的先驱者提出&#xff0c;旨在解决传统软件开发过程中存在的一些问题&#xff0c;如需求变化频繁、开发周期长、成本高等。敏捷开发强调团队合作、客户需求和快速…

java算法题每日多道六

138. 随机链表的复制 题目 给你一个长度为 n 的链表&#xff0c;每个节点包含一个额外增加的随机指针 random &#xff0c;该指针可以指向链表中的任何节点或空节点。 构造这个链表的 深拷贝。 深拷贝应该正好由 n 个 全新 节点组成&#xff0c;其中每个新节点的值都设为其对…

国内ip地址怎么改?详解修改ip地址的步骤

在网络通信中&#xff0c;IP地址是设备在网络上的标识&#xff0c;对于用户、服务器和网络安全都至关重要。然而&#xff0c;有时候在特定情况下&#xff0c;可能需要修改IP地址以满足不同需求或解决特定问题。虎观代理小二将深入研究中国国内IP地址修改的方法与影响&#xff0…

C语言结构体居然还能这样初始化!

目录 前言 一、定义时赋值&#xff08;常见&#xff09; 二、定义后再赋值&#xff08;常见&#xff09; 三、定义时乱序赋值&#xff08;不常见&#xff09; 四、定义时乱序赋值&#xff08;反正我第一次见&#xff0c;之前没接触过&#xff09; 前言 本文适用于对C语言结构…

javaWeb奶茶商城前后台系统

一、简介 在当前数字化时代&#xff0c;电子商务已成为人们生活中不可或缺的一部分。为了满足用户对奶茶的需求&#xff0c;我设计并实现了一个基于JavaWeb的奶茶商城前后台系统。该系统涵盖了用户前台和管理员后台两大模块&#xff0c;包括登录注册、商品展示、购物车管理、订…

知识图表示学习中的负抽样研究综述

摘要 知识图表示学习(KGRL)或知识图嵌入(KGE)在知识构建和信息探索的人工智能应用中起着至关重要的作用。这些模型旨在将知识图中的实体和关系编码到低维向量空间中。在KGE模型的训练过程中&#xff0c;使用正样本和负样本是区分的必要条件。然而&#xff0c;直接从现有的知识…