VUE + OPENLAYERS實(shí)現(xiàn)實(shí)時(shí)定位功能
前言
本系列文章介紹一個(gè)簡單的實(shí)時(shí)定位示例,示例的組成主要包括:
- 服務(wù)后端,使用 Java 語言編寫,模擬生成 GeoJSON 數(shù)據(jù)。
- 前端展示,使用 Vue + OpenLayers ,負(fù)責(zé)定時(shí)向后端服務(wù)請(qǐng)求 GeoJSON 數(shù)據(jù),并在以標(biāo)簽的形式展現(xiàn)定位數(shù)據(jù)。
實(shí)現(xiàn)的效果:
一、定義標(biāo)簽樣式
var image = new CircleStyle({ radius: 5, fill: new Fill({ color: "rgba(255, 0, 0, 1)" }), stroke: new Stroke({ color: "red", width: 1 }) }); var styles = { Point: new Style({ image: image }) }; var styleFunction = function(feature) { return styles[feature.getGeometry().getType()]; };
二、模擬 GeoJSON 數(shù)據(jù)
var geojsonObject = { type: "FeatureCollection", features: [ { type: "Feature", geometry: { type: "Point", coordinates: [0, 0] } } //此處可以添加更多 feature ] };
三、創(chuàng)建 VerctorLayer
//讀取 GeoJSON, 將其作為 vectorSource 的數(shù)據(jù)源 var vectorSource = new VectorSource({ features: new GeoJSON().readFeatures(geojsonObject) }); var vectorLayer = new VectorLayer({ source: vectorSource, style: styleFunction });
四、構(gòu)建地圖
mounted() { this.map = new Map({ layers: [ new TileLayer({ source: new OSM() }), vectorLayer ], target: "map", view: new View({ center: [0, 0], zoom: 2 }) }); //設(shè)置定時(shí)任務(wù),調(diào)用移動(dòng)標(biāo)簽方法 setInterval(this.translate, 500); },
五、模擬實(shí)時(shí)移動(dòng)
methods: { translate() { //遍歷標(biāo)簽, 修改坐標(biāo)位置 vectorSource.forEachFeature(function(f) { console.log("translate"); //隨機(jī)產(chǎn)生坐標(biāo)增量(此處不是坐標(biāo)絕對(duì)值!!!!) var x = Math.random() * 1000000; var y = Math.random() * 1000000; f.getGeometry().translate(x, y); }); } }
總結(jié)
以上是一個(gè)簡單實(shí)時(shí)定位前端示例,通過模擬的 GeoJSON 對(duì)象展示標(biāo)簽,并通過定時(shí)任務(wù)模擬標(biāo)簽位置變化。下一篇將使用 Java 服務(wù)端提供位置數(shù)據(jù),完整模擬一個(gè)實(shí)時(shí)定位系統(tǒng)。
可以在vue項(xiàng)目中直接運(yùn)行的完整代碼:
<template> <div> <span>hi, map</span> <div id="map" class="map"></div> </div> </template> <script lang="ts"> import "ol/ol.css"; import GeoJSON from "ol/format/GeoJSON"; import Map from "ol/Map"; import View from "ol/View"; import { Circle as CircleStyle, Fill, Stroke, Style } from "ol/style"; import { OSM, Vector as VectorSource } from "ol/source"; import { Tile as TileLayer, Vector as VectorLayer } from "ol/layer"; import Vue from "vue"; var image = new CircleStyle({ radius: 5, fill: new Fill({ color: "rgba(255, 0, 0, 1)" }), stroke: new Stroke({ color: "red", width: 1 }) }); var styles = { Point: new Style({ image: image }) }; var styleFunction = function(feature) { return styles[feature.getGeometry().getType()]; }; var geojsonObject = { type: "FeatureCollection", features: [ { type: "Feature", geometry: { type: "Point", coordinates: [0, 0] } } ] }; var vectorSource = new VectorSource({ features: new GeoJSON().readFeatures(geojsonObject) }); var vectorLayer = new VectorLayer({ source: vectorSource, style: styleFunction }); export default Vue.extend({ data() { return { map: {} }; }, mounted() { this.map = new Map({ layers: [ new TileLayer({ source: new OSM() }), vectorLayer ], target: "map", view: new View({ center: [0, 0], zoom: 2 }) }); setInterval(this.translate, 500); }, methods: { translate() { vectorSource.forEachFeature(function(f) { console.log("translate"); var x = Math.random() * 1000000; var y = Math.random() * 1000000; f.getGeometry().translate(x, y); }); } } }); </script> <style> .map { width: 100%; height: 600px; } </style>
到此這篇關(guān)于VUE + OPENLAYERS實(shí)現(xiàn)實(shí)時(shí)定位功能的文章就介紹到這了,更多相關(guān)VUE OPENLAYERS 定位內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Vue利用openlayers實(shí)現(xiàn)點(diǎn)擊彈窗的方法詳解
- Vue使用openlayers實(shí)現(xiàn)繪制圓形和多邊形
- Vue結(jié)合Openlayers使用Overlay添加Popup彈窗實(shí)現(xiàn)
- vue使用openlayers創(chuàng)建地圖
- Vue+Openlayers實(shí)現(xiàn)實(shí)時(shí)坐標(biāo)點(diǎn)展示
- vue利用openlayers加載天地圖和高德地圖
- Vue + OpenLayers 快速入門學(xué)習(xí)教程
- vue+openlayers繪制省市邊界線
- Vue openLayers實(shí)現(xiàn)圖層數(shù)據(jù)切換與加載流程詳解
相關(guān)文章
vue計(jì)算屬性+vue中class與style綁定(推薦)
這篇文章主要介紹了vue計(jì)算屬性+vue中class與style綁定,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-03-03vue element ui validate 主動(dòng)觸發(fā)錯(cuò)誤提示操作
這篇文章主要介紹了vue element ui validate 主動(dòng)觸發(fā)錯(cuò)誤提示操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-09-09vue+vant實(shí)現(xiàn)商品列表批量倒計(jì)時(shí)功能
這篇文章主要介紹了vue+vant實(shí)現(xiàn)商品列表批量倒計(jì)時(shí)功能,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-01-01關(guān)于Element-ui中table默認(rèn)選中toggleRowSelection問題
這篇文章主要介紹了關(guān)于Element-ui中table默認(rèn)選中toggleRowSelection問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-08-08vue框架編輯接口頁面下拉級(jí)聯(lián)選擇并綁定接口所屬模塊
這篇文章主要為大家介紹了vue框架編輯接口頁面實(shí)現(xiàn)下拉級(jí)聯(lián)選擇以及綁定接口所屬模塊,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05vue3+element-plus?Dialog對(duì)話框的使用與setup?寫法的用法
這篇文章主要介紹了vue3+element-plus?Dialog對(duì)話框的使用?與?setup?寫法的使用,本文通過兩種方式結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-04-04