Vue+Openlayers实现实时坐标点展示

 更新时间:2022年03月30日 11:05:32   作者:小小并不小  
这篇文章主要为大家详细介绍了Vue+Openlayers实现实时坐标点展示,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了Vue+Openlayers实现实时坐标点展示的具体代码,供大家参考,具体内容如下

直接上代码

<!--
 * @Description: 实时坐标点
 * @Author: Dragon
 * @Date: 2020-12-18 10:08:40
 * @LastEditTime: 2020-12-18 15:59:29
 * @LastEditors: Dragon
-->
 
<template>
  <div id="map"></div>
</template>
 
<script>
import "ol/ol.css";
import { Map, View, Feature } from "ol";
import { Image as ImageLayer, Vector as VectorLayer } from "ol/layer";
import { ImageStatic, Vector as VectorSource } from "ol/source";
import { getCenter } from "ol/extent";
import { Projection } from "ol/proj";
 
import { Point } from "ol/geom";
import { Icon, Style, Text, Fill, Stroke } from "ol/style";
 
// import { websocket }  from "./mixins";
import staticMap from "@/assets/map.png";
import img from "@/assets/tx-icon-1.png";
 
 
export default {
  data() {
    return {
      map: null, // 地图
      imgx: 0, // 当前地图宽
      imgy: 0, // 当前地图高
      persons: [], // 人员
      features: [],
      feature: null,
      vectorSource: new VectorSource(),
      timer: null
    };
  },
  // mixins: [websocket],
  watch: {
    persons(val) {
      if (val) {
        this.setFeature();
      }
    },
  },
  methods: {
     // 初始化地图
    initMap() {
      let extent = [0, 0, this.imgx, this.imgy];
      let projection = new Projection({
        extent: extent
      });
      let $this = this;
      // 默认地图
      let mapLayer = new ImageLayer({  
        source: new ImageStatic({
          url: staticMap,
          projection: projection,
          imageExtent: extent
        })
      })
      // 绘制点
      let featureLayer = new VectorLayer({
        source: this.vectorSource,
      });
 
      this.map = new Map({
        target: "map",
        layers: [
          mapLayer,
          featureLayer
        ],
        view: new View({
          projection: projection,
          center: getCenter(extent),
          zoom: 2,
          maxZoom: 18
        })
      });
    },
 
    // WebSocket数据接收
    // getMessage(message) {
      // let res = JSON.parse(message.data);
      // this.persons = res.data;
    // },
 
    // 点
    setFeature() {
      let that = this;
      that.features = [];
      that.vectorSource.clear(); // 先清除
      that.persons.map((item) => {
        that.feature = new Feature({
          geometry: new Point([item.x, item.y]),
          name: item.name,
        });
        // 设置Feature的样式,使用小旗子图标
        that.feature.setStyle(
          new Style({
            image: new Icon({
              anchor: [0, 1],
              src: img,
            }),
            text: new Text({
              // 位置
              textAlign: "center",
              // 基准线
              textBaseline: "middle",
              // 文字样式
              font: "normal 20px 微软雅黑",
              // 文本内容
              text: item.name,
              // 文本填充样式(即文字颜色)
              fill: new Fill({ color: "#aa3300" }),
              stroke: new Stroke({ color: "#ffcc33", width: 2 }),
            }),
          })
        );
        that.features.push(that.feature);
      });
      that.vectorSource.addFeatures(that.features);
    },
  },
  mounted() {
    let img = new Image();
    img.src = staticMap;
    let that = this;
    img.onload = function(res) {
      that.imgx = res.target.width;
      that.imgy = res.target.height;
      that.initMap();
      // that.initWebSocket();
    };
  },
  created() {
    let that = this
    that.timer = setInterval(function() {
      that.persons = [
        {id: 1, name: "点-1", x: 497.08, y: 187.88, z: 0},
        {id: 2, name: "点-2", x: 725.32, y: 565.88, z: 0},
        {id: 3, name: "点-3", x: 643.24, y: 503.96, z: 0}
      ]
      console.warn(that.persons, 22)
    },3000)
  },
  beforeDestroy() {
    clearInterval(this.timer)
  }
};
</script>
 
<style>
#map {
  width: 100%;
  height: calc(100Vh - 50px);
}
</style>

效果图

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • vue组件编写之todolist组件实例详解

    vue组件编写之todolist组件实例详解

    这篇文章主要介绍了vue组件编写之todolist组件的实例讲解,本文给大家介绍的非常详细,需要的朋友可以参考下
    2018-01-01
  • Vue监听页面刷新和关闭功能

    Vue监听页面刷新和关闭功能

    我在做项目的时候,有一个需求,在离开(跳转或者关闭)购物车页面或者刷新购物车页面的时候向服务器提交一次购物车商品数量的变化。这篇文章主要介绍了vue监听页面刷新和关闭功能,需要的朋友可以参考下
    2019-06-06
  • Vue实现本地购物车功能

    Vue实现本地购物车功能

    这篇文章主要为大家详细介绍了Vue实现本地购物车功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-12-12
  • element中el-container容器与div布局区分详解

    element中el-container容器与div布局区分详解

    这篇文章主要介绍了element中el-container容器与div布局区分详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-05-05
  • 详解vue-validator(vue验证器)

    详解vue-validator(vue验证器)

    本篇文章主要介绍了vue-validator(vue验证器),小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-01-01
  • VUE中的export default和export使用方法解析

    VUE中的export default和export使用方法解析

    export default和export都能导出一个模块里面的常量,函数,文件,模块等,在其它文件或模块中通过import来导入常量,函数,文件或模块。但是,在一个文件或模块中export,import可以有多个,export default却只能有一个。
    2022-12-12
  • element中el-table局部刷新的实现示例

    element中el-table局部刷新的实现示例

    本文主要介绍了element中el-table局部刷新的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-04-04
  • .env在mode文件中如何添加注释详解

    .env在mode文件中如何添加注释详解

    这篇文章主要为大家介绍了.env在mode文件中如何添加注释详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-09-09
  • vue实现时间倒计时功能

    vue实现时间倒计时功能

    这篇文章主要为大家详细介绍了vue实现时间倒计时功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-08-08
  • Vue项目打包部署到GitHub Pages的实现步骤

    Vue项目打包部署到GitHub Pages的实现步骤

    本文主要介绍了Vue项目打包部署到GitHub Pages的实现步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-04-04

最新评论