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

news2024/9/29 13:17:39

前言

这里主要是展示一下 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.coloradmin.cn/o/1541131.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

【嵌入式】Docker镜像构建指南:引领应用部署的革新之路

&#x1f9d1; 作者简介&#xff1a;阿里巴巴嵌入式技术专家&#xff0c;深耕嵌入式人工智能领域&#xff0c;具备多年的嵌入式硬件产品研发管理经验。 &#x1f4d2; 博客介绍&#xff1a;分享嵌入式开发领域的相关知识、经验、思考和感悟。提供嵌入式方向的学习指导、简历面…

深度学习pytorch——GPU加速(持续更新)

使用 .to(device)&#xff0c;以前使用 .cuda() &#xff0c;但是现在基本不使用了。 代码示例&#xff1a; 查看电脑GPU运行情况&#xff1a; 使用Ctrl Shift ESC快捷键&#xff1a;

Unreal中的四元数FQuat

四元数&#xff1a;Quaternion&#xff0c;四维数域内的数&#xff0c;可用于描述点在三维空间内的旋转&#xff08;因为三维的旋转可以理解为绕某个轴旋转一个角度&#xff0c;所以需要4个维度的信息&#xff09; 注意这里的旋转的轴&#xff0c;指的是从原点到 ( x , y , z )…

vue3+threejs新手从零开发卡牌游戏(九):添加抽卡逻辑和动效

首先优化下之前的代码&#xff0c;把game/deck/p1.vue中修改卡组方法和渲染卡组文字方法提到公共方法中&#xff0c;此时utils/common.ts完整代码如下&#xff1a; import { nextTick } from vue; import * as THREE from three; import * as TWEEN from tweenjs/tween.js impo…

数据库基础篇-------语法结构

友友们&#xff0c;大家好&#xff0c;今天我们来回顾我们的数据库啦&#xff0c;数据库技术是在我们大一就进行了解的&#xff0c;但是在大二的时候有的学校会进行数据库开发技术的教学&#xff0c;这两本书是不一样的&#xff0c;数据库基础更加偏向于对应的基础语法结构&…

计算方法实验2:列主元消元法和Gauss-Seidel迭代法解线性方程组

Task 即已知 y 0 0 , y 100 1 y_00,y_{100}1 y0​0,y100​1&#xff0c;解线性方程组 A y b \mathbf{A}\mathbf{y} \mathbf{b} Ayb&#xff0c;其中 A 99 99 [ − ( 2 ϵ h ) ϵ h 0 ⋯ 0 ϵ − ( 2 ϵ h ) ϵ h ⋯ 0 0 ϵ − ( 2 ϵ h ) ⋯ 0 ⋮ ⋮ ⋱ ⋱ ⋮ 0 0 ⋯…

数学建模综合评价模型与决策方法

评价方法主要分为两类&#xff0c;其主要区别在确定权重的方法上 一类是主观赋权法&#xff0c;多次采取综合资讯评分确定权重&#xff0c;如综合指数法&#xff0c;模糊综合评判法&#xff0c;层次评判法&#xff0c;功效系数法等 另一类是客观赋权法&#xff0c;根据各指标…

(C语言)浮点数在内存中的存储详解

1. 浮点数 常见的浮点数&#xff1a;3.14159、 1E10等 &#xff0c;浮点数家族包括&#xff1a; float、double、long double 类型。 浮点数表示的范围&#xff1a; float.h 中定义. 2. 浮点数的存储 我们先来看一串代码&#xff1a; int main() {int n 9;float* pFloa…

高中信息技术教资刷题笔记_大题篇

1.选择排序 2. SMTP属于TCP/IP协议体系结构中的哪一层&#xff1f;请列出其通信的三个阶段。 3.高中信息技术课程的基本理念/意义 4.视频作品制作和发布的主要环节 5.信息社会责任内涵及学生表现 6.教学活动意图 ①突出学生的主体地位。材料中&#xff0c;王老师设计的“扮演谍…

关系型数据库mysql(4)事务

目录 一.事务的概念 1.事务的定义 2.事务的特点 2.1原子性 2.2一致性 2.4持久性 3.事务之间的相互影响 3.1脏读 3.2不可重复读 3.3幻读 3.4丢失更新 4. 事务的隔离级别&#xff08;如何解决事务的干扰&#xff09; 4.1查询全局事务隔离级别 4.2查询全局事务 …

