vue+openlayers实现地图打点

2023-11-14


前言

openlayers的使用

一、使用步骤

1.引入库

代码如下(示例):npm install ol

import "ol/ol.css"; //样式
import Map from "ol/Map"; //地图对象
import Feature from "ol/Feature"; //标记
import Overlay from "ol/Overlay";
import View from "ol/View"; //视图
import { Point, LineString } from "ol/geom"; //标点,画线
import {
  OSM,
  XYZ,
  TileArcGISRest,
  Vector as VectorSource,
  WMTS,
} from "ol/source"; //图源
import { Vector as VectorLayer, Tile as TileLayer } from "ol/layer"; //图层
import { fromLonLat, transform } from "ol/proj"; //坐标系维度转换
import {
  Circle as CircleStyle,
  Fill,
  Icon,
  Stroke,
  Style,
  RegularShape,
} from "ol/style"; //填充图标描边等之类用来标记打点使用

2.方法里使用

<template>
  <div>
    <div id="container">
      <audio
        controls="controls"
        ref="audiod"
        style="display: none"
        src="../audio/alert.mp3"
      ></audio>
</div>

  </div>
</template>
  props: ["shipData"],
  mounted() {
    this.initMap();
  },
 watch: {
    checkmsgModal() {},
    shipData: {
      deep: true,
      handler() {
        this.vectorSource.clear();
        this.shipData.forEach((item, index) => {
          item.customSortId = index;
          this.setMarker(item);
        });
      },
    },

  },
