mirror of
https://github.com/pure-admin/vue-pure-admin.git
synced 2025-11-21 14:13:36 +08:00
refactor: use setup refactor
This commit is contained in:
@@ -1,6 +1,5 @@
|
||||
import { App } from "vue";
|
||||
import amap from "./src/Amap.vue";
|
||||
import baiduMap from "./src/BaiduMap.vue";
|
||||
|
||||
export const Amap = Object.assign(amap, {
|
||||
install(app: App) {
|
||||
@@ -8,13 +7,6 @@ export const Amap = Object.assign(amap, {
|
||||
}
|
||||
});
|
||||
|
||||
export const BaiduMap = Object.assign(baiduMap, {
|
||||
install(app: App) {
|
||||
app.component(baiduMap.name, baiduMap);
|
||||
}
|
||||
});
|
||||
|
||||
export default {
|
||||
Amap,
|
||||
BaiduMap
|
||||
Amap
|
||||
};
|
||||
|
||||
@@ -1,45 +1,17 @@
|
||||
<template>
|
||||
<div
|
||||
id="mapview"
|
||||
ref="mapview"
|
||||
v-loading="loading"
|
||||
element-loading-text="地图加载中"
|
||||
element-loading-spinner="el-icon-loading"
|
||||
element-loading-background="rgba(0, 0, 0, 0.8)"
|
||||
></div>
|
||||
</template>
|
||||
|
||||
<script lang="ts">
|
||||
<script setup lang="ts">
|
||||
import AMapLoader from "@amap/amap-jsapi-loader";
|
||||
import {
|
||||
reactive,
|
||||
toRefs,
|
||||
defineComponent,
|
||||
onBeforeMount,
|
||||
getCurrentInstance
|
||||
} from "vue";
|
||||
|
||||
import { reactive, getCurrentInstance, onBeforeMount, onUnmounted } from "vue";
|
||||
import { mapJson } from "/@/api/mock";
|
||||
import { deviceDetection } from "/@/utils/deviceDetection";
|
||||
|
||||
import greenCar from "/@/assets/green.png";
|
||||
|
||||
let MarkerCluster;
|
||||
|
||||
export interface MapConfigureInter {
|
||||
// eslint-disable-next-line no-undef
|
||||
on: Fn;
|
||||
// eslint-disable-next-line no-undef
|
||||
destroy?: Fn;
|
||||
// eslint-disable-next-line no-undef
|
||||
clearEvents?: Fn;
|
||||
// eslint-disable-next-line no-undef
|
||||
addControl?: Fn;
|
||||
// eslint-disable-next-line no-undef
|
||||
setCenter?: Fn;
|
||||
// eslint-disable-next-line no-undef
|
||||
setZoom?: Fn;
|
||||
// eslint-disable-next-line no-undef
|
||||
plugin?: Fn;
|
||||
}
|
||||
|
||||
@@ -47,116 +19,119 @@ export interface mapInter {
|
||||
loading: boolean;
|
||||
}
|
||||
|
||||
export default defineComponent({
|
||||
name: "Amap",
|
||||
setup() {
|
||||
const instance = getCurrentInstance();
|
||||
let map: MapConfigureInter;
|
||||
let MarkerCluster;
|
||||
let map: MapConfigureInter;
|
||||
|
||||
const mapSet: mapInter = reactive({
|
||||
loading: deviceDetection() ? false : true
|
||||
const instance = getCurrentInstance();
|
||||
|
||||
const mapSet: mapInter = reactive({
|
||||
loading: deviceDetection() ? false : true
|
||||
});
|
||||
|
||||
// 地图创建完成(动画关闭)
|
||||
const complete = (): void => {
|
||||
if (map) {
|
||||
map.on("complete", () => {
|
||||
mapSet.loading = false;
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
// 地图创建完成(动画关闭)
|
||||
const complete = (): void => {
|
||||
if (map) {
|
||||
map.on("complete", () => {
|
||||
mapSet.loading = false;
|
||||
});
|
||||
}
|
||||
};
|
||||
onBeforeMount(() => {
|
||||
if (!instance) return;
|
||||
let { MapConfigure } = instance.appContext.config.globalProperties.$config;
|
||||
let { options } = MapConfigure;
|
||||
|
||||
// 销毁地图实例
|
||||
const destroyMap = (): void => {
|
||||
if (map) {
|
||||
map.destroy() && map.clearEvents("click");
|
||||
}
|
||||
};
|
||||
AMapLoader.load({
|
||||
key: MapConfigure.amapKey,
|
||||
version: "2.0",
|
||||
plugins: ["AMap.MarkerCluster"]
|
||||
})
|
||||
.then(AMap => {
|
||||
// 创建地图实例
|
||||
map = new AMap.Map(instance.refs.mapview, options);
|
||||
|
||||
onBeforeMount(() => {
|
||||
if (!instance) return;
|
||||
let { MapConfigure } =
|
||||
instance.appContext.config.globalProperties.$config;
|
||||
let { options } = MapConfigure;
|
||||
//地图中添加地图操作ToolBar插件
|
||||
map.plugin(["AMap.ToolBar", "AMap.MapType"], () => {
|
||||
map.addControl(new AMap.ToolBar());
|
||||
//地图类型切换
|
||||
map.addControl(
|
||||
new AMap.MapType({
|
||||
defaultType: 0
|
||||
})
|
||||
);
|
||||
});
|
||||
|
||||
AMapLoader.load({
|
||||
key: MapConfigure.amapKey,
|
||||
version: "2.0",
|
||||
plugins: ["AMap.MarkerCluster"]
|
||||
})
|
||||
.then(AMap => {
|
||||
// 创建地图实例
|
||||
map = new AMap.Map(instance.refs.mapview, options);
|
||||
|
||||
//地图中添加地图操作ToolBar插件
|
||||
map.plugin(["AMap.ToolBar", "AMap.MapType"], () => {
|
||||
map.addControl(new AMap.ToolBar());
|
||||
//地图类型切换
|
||||
map.addControl(
|
||||
new AMap.MapType({
|
||||
defaultType: 0
|
||||
})
|
||||
);
|
||||
});
|
||||
|
||||
MarkerCluster = new AMap.MarkerCluster(map, [], {
|
||||
gridSize: 80, // 聚合网格像素大小
|
||||
maxZoom: 14,
|
||||
renderMarker(ctx) {
|
||||
let { marker, data } = ctx;
|
||||
if (Array.isArray(data) && data[0]) {
|
||||
var { driver, plateNumber, orientation } = data[0];
|
||||
var content = `<img style="transform: scale(1) rotate(${
|
||||
360 - Number(orientation)
|
||||
}deg);" src='${greenCar}' />`;
|
||||
marker.setContent(content);
|
||||
marker.setLabel({
|
||||
direction: "bottom",
|
||||
offset: new AMap.Pixel(-4, 0), //设置文本标注偏移量
|
||||
content: `<div> ${plateNumber}(${driver})</div>` //设置文本标注内容
|
||||
});
|
||||
marker.setOffset(new AMap.Pixel(-18, -10));
|
||||
marker.on("click", ({ lnglat }) => {
|
||||
map.setZoom(13); //设置地图层级
|
||||
map.setCenter(lnglat);
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
// 获取模拟车辆信息
|
||||
mapJson()
|
||||
.then(res => {
|
||||
let points: object = res.info.map((v: any) => {
|
||||
return {
|
||||
lnglat: [v.lng, v.lat],
|
||||
...v
|
||||
};
|
||||
});
|
||||
if (MarkerCluster) MarkerCluster.setData(points);
|
||||
})
|
||||
.catch(err => {
|
||||
console.log("err:", err);
|
||||
MarkerCluster = new AMap.MarkerCluster(map, [], {
|
||||
// 聚合网格像素大小
|
||||
gridSize: 80,
|
||||
maxZoom: 14,
|
||||
renderMarker(ctx) {
|
||||
let { marker, data } = ctx;
|
||||
if (Array.isArray(data) && data[0]) {
|
||||
var { driver, plateNumber, orientation } = data[0];
|
||||
var content = `<img style="transform: scale(1) rotate(${
|
||||
360 - Number(orientation)
|
||||
}deg);" src='${greenCar}' />`;
|
||||
marker.setContent(content);
|
||||
marker.setLabel({
|
||||
direction: "bottom",
|
||||
//设置文本标注偏移量
|
||||
offset: new AMap.Pixel(-4, 0),
|
||||
//设置文本标注内容
|
||||
content: `<div> ${plateNumber}(${driver})</div>`
|
||||
});
|
||||
marker.setOffset(new AMap.Pixel(-18, -10));
|
||||
marker.on("click", ({ lnglat }) => {
|
||||
map.setZoom(13); //设置地图层级
|
||||
map.setCenter(lnglat);
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
complete();
|
||||
// 获取模拟车辆信息
|
||||
mapJson()
|
||||
.then(res => {
|
||||
let points: object = res.info.map((v: any) => {
|
||||
return {
|
||||
lnglat: [v.lng, v.lat],
|
||||
...v
|
||||
};
|
||||
});
|
||||
if (MarkerCluster) MarkerCluster.setData(points);
|
||||
})
|
||||
.catch(() => {
|
||||
mapSet.loading = false;
|
||||
throw "地图加载失败,请重新加载";
|
||||
.catch(err => {
|
||||
console.log("err:", err);
|
||||
});
|
||||
});
|
||||
|
||||
return {
|
||||
...toRefs(mapSet),
|
||||
complete,
|
||||
destroyMap,
|
||||
greenCar
|
||||
};
|
||||
complete();
|
||||
})
|
||||
.catch(() => {
|
||||
mapSet.loading = false;
|
||||
throw "地图加载失败,请重新加载";
|
||||
});
|
||||
});
|
||||
|
||||
onUnmounted(() => {
|
||||
if (map) {
|
||||
// 销毁地图实例
|
||||
map.destroy() && map.clearEvents("click");
|
||||
}
|
||||
});
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<div
|
||||
id="mapview"
|
||||
ref="mapview"
|
||||
v-loading="mapSet.loading"
|
||||
element-loading-text="地图加载中"
|
||||
element-loading-spinner="el-icon-loading"
|
||||
element-loading-background="rgba(0, 0, 0, 0.8)"
|
||||
></div>
|
||||
</template>
|
||||
|
||||
<style lang="scss" scoped>
|
||||
#mapview {
|
||||
height: 100%;
|
||||
|
||||
@@ -1,14 +0,0 @@
|
||||
<template>
|
||||
<div></div>
|
||||
</template>
|
||||
|
||||
<script lang="ts">
|
||||
export default {
|
||||
name: "BaiduMap",
|
||||
setup() {
|
||||
return {};
|
||||
}
|
||||
};
|
||||
</script>
|
||||
|
||||
<style scoped></style>
|
||||
Reference in New Issue
Block a user