Vue+Openlayers實(shí)現(xiàn)實(shí)時(shí)坐標(biāo)點(diǎn)展示
本文實(shí)例為大家分享了Vue+Openlayers實(shí)現(xiàn)實(shí)時(shí)坐標(biāo)點(diǎn)展示的具體代碼,供大家參考,具體內(nèi)容如下
直接上代碼
<!--
?* @Description: 實(shí)時(shí)坐標(biāo)點(diǎn)
?* @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, // 當(dāng)前地圖寬
? ? ? imgy: 0, // 當(dāng)前地圖高
? ? ? 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;
? ? ? // 默認(rèn)地圖
? ? ? let mapLayer = new ImageLayer({ ?
? ? ? ? source: new ImageStatic({
? ? ? ? ? url: staticMap,
? ? ? ? ? projection: projection,
? ? ? ? ? imageExtent: extent
? ? ? ? })
? ? ? })
? ? ? // 繪制點(diǎn)
? ? ? 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;
? ? // },
?
? ? // 點(diǎn)
? ? 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的樣式,使用小旗子圖標(biāo)
? ? ? ? that.feature.setStyle(
? ? ? ? ? new Style({
? ? ? ? ? ? image: new Icon({
? ? ? ? ? ? ? anchor: [0, 1],
? ? ? ? ? ? ? src: img,
? ? ? ? ? ? }),
? ? ? ? ? ? text: new Text({
? ? ? ? ? ? ? // 位置
? ? ? ? ? ? ? textAlign: "center",
? ? ? ? ? ? ? // 基準(zhǔn)線
? ? ? ? ? ? ? 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: "點(diǎn)-1", x: 497.08, y: 187.88, z: 0},
? ? ? ? {id: 2, name: "點(diǎn)-2", x: 725.32, y: 565.88, z: 0},
? ? ? ? {id: 3, name: "點(diǎn)-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)文章
Vue2.0+ElementUI實(shí)現(xiàn)查詢條件展開和收起功能組件(代碼示例)
文章介紹了如何將查詢條件表單封裝成一個(gè)通用組件,以提高開發(fā)效率,組件支持多條件的折疊和展開功能,并提供了使用示例,感興趣的朋友一起看看吧2025-01-01
Vue-cli項(xiàng)目部署到Nginx服務(wù)器的方法
這篇文章主要介紹了Vue-cli項(xiàng)目部署到Nginx服務(wù)器的方法,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-11-11
element?ui?watch?el-input賦值之后無法刪除或修改問題
這篇文章主要介紹了element?ui?watch?el-input賦值之后無法刪除或修改問題及解決,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-02-02
vue中報(bào)錯(cuò)Duplicate?keys?detected:'1'.?This?may?c
我們在vue開發(fā)過程中常會遇到一些錯(cuò)誤,這篇文章主要給大家介紹了關(guān)于vue中報(bào)錯(cuò)Duplicate?keys?detected:‘1‘.?This?may?cause?an?update?error的解決方法,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-03-03
Vue3+Vite+TS實(shí)現(xiàn)二次封裝element-plus業(yè)務(wù)組件sfasga
這篇文章主要介紹了在Vue3+Vite+TS的基礎(chǔ)上實(shí)現(xiàn)二次封裝element-plus業(yè)務(wù)組件sfasga,下面文章也將圍繞實(shí)現(xiàn)二次封裝element-plus業(yè)務(wù)組件sfasga的相關(guān)介紹展開相關(guān)內(nèi)容,具有一定的參考價(jià)值,需要的小伙伴可惡意參考一下2021-12-12
vue 實(shí)現(xiàn)在函數(shù)中觸發(fā)路由跳轉(zhuǎn)的示例
今天小編就為大家分享一篇vue 實(shí)現(xiàn)在函數(shù)中觸發(fā)路由跳轉(zhuǎn)的示例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-09-09
基于 flexible 的 Vue 組件:Toast -- 顯示框效果
這篇文章主要介紹了基于 flexible 的 Vue 組件:Toast -- 顯示框效果,需要的朋友可以參考下2017-12-12

