Vue+Openlayers實現(xiàn)實時坐標點展示
更新時間:2022年03月30日 11:05:32 作者:小小并不小
這篇文章主要為大家詳細介紹了Vue+Openlayers實現(xiàn)實時坐標點展示,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了Vue+Openlayers實現(xiàn)實時坐標點展示的具體代碼,供大家參考,具體內(nèi)容如下
直接上代碼
<!-- ?* @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數(shù)據(jù)接收 ? ? // 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, ? ? ? ? }); ? ? ? ? // 設(shè)置Feature的樣式,使用小旗子圖標 ? ? ? ? that.feature.setStyle( ? ? ? ? ? new Style({ ? ? ? ? ? ? image: new Icon({ ? ? ? ? ? ? ? anchor: [0, 1], ? ? ? ? ? ? ? src: img, ? ? ? ? ? ? }), ? ? ? ? ? ? text: new Text({ ? ? ? ? ? ? ? // 位置 ? ? ? ? ? ? ? textAlign: "center", ? ? ? ? ? ? ? // 基準線 ? ? ? ? ? ? ? textBaseline: "middle", ? ? ? ? ? ? ? // 文字樣式 ? ? ? ? ? ? ? font: "normal 20px 微軟雅黑", ? ? ? ? ? ? ? // 文本內(nèi)容 ? ? ? ? ? ? ? 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>
效果圖
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
element中el-container容器與div布局區(qū)分詳解
這篇文章主要介紹了element中el-container容器與div布局區(qū)分詳解,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-05-05VUE中的export default和export使用方法解析
export default和export都能導(dǎo)出一個模塊里面的常量,函數(shù),文件,模塊等,在其它文件或模塊中通過import來導(dǎo)入常量,函數(shù),文件或模塊。但是,在一個文件或模塊中export,import可以有多個,export default卻只能有一個。2022-12-12element中el-table局部刷新的實現(xiàn)示例
本文主要介紹了element中el-table局部刷新的實現(xiàn)示例,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-04-04Vue項目打包部署到GitHub Pages的實現(xiàn)步驟
本文主要介紹了Vue項目打包部署到GitHub Pages的實現(xiàn)步驟,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-04-04