vue利用openlayers实现动态轨迹

yizhihongxing

“vue利用openlayers实现动态轨迹”的实现过程中,需要安装openlayers、esri-leaflet等相关库,而vue可以利用npm进行安装,具体步骤如下:

安装依赖库

npm install ol esri-leaflet --save

Vue中引入openlayers和esri-leaflet

import 'ol/ol.css'
import ol from 'ol'
import 'esri-leaflet'

在Vue中利用openlayers实现动态轨迹,则需要实现以下几步:

  1. 初始化地图对象
let map = new ol.Map({
  target: 'map',
  layers: [
    new ol.layer.Tile({
      source: new ol.source.OSM()
    })
  ],
  view: new ol.View({
    center: [0, 0],
    zoom: 2
  })
});
  1. 实例化layer和source
let vectorLayer = new ol.layer.Vector({
  source: new ol.source.Vector({
    features: [] // 存放轨迹的feature
  })
})
map.addLayer(vectorLayer)
  1. 初始化feature
let feature = new ol.Feature({
  geometry: new ol.geom.LineString([]) // 空的轨迹线
})
vectorLayer.getSource().addFeature(feature)
  1. 接收实时位置更新
let newCoords = []
for (let i = 0; i < coords.length; i++) {
  newCoords.push(
    ol.proj.fromLonLat([coords[i].longitude, coords[i].latitude])
  )
}
feature.setGeometry(new ol.geom.LineString(newCoords))
  1. 清除轨迹线
vectorLayer.getSource().clear()
  1. 一条示例:实时更新轨迹线
<template>
  <div>
    <div id="map" style="width: 100%; height: 100vh;"></div>
  </div>
</template>

<script>
import 'ol/ol.css'
import ol from 'ol'
import 'esri-leaflet'

export default {
  name: 'RealtimeTrack',
  data() {
    return {
      map: null,
      feature: null
    }
  },
  mounted() {
    this.initMap()
  },
  methods: {
    initMap() {
      let self = this
      let map = new ol.Map({
        target: 'map',
        layers: [
          new ol.layer.Tile({
            source: new ol.source.OSM()
          })
        ],
        view: new ol.View({
          center: [0, 0],
          zoom: 2
        })
      })
      let vectorLayer = new ol.layer.Vector({
        source: new ol.source.Vector({
          features: []
        })
      })
      map.addLayer(vectorLayer)
      let feature = new ol.Feature({
        geometry: new ol.geom.LineString([])
      })
      vectorLayer.getSource().addFeature(feature)
      self.feature = feature

      setInterval(function() {
        // 模拟实时位置更新
        let newCoords = []
        let coords = self.getNewCoords()
        for (let i = 0; i < coords.length; i++) {
          newCoords.push(
            ol.proj.fromLonLat([coords[i].longitude, coords[i].latitude])
          )
        }
        feature.setGeometry(new ol.geom.LineString(newCoords))
      }, 1000)
    },
    getNewCoords() {
      // 返回一个包含经纬度信息的数组
      return [
        { longitude: 112.9813, latitude: 28.1788 },
        { longitude: 112.9534, latitude: 28.1710 },
        { longitude: 112.9397, latitude: 28.1654 },
        { longitude: 112.9378, latitude: 28.1364 },
        { longitude: 112.9385, latitude: 28.1126 }
      ]
    }
  }
}
</script>

<style scoped>
#map {
  top: 0px;
  left: 0px;
  width: 100%;
  height: 100vh;
  position: absolute;
}
</style>
  1. 另一条示例:清除轨迹线
<template>
  <div>
    <div id="map" style="width: 100%; height: 100vh;"></div>
    <button @click="clearTrack">Clear Track</button>
  </div>
</template>

<script>
import 'ol/ol.css'
import ol from 'ol'
import 'esri-leaflet'

export default {
  name: 'ClearTrack',
  data() {
    return {
      map: null,
      vectorLayer: null
    }
  },
  mounted() {
    this.initMap()
  },
  methods: {
    initMap() {
      let self = this
      let map = new ol.Map({
        target: 'map',
        layers: [
          new ol.layer.Tile({
            source: new ol.source.OSM()
          })
        ],
        view: new ol.View({
          center: [0, 0],
          zoom: 2
        })
      })
      self.map = map
      let vectorLayer = new ol.layer.Vector({
        source: new ol.source.Vector({
          features: []
        })
      })
      map.addLayer(vectorLayer)
      self.vectorLayer = vectorLayer
      let feature = new ol.Feature({
        geometry: new ol.geom.LineString([])
      })
      vectorLayer.getSource().addFeature(feature)

      // 模拟实时位置更新
      setInterval(function() {
        let newCoords = []
        let coords = self.getNewCoords()
        for (let i = 0; i < coords.length; i++) {
          newCoords.push(
            ol.proj.fromLonLat([coords[i].longitude, coords[i].latitude])
          )
        }
        feature.setGeometry(new ol.geom.LineString(newCoords))
      }, 1000)
    },
    clearTrack() {
      this.vectorLayer.getSource().clear()
    },
    getNewCoords() {
      // 返回一个包含经纬度信息的数组
      return [
        { longitude: 112.9813, latitude: 28.1788 },
        { longitude: 112.9534, latitude: 28.1710 },
        { longitude: 112.9397, latitude: 28.1654 },
        { longitude: 112.9378, latitude: 28.1364 },
        { longitude: 112.9385, latitude: 28.1126 }
      ]
    }
  }
}
</script>

