地图打点动态数据加载,

This commit is contained in:
fengerli
2025-08-05 19:02:04 +08:00
parent d19dc91223
commit 6c5a702ff9
10 changed files with 208 additions and 86 deletions

View File

@ -1,73 +1,151 @@
<template>
<view class="map-container">
<map style="width: 100%; height: 100%;" :latitude="latitude" :longitude="longitude" :markers="markers"
:custom-map-style="customMapStyle" :enable-custom="true">
</map>
<view>
<view class="page-body">
<view class="page-section page-section-gap">
<map style="width: 100%; height: 100vh;"
:latitude="latitude"
:longitude="longitude"
:markers="covers"
@markertap="onMarkerTap"
:scale="16">
</map>
</view>
</view>
<!-- 加载提示 -->
<view v-if="loading" class="loading-mask">
<view class="loading-content">地图加载中...</view>
<view class="loading-content">加载中...</view>
</view>
</view>
</template>
<script>
export default {
data() {
return {
loading: true, // 默认显示加载提示
latitude: 30.592,
longitude: 114.305,
customMapStyle: '8c3efc37298895fd78e6aa0e799e78ce',
markers: [{
id: 1,
latitude: 30.507,
longitude: 114.404,
title: '华中科技大学'
}, {
id: 2,
latitude: 30.476,
longitude: 114.428,
title: '光谷金融港'
}, {
id: 3,
latitude: 30.504,
longitude: 114.433,
title: '武汉东站'
}]
}
},
onLoad() {
// 尝试获取当前位置
uni.getLocation({
type: 'gcj02',
success: (res) => {
this.latitude = res.latitude;
this.longitude = res.longitude;
export default {
data() {
return {
title: 'map',
latitude: 39.909, // 默认纬度
longitude: 116.39742, // 默认经度
covers: [], // 标记点数组
loading: true, // 加载状态
markerData: [] // 存储原始标记数据
}
},
onLoad() {
// 获取事件通道
const eventChannel = this.getOpenerEventChannel();
// 监听传递过来的位置数据
eventChannel.on('Map', (receivedData) => {
this.loading = true;
this.covers = [];
//先获取data属性内容
const dataContent = receivedData.data;
//data是数组
if (Array.isArray(dataContent)) {
// 过滤掉经纬度为空的设备
const validDevices = dataContent.filter(device =>
device.latitude !== null &&
device.longitude !== null &&
device.latitude !== '' &&
device.longitude !== ''
);
this.markerData = validDevices;
if (validDevices.length > 0) {
this.processMultipleMarkers(validDevices);
} else {
console.log('没有有效的设备经纬度数据');
this.loading = false;
}
});
},
onReady() {
// 页面初次渲染完成时,隐藏加载提示
}
//data是单个设备对象
else if (typeof dataContent === 'object' && dataContent !== null) {
if (this.validateMarker(dataContent)) {
this.markerData = [dataContent];
this.processSingleMarker(dataContent);
} else {
console.log('单个设备数据缺少有效的经纬度');
this.loading = false;
}
}
});
},
methods: {
// 处理单个标记点
processSingleMarker(marker) {
// 转换经纬度为数字
const lat = parseFloat(marker.latitude);
const lng = parseFloat(marker.longitude);
// 设置地图中心
this.latitude = lat;
this.longitude = lng;
// 创建标记点
this.covers = [{
id: marker.deviceImei, // 适配deviceId字段
latitude: lat,
longitude: lng,
iconPath: marker.devicePic || '/static/images/common/device.png',
width: 40,
height: 40,
anchor: {x: 0.5, y: 0.5}, // 锚点在中心
callout: {
content: `ID: ${marker.deviceImei}`
}
}];
this.loading = false;
},
// 处理多个标记点
processMultipleMarkers(markers) {
// 使用第一个有效标记点的位置作为地图中心
const firstMarker = markers[0];
this.latitude = parseFloat(firstMarker.latitude);
this.longitude = parseFloat(firstMarker.longitude);
// 转换所有有效标记点
this.covers = markers.map((marker, index) => ({
id: marker.deviceId || marker.id || marker.deviceImei || index + 1,
latitude: parseFloat(marker.latitude),
longitude: parseFloat(marker.longitude),
iconPath: marker.devicePic || '/static/images/common/device.png',
width: 40,
height: 40,
anchor: {x: 0.5, y: 0.5},
callout: {
content: `ID: ${marker.deviceImei}`,
}
}));
this.loading = false;
},
// 验证标记点是否包含必要信息
validateMarker(marker) {
// 验证经纬度是否有效
const hasValidLat = marker.latitude && marker.latitude !== '' && !isNaN(parseFloat(marker.latitude));
const hasValidLng = marker.longitude && marker.longitude !== '' && !isNaN(parseFloat(marker.longitude));
return hasValidLat && hasValidLng;
},
// 标记点点击事件
onMarkerTap(e) {
const clickedMarker = this.markerData.find(
item =>item.deviceImei === e.markerId
);
}
}
}
</script>
<style>
.map-container {
width: 100%;
height: 100vh;
position: relative;
}
/* 加载提示 */
<style>
.loading-mask {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: #121212;
background: rgba(0, 0, 0, 0.5);
display: flex;
justify-content: center;
align-items: center;
@ -75,10 +153,23 @@
}
.loading-content {
color: rgba(255, 255, 255, 0.9);
color: #fff;
font-size: 16px;
padding: 12px 24px;
background: rgba(0, 0, 0, 0.7);
border-radius: 8px;
}
.empty-tip {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
background: rgba(0, 0, 0, 0.7);
color: #fff;
padding: 10px 20px;
border-radius: 4px;
z-index: 99;
}
</style>