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)文章
element中el-container容器與div布局區(qū)分詳解
這篇文章主要介紹了element中el-container容器與div布局區(qū)分詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-05-05
VUE中的export default和export使用方法解析
export default和export都能導(dǎo)出一個(gè)模塊里面的常量,函數(shù),文件,模塊等,在其它文件或模塊中通過import來導(dǎo)入常量,函數(shù),文件或模塊。但是,在一個(gè)文件或模塊中export,import可以有多個(gè),export default卻只能有一個(gè)。2022-12-12
element中el-table局部刷新的實(shí)現(xiàn)示例
本文主要介紹了element中el-table局部刷新的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-04-04
vue實(shí)現(xiàn)時(shí)間倒計(jì)時(shí)功能
這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)時(shí)間倒計(jì)時(shí)功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-08-08
Vue項(xiàng)目打包部署到GitHub Pages的實(shí)現(xiàn)步驟
本文主要介紹了Vue項(xiàng)目打包部署到GitHub Pages的實(shí)現(xiàn)步驟,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-04-04

