iot-fontend/src/views/iot/deviceMap/index.vue

160 lines
5.1 KiB
Vue
Raw Normal View History

<template>
<a-layout>
<a-layout-sider :resize-directions="['right']">
<a-row>
<a-col :span="24" v-for="device in deviceList" :key="device.id" @click="selectDevice(device)">
{{ device.name }}
</a-col>
</a-row>
</a-layout-sider>
<a-layout-content>
<div id="container"></div>
</a-layout-content>
</a-layout>
</template>
<script lang="ts" setup>
import { onMounted, ref } from 'vue';
import AMapLoader from '@amap/amap-jsapi-loader';
import { DeviceRecord, queryDeviceList } from '@/api/device';
import "@amap/amap-jsapi-types";
import router from '@/router';
let map: any = null;
const deviceList = ref<DeviceRecord[]>([]);
let selectDevice = (device: DeviceRecord) => {
console.log(device);
}
AMapLoader.load({
key: 'a4e80eed798a56451b226dcfca81b846', // 申请好的Web端开发者Key首次调用 load 时必填
version: '2.0', // 指定要加载的 JSAPI 的版本,缺省时默认为 1.4.15
plugins: [], // 需要使用的的插件列表,如比例尺'AMap.Scale'等
})
.then((AMap) => {
map = new AMap.Map('container', {
// 设置地图容器id
viewMode: '3D', // 是否为3D地图模式
zoom: 11, // 初始化地图级别
center: [116.397428, 39.90923], // 初始化地图中心点位置
});
selectDevice = (device: DeviceRecord) => {
if (device.longitude && device.latitude) {
const lngLat = [device.longitude, device.latitude];
const content = document.createElement('div');
content.className = 'custom-content-marker';
content.innerHTML = `
<img src="https://webapi.amap.com/theme/v1.3/markers/n/mark_b.png" alt="标记点" />`;
// 创建标记点
const marker = new AMap.Marker({
position: lngLat,
content,
});
// 将标记点添加到地图上
map.setCenter(lngLat);
map.setZoom(20);
map.add(marker);
// const infoContent = `
// <div style="width: 220px; padding: 10px; background: #fff; border-radius: 4px; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);">
// <h4 style="margin: 0 0 10px; font-size: 16px; color: #333;">设备信息</h4>
// <p style="margin: 0; font-size: 14px; color: #555;"><strong>设备名称:</strong> ${device.name}</p>
// <p style="margin: 0; font-size: 14px; color: #555;"><strong>硬件版本:</strong> ${device.hardwareVersion}</p>
// <p style="margin: 0; font-size: 14px; color: #555;"><strong>固件版本:</strong> ${device.firmwareVersion}</p>
// <p style="margin: 0; font-size: 14px; color: #555;"><strong>所属产品:</strong> ${device.productId}</p>
// </div>
// `;
const infoContent = [
`<br/><div style="width: 300px;margin-left: 20px;padding-bottom: 10px"><b>设备名称: ${device.name}</b>`,
`<span style="font-size: 16px; color: #333;">硬件版本: ${device.hardwareVersion}</span>`,
`<span style="font-size: 16px; color: #333;">固件版本: ${device.firmwareVersion}</span>`,
`<span style="font-size: 16px; color: #333;">所属产品: ${device.productName}</span>`,
`<a-button style="margin-left: 200px;color: #0960bd" onclick="handleViewDetail('${device.id}')">查看详情
</a-button></div>`
]
const handleViewDetail = (deviceId: number) => {
router.push({ name: 'deviceDetail', params: { id: deviceId } });
};
window.handleViewDetail = handleViewDetail;
// 初始化信息窗体
const infoWindow = new AMap.InfoWindow({
content: infoContent.join('<br>'),
offset: new AMap.Pixel(10, 10), // 调整信息窗体的偏移量
});
// 绑定标记点点击事件
marker.on('click', () => {
infoWindow.open(map, lngLat); // 打开信息窗体
});
} else {
console.warn('设备缺少经纬度信息', device);
}
};
})
.catch((e) => {
console.log(e);
});
// 将依赖于 AMap 的代码放在 then 回调中
onMounted(() => {
// 获取设备列表
queryDeviceList({
current: 1,
size: 10,
})
.then((response) => {
deviceList.value = response.data.records;
})
.catch((error) => {
console.error('获取设备列表失败', error);
});
});
// onUnmounted(() => {
// map?.destroy();
// });
</script>
<style scoped>
#container {
width: 100%;
height: 800px;
}
.custom-content-marker {
position: relative;
width: 25px;
height: 34px;
}
.custom-content-marker img {
width: 100%;
height: 100%;
}
.custom-content-marker .close-btn {
position: absolute;
top: -6px;
right: -8px;
width: 15px;
height: 15px;
font-size: 12px;
background: #ccc;
border-radius: 50%;
color: #fff;
text-align: center;
line-height: 15px;
box-shadow: -1px 1px 1px rgba(10, 10, 10, .2);
}
.custom-content-marker .close-btn:hover{
background: #666;
}
</style>