【零基础C语言】联合体(共用体)和枚举

目录 自定义类型&#xff1a;联合体(共用体)和枚举 1.自定义类型&#xff1a;联合体(共用体) 1.1 联合体的声明 1.2 联合体的特点 ​编辑1.3 联合体的大小计算 1.4使⽤联合体是可以节省空间的 1.5使用联合体写一个程序判断机器是大端还是小端存储 2.自定义类型&#xff1a;…

详解|temu选品师是什么?算蓝海项目吗?

在快速发展的跨境电商行业中&#xff0c;temu选品师这一岗位逐渐受到人们的关注。temu作为拼多多旗下的跨境电商平台&#xff0c;致力于为中国商家提供一个通向全球市场的桥梁。而temu选品师&#xff0c;则是这个桥梁上不可或缺的角色。 temu选品师主要负责在海量商品中挑选出具…

探索Zalo:从社交APP到Mini App开发指南

1.Zalo是什么&#xff1f; Zalo是一款源自越南的即时通讯和社交软件&#xff08;相当于国内的微信&#xff09;&#xff0c;由越南VNG公司开发。它集成了多种功能&#xff0c;包括但不限于免费的文字、语音、视频消息发送&#xff0c;高质量的语音和视频通话&#xff0c;群聊功…

电子电器架构 —— 诊断数据DTC具体故障篇

电子电器架构 —— 诊断数据DTC起始篇 我是穿拖鞋的汉子,魔都中坚持长期主义的汽车电子工程师 (Wechat:gongkenan2013)。 老规矩,分享一段喜欢的文字,避免自己成为高知识低文化的工程师: 本就是小人物,输了就是输了,不要在意别人怎么看自己。江湖一碗茶,喝完再挣扎…

批量重命名文件名,批量管理文件,复制后轻松删除原文件

在现代工作中&#xff0c;我们经常需要处理大量的文件&#xff0c;无论是工作文档、图片还是视频资料。对于很多人来说&#xff0c;文件管理是一项繁琐且耗时的任务。不过&#xff0c;现在有一种高效的文件管理方法&#xff0c;可以帮助你轻松复制文件后删除原文件夹&#xff0…

Redis入门到实战-第四弹

Redis实战热身Strings 篇 完整命令参考官网 官网地址 声明: 由于操作系统, 版本更新等原因, 文章所列内容不一定100%复现, 还要以官方信息为准 https://redis.io/Redis概述 Redis是一个开源的&#xff08;采用BSD许可证&#xff09;&#xff0c;用作数据库、缓存、消息代理…

鸿蒙开发学习【地图位置服务组件】

简介 移动终端设备已经深入人们日常生活的方方面面&#xff0c;如查看所在城市的天气、新闻轶事、出行打车、旅行导航、运动记录。这些习以为常的活动&#xff0c;都离不开定位用户终端设备的位置。 当用户处于这些丰富的使用场景中时&#xff0c;系统的位置定位能力可以提供…

解析服务器出现大量 TIME_WAIT 和 CLOSE_WAIT 状态的原因及排查方法

服务器出现大量 TIME_WAIT 状态的原因有哪些&#xff1f; 首先要知道 TIME_WAIT 状态是主动关闭连接方才会出现的状态&#xff08;别陷入一个误区不是只有客户端才能主动关闭连接的&#xff09;&#xff0c;所以如果服务器出现大量的 TIME_WAIT 状态的 TCP 连接&#xff0c;就是…

分布式组件 Nacos

1.在之前的文章写过的就不用重复写。 写一些没有写过的新东西 2.细节 2.1命名空间 &#xff1a; 配置隔离 默认&#xff1a; public &#xff08;默认命名空间&#xff09;:默认新增所有的配置都在public空间下 2.1.1 开发 、测试 、生产&#xff1a;有不同的配置文件 比如…

关于mysql无法添加中文数据的问题以及解决方案

往数据库表插入语句时&#xff0c;插入中文字段&#xff0c;中文直接变成&#xff1f;的问题&#xff0c; 出现这样的问题就是在创建数据库时 数据库字符集 没有选择uft8, 数据库校对规则没有选择utf8-bin 用 SHOW CREATE DATABASE 数据名; 可以查看你的这个数据库的定义…