methods:{
  initMap() {
      let that = this;
      //地图瓦片
      let tileLayer = new TileLayer({
        source: new XYZ({
          url: "写入地图瓦片地址",

        }),
      });
      // 绘制点
      let featureLayer = new VectorLayer({
        source: that.vectorSource,
      });
      // 绘制线
      that.vectorLineSource = new VectorSource({ wrapX: false });
      let featureLineLayer = new VectorLayer({
        source: that.vectorLineSource,
      });
      let sourceSatelliteMark = new TileLayer({
        source: new XYZ({
          url: "瓦片地址",
       
        }),
      });
      this.map = new Map({
        //olMap为map的地图容器
        target: "container", // DOM容器
        // 设置地图图层
        layers: [tileLayer, featureLayer, featureLineLayer], //地图源的瓦片图层
        // 设置显示地图的视图
        view: new View({
          // projection: 'EPSG:4326',
          center: transform(this.jwd, "EPSG:4326", "EPSG:3857"), //地图中心点 经纬度
          zoom: this.zoom, // 缩放级别-显示层级
          maxZoom: 16,
        }),
      });
      // this.map.addLayer(tileLayer);
       //这个是点击出现弹窗
      let overlayForm = new Overlay({
        // 创建一个图层
        element: this.$refs.msgForm.$el, // 图层绑定我们上边的弹窗
        autoPan: true,
        autoPanAnimation: {
          duration: 250,
        },
        autoPanMargin: 100,
        positioning: "center-center",
      });
      overlayForm.setPosition(undefined); // 设置弹窗位置,刚开始我们不让他显示,就是undefined就行
      this.map.addOverlay(overlayForm); // 然后把弹窗的图层加载到地图上
      this.map.on("click", (ev) => {
        let pixel = ev.pixel; // 鼠标点击的位置,这个应该是像素
        // let mouse = ev.coordinate  // 鼠标点击的坐标位置
        let mouse = ev.coordinate; // 鼠标点击的坐标位置
        const hdms = transform(mouse, "EPSG:3857", "EPSG:4326"); // 转换坐标格式
        let feature = this.map.forEachFeatureAtPixel(pixel, (feature) => {
          return feature; // 查找出点击的哪个坐标
        });
        console.log(feature);
        // console.log(feature)
        if (feature) {
          this.buoyData.forEach((item) => {
            // console.log(item.buoyId);
            if (item.buoyId == feature.values_.idName) {

              overlayForm.setPosition(mouse); // 设置弹窗的位置



              });
            }
          });
        }
        //  else {
        //   this.msgModal = false;
        //   overlayForm.setPosition(undefined); // 如果没有点击坐标点,就隐藏弹窗
        // }
      });
    },
      //标记
    setMarker(item) {

        let feature = new Feature({
          id: item.customSortId,
          geometry: new Point(
            transform(
              [item.target.position.lon, item.target.position.lat],
              "EPSG:4326",
              "EPSG:3857"
            )
          ),
        });

        feature.setStyle(
          new Style({
            image: new Icon({
              src: require("../components/screenHeader/images/greenShip.svg"),
              scale: 1.1,
              // rotation: (item.heading * Math.PI) / 180,
            }),
          })
        );
        this.vectorSource.addFeature(feature);
 // 使用Overlay添加GIF动态图标点位信息 (我这个要打动图,试了下还是加个图层,方法在下面,用了iview组件,里面的一个通知提醒,有预警的话就弹出,由于里面的内容不好改,就使用了render函数)
    addGif(lon, lat, id) {
      const that = this;
      let mapDom = that.$refs.container;
      var oDiv = document.createElement("div");
      oDiv.id = id;
      oDiv.style.width = "40px";
      oDiv.style.height = "40px";
      oDiv.style.backgroundImage =
        "url( " + require("../components/screenHeader/images/run.gif") + ")";
      oDiv.style.backgroundSize = "100%";
      mapDom.appendChild(oDiv);
      this.markerPoint = new Overlay({
        position: fromLonLat([lon, lat]),
        positioning: "center-center",
        element: document.getElementById(id),
        id: id,
        stopEvent: true,
      });
      this.map.addOverlay(this.markerPoint);
      mapDom.removeChild(oDiv);
    },

}
    //弹出预警信息
    warning() {
      const that = this;
      this.$refs.audiod.play();//弹出预警伴随有声音提醒
      this.$Notice.open({
        render: (h) => {
          return h(
            "div",
            {
              style: {
                width: "100%",
                fontSize: "14px",
              },
            },
            [

              h(
                "Button",
                {
                  style: {
                    marginTop: "10px",
                    marginLeft: "10px",
                    width: "21%",
                  },
                  on: {
                    click: () => {
                      that.map.removeOverlay(that.markerPoint);
                      this.addGif(lon, lat, that.warnList.warnId);
                    },
                  },
                },
                ["定位"]
              ),

        },

        duration: 0,
        onClose() {
          that.map.removeOverlay(that.markerPoint);//关闭时清除gif图层
        },
      });
      // var div = document.querySelector(".ivu-notice");
      // console.log(div, "111");
    },

3.图片:


总结

完成!

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

vue+openlayers实现地图打点 的相关文章

随机推荐

  • eclipse安装教程(2021最新版)超级易懂到吐血

    第一步 下载JDK 下载地址 http www oracle com technetwork java javase downloads index html 第二步 根据自己电脑的系统 选择相应的版本x64代表64位 x86代表32位 点
  • 螺杆真空泵安装流程图_螺杆式真空泵基本知识送给刚入行的新朋友

    螺杆式真空泵是容积式真空泵中的新兴成员 出现于上世纪90年代前后 发展较晚 但作为一种理想干泵 螺杆式真空泵在面世后获得了快速发展 现在就跟小编去了解一下它的基本知识吧 一 螺杆式真空泵特点 螺杆式真空泵脱胎于螺杆式压缩机与螺杆液体输送泵
  • 精英反向与二次插值改进的黏菌算法-附代码

    精英反向与二次插值改进的黏菌算法 文章目录 精英反向与二次插值改进的黏菌算法 1 黏菌算法 2 改进黏菌算法 2 1 精英反向学习机制 2 2 二次插值方法 3 实验结果 4 参考文献 5 Matlab代码 6 python代码 摘要 针对
  • 关于集合Collection

    集合框架 概念 1 Collection是所有集合的顶级接口 2 集合和数组一样 可以保存一组元素 并且提供了操作元素的相关方法 使用更方便 3 Collection 下面有多种实现类 因此我们有更多的数据结构可以选择 Collection
  • 蓝桥杯2020年第十一届省赛真题-子串分值

    题目 题目链接 题解 思维 考虑每个字符对最终答案的贡献 每个字符的贡献为其左侧连续与之不相同的字符个数 1乘以其右侧与之不相同的字符个数 1 样例 ababc 第一个字符a的贡献 0 1 1 1 2 a ab 第二个字符b的贡献 1 1
  • Java使用poi-tl1.9.1生成Word文档的几个小技巧

    目录 前言 一 poi tl简介 1 什么是poi tl 2 常见的word生成对比 3 poi tl功能点 二 poi tl文档生成 1 模板准备 2 目标参数填充 3 生成效果 三 可能会遇到的问题 1 混合图表生成报错 2 图表参数设
  • Linux /etc/resolv.conf DNS服务器IP地址修改被覆盖问题

    为了临时修改DNS服务器IP地址 可能会选择手动修改 etc resolv conf 文件 而在重启Network Manager服务后 sudo systemctl restart NetworkManager resolv conf 文
  • mvc三层架构(用户信息管理系统)

    mvc三层架构 实战项目 用户信息管理系统 一 三层架构 View 层 用于接收用户提交请求的代码 Service 层 系统的业务逻辑主要在这里完成 Dao 层 直接操作数据库的代码 二 三层架构图 三 用户信息管理系统 利用MVC三层架构
  • 金蝶K3客户端:组件KdSvrMgr无法正常工作 排查分析步骤

    远程组件配置工具无法测试通过 并出错 对于以上错误 在其他的地方还会表现为 拒绝的权限 这样子的信息 其实问题实质是一样的 分析如下 1 远程中间层机器和本机网络不通 可以使用ping命令确认是否网络通畅 如果网络通了还是问题依旧 进入分析
  • LeetCode 448. Find All Numbers Disappeared in an Array

    原题网址 https leetcode com problems find all numbers disappeared in an array Given an array of integers where 1 a i n n siz
  • 多项式与快速傅里叶变换(FFT)

    上次算导老师讲分治高精度乘法 n 1 8左右的复杂度 并且说acm里就有这个 然后我小声bb说acm里高精度快速乘是nlogn的 然后一阵虚因为自己不会FFT 今天算法课又提到了并且我和同学吹牛快速傅里叶变换只要nlogn巴拉巴拉 然后又一
  • 使用kubebuilder结合code-generator开发k8s controller(1)

    为了开了controller 先后分析和尝试了几周 现把步骤和踩的坑记录分享一下 使用kubebuilder结合code generator开发k8s controller 1 使用kubebuilder结合code generator开发
  • 查看matlab中函数源代码的方法

    有几种方法可以实现查看matlab里自带函数的源代码 在命令窗口中输入 1 type 函数名 如 type rgb2gray 或者 type rgb2gray m 即可在命令窗口中显示此函数的源代码 2 open 函数名 如 open rg
  • 数据结构与算法--用c语言建立顺序表以及其相关操作

    一 线性表的介绍 线性表分为顺序表和单链表 线性表是数据结构最基础的结构之一 他们与栈 队列 串和数组都属于线性结构 由n个 n gt 0 个数据特性相同的元素构成的有限序列称为线性表 n称为线性表的长度 n 0时称为空表 对于非空的线性表
  • (UE4 4.20 ) UE4的Actor生命周期(1) 之 SpawnActor , SpawnActorDeferred 产生 Actor 和 AActor 初始化的执行流程

    在https docs unrealengine com en us Programming UnrealArchitecture Actors ActorLifecycle 官方的编程文档中 UE4官方给出了有关Actor生命周期的宝贵资
  • 如何用Python写一个爬虫

    在当今的互联网时代 网络爬虫已经成为了一种非常重要的技术手段 通过爬虫 我们可以快速地获取大量的数据并进行分析 这对于很多行业都非常有帮助 在本篇文章中 我们将详细介绍如何用Python写一个爬虫 1 爬虫的基本原理 在开始编写爬虫之前 我
  • C++类属(泛型)——模板详解

    第八章 类属 泛型 模板 1 概述 在程序设计中经常会用到这样的一些程序实体 这些程序实体的实现和所完成的基本功能相同 不同的地方仅在于它们所涉及的数据类型不同 对于这些函数或类 如果能分别只用一个函数和一个类来描述它们 将会大大简化程序设
  • 设置openEuler(欧拉系统)安装源

    在安装openEuler时 完整的 ISO映像 使用的是ISO的源一般不用设置安装源 安装netinst版需要连网下载文件 所以需要设置安装源 下面将按照提示一步步配置 先设置网络连网 然后点完成 选择硬盘 默认自动分区 添加安装源 官方列
  • 使用MicroPython制作红绿灯模拟器

    我们将使用行人步行按钮实现交通信号灯 该项目与LED配合使用 这使我们能够在代码执行时看到其状态 对于交通信号灯 也称为刹车灯 我们将使用红色 黄色和绿色的LED来匹配交通信号灯上的相同颜色的灯 我们还将使用红色和黄色的LED来表示 请勿行
  • vue+openlayers实现地图打点

    前言 openlayers的使用 一 使用步骤 1 引入库 代码如下 示例 npm install ol import ol ol css 样式 import Map from ol Map 地图对象 import Feature from