<style scoped>
#map {
  top: 0px;
  left: 0px;
  width: 100%;
  height: 100vh;
  position: absolute;
}
</style>

本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:vue利用openlayers实现动态轨迹 - Python技术站

(0)
上一篇 2023年5月28日
下一篇 2023年5月28日

相关文章

  • 详解vue-cli项目在IE浏览器打开报错解决方法

    当使用vue-cli创建的项目在IE浏览器中打开时,可能会遇到Object doesn’t support property or method ‘assign’或Promise未定义等错误。这是因为IE浏览器不支持ES6(ES2015)新特性,而vue-cli默认使用的是ES6语法并使用了一些ES6新特性,这对于IE浏览器来说是无法正确识别的。 下面是解决…

    Vue 2023年5月27日
    00
  • vue项目每30秒刷新1次接口的实现方法

    实现Vue项目每30秒刷新1次接口可以通过以下步骤完成: 安装axios库 可以通过以下命令安装axios: npm install axios –save 在Vue项目中创建一个Data对象来保存需要更新的数据 data() { return { data: [] } } 在Vue的Mounted生命周期钩子函数中初始化请求数据 mounted() { …

    Vue 2023年5月29日
    00
  • Vue实现导航栏菜单

    我会为您详细讲解如何使用Vue实现导航栏菜单。 1. 确定导航栏菜单数据格式 首先我们需要确定导航栏菜单的数据格式,一般而言可以使用以下格式: menuList: [ { name: ‘Home’, path: ‘/’, icon: ‘el-icon-menu’ }, { name: ‘Service’, path: ‘/service’, icon: ‘e…

    Vue 2023年5月27日
    00
  • Vue通过Blob对象实现导出Excel功能示例代码

    Vue通过Blob对象实现导出Excel功能示例代码 在前端开发中常常需要将数据导出为Excel表格,那么在Vue中如何通过Blob对象实现导出Excel功能呢?本文将提供一份完整的攻略供大家参考。 步骤一:安装依赖 我们需要安装两个依赖,分别是xlsx和file-saver,前者用于生成Excel文件,后者用于保存Excel文件。我们可以使用npm来进行安…

    Vue 2023年5月27日
    00
  • Vue-cli框架实现计时器应用

    Vue-cli是一款能够快速搭建Vue项目的脚手架工具。在这篇攻略中,我们将详细讲解如何通过Vue-cli框架实现一个计时器应用。 1. 创建Vue项目 首先,我们需要通过Vue-cli创建一个新的Vue项目。打开命令行工具,执行以下命令: vue create timer-app 其中timer-app为项目名称。执行上述命令后,Vue-cli会自动下载所…

    Vue 2023年5月29日
    00
  • vue2.0移动端滑动事件vue-touch的实例代码

    下面我将详细讲解vue2.0移动端滑动事件vue-touch的实例代码的完整攻略。 简介 Vue-Touch是一个基于HammerJS封装的适用于Vue2.x的触摸插件。它可以在Vue组件中使用v-touch指令,实现触屏事件的绑定和处理,包括Tap、Doubletap、Press、Swipe、Pinch、Rotate等常见的手势事件。 安装 在使用vue-…

    Vue 2023年5月29日
    00
  • vue下载excel文件的四种方法实例

    下面是“vue下载excel文件的四种方法实例”的完整攻略: 1. 基于前端导出Excel库的实现 使用前端导出Excel库(如FileSaver.js),将数据导出为.xlsx或.csv格式的文件,使其能够被浏览器下载。 import { saveAs } from ‘file-saver’; //导入FileSaver.js库 export functi…

    Vue 2023年5月28日
    00
  • Vue3 构建 Web Components使用详解

    Vue3 构建 Web Components使用详解 Web Components 是一种新的 Web 技术,它可以让 Web 开发者更加灵活地创建自定义标签,实现跨框架、跨库的组件化开发。Vue3 作为当前最流行的前端框架之一,也支持使用 Web Components 扩展其功能。 什么是 Web Components Web Components 是一种…

    Vue 2023年5月28日
    00
合作推广
合作推广
分享本页
返回顶部