FenXiNspUniapp/pages/maps/index - 副本.vue

457 lines
12 KiB
Vue
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<template>
<view class="container">
<map id="map" ref="map" style="width: 100%; height:2000rpx;" scale="13" :markers="markers" longitude="111.436625"
latitude="36.771975" show-location @markertap="markertap" @callouttap="callouttap">
</map>
<view class="zdxq">
<view class="zdxqitem" @click="switch2Change('团柏河')">
<image src="../../static/zhandian_nongtian_dingwei.png" mode=""></image>
<view class="textss">
团柏河典型小流域监测站
</view>
<!-- <view class="crildon" :style="!issscd?'background:#00ff37':'background: #ccc;'"></view> -->
</view>
<view class="zdxqitem" @click="switch2Change('对竹河')">
<image src="../../static/zhandian_xuqin_dingwei.png" mode=""></image>
<view class="textss">
对竹河典型小流域监测站
</view>
<!-- <view class="crildon" :style="!isssscd?'background:#00ff37':'background: #ccc;'"></view> -->
</view>
<view class="zdxqitem" @click="switch2Change('土壤')">
<image src="../../static/iconsheturangzs.png" mode=""></image>
<view class="textss">
土壤监测
</view>
</view>
</view>
</view>
</template>
<script>
import api from '@/api/api'
import configService from '@/common/service/config.service.js';
export default {
data() {
return {
baseUrl:configService.staticDomainURL,
markers: [],
list: [],
issscd: false, // 团柏河显示状态 (false表示显示)
isssscd: false, // 对竹河显示状态 (false表示显示)
stationInfo: [],
cusLocation: [],
stationlist: [],
stationTwolist:[],
mapCtx: null // 添加地图上下文
};
},
onLoad() {
this.$nextTick(() => {
this.getList()
})
},
onReady() {
// 在onReady中初始化地图上下文
this.mapCtx = uni.createMapContext('map', this);
},
onShow() {
if (!uni.getStorageSync('third_session')) {
uni.reLaunch({
url: '/packDetail/pages/login/login'
})
}
},
methods: {
getList() {
this.markers = []
this.list = []
this.stationInfo = []
this.cusLocation = []
this.stationlist = []
this.stationTwolist = []
uni.showLoading({
title: '加载中'
});
this.$http.get('/applet/survStationInfo/stationMap').then(res => {
// 合并 stationInfo 和 deviceList
const combinedList = [...res.data.data.stationInfo, ...res.data.data.deviceList];
this.stationlist = combinedList;
this.stationTwolist = [...res.data.data.stationInfo];
console.log("合并后的stationlist+++++", this.stationlist);
// 处理所有标记点
this.stationlist.forEach((item, index) => {
item.id = index;
// 统一字段名称,处理 deviceList 和 stationInfo 的字段差异
const markerItem = {
id: item.id,
latitude: Number(item.latitude || item.stationLatitude),
longitude: Number(item.longitude || item.stationLongitude),
stationType: item.stationType || 'device', // deviceList 没有 stationType默认为 'device'
sortNo: item.sortNo || index,
stationName: item.name || item.stationName,
stationCode: item.ids || item.stationCode || item.id,
// 添加分组信息
groupName: item.groupName || '',
// 设备类型
deviceType: item.type || null
};
this.list.push(markerItem);
// 分类处理
if (item.stationType === 'cusLocaltion') {
this.cusLocation.push(item);
}
if (item.stationType === 'orient' || item.stationType === 'livestock') {
this.stationInfo.push(item);
}
});
this.issscd = false
this.isssscd = false
this.qiyeChange()
this.list1query()
this.addDeviceMarkers() // 添加设备标记
// 延迟执行以确保标记已经添加到地图
setTimeout(() => {
this.adjustMapToFitAllMarkers();
}, 500);
uni.hideLoading();
})
},
// 添加设备标记
addDeviceMarkers() {
this.stationlist.forEach((item, index) => {
// 如果是 deviceList 中的设备(有 type 字段但没有 stationType 字段)
if (item.type && !item.stationType) {
let iconPath = '';
let width = 35;
let height = 35;
// 根据设备类型设置不同的图标
switch(item.type) {
case 'soil':
iconPath = this.baseUrl + '/icon/device/soil.png';
width = 40;
height = 40;
break;
case 'camera':
iconPath = this.baseUrl + '/icon/device/cam.png';
width = 45;
height = 45;
break;
default:
iconPath = this.baseUrl + '/icon/device/default.png';
}
const markerItem = {
id: item.id,
iconPath: iconPath,
latitude: Number(item.latitude),
longitude: Number(item.longitude),
width: width,
height: height,
customCallout: {
anchorY: 0,
anchorX: 0,
display: 'ALWAYS'
},
stationType: 'device',
stationName: item.name,
stationCode: item.ids,
deviceType: item.type,
joinCluster: true
};
this.markers.push(markerItem);
}
});
},
// 调整地图视野以包含所有标记点
adjustMapToFitAllMarkers() {
if (this.mapCtx && this.list.length > 0) {
this.mapCtx.includePoints({
points: this.list.map(item => ({
latitude: item.latitude,
longitude: item.longitude
})),
padding: [40, 40, 40, 40] // 上下左右的边距
});
}
},
// 调整地图视野到特定区域的标记点
adjustMapToRegion(regionName) {
let points = [];
if (regionName === '团柏河') {
// 获取团柏河区域的所有标记点
points = this.stationTwolist
.filter(item => item.groupName === '团柏河')
.map(item => ({
latitude: Number(item.stationLatitude),
longitude: Number(item.stationLongitude)
}));
} else if (regionName === '对竹河') {
// 获取对竹河区域的所有标记点
points = this.stationTwolist
.filter(item => item.groupName === '对竹河')
.map(item => ({
latitude: Number(item.stationLatitude),
longitude: Number(item.stationLongitude)
}));
}
if (points.length > 0 && this.mapCtx) {
this.mapCtx.includePoints({
points: points,
padding: [60, 60, 60, 60] // 可以调整边距以获得更好的显示效果
});
}
},
markertap(e) {
const index = e.detail.markerId
const marker = this.markers.find(m => m.id === index);
if (marker) {
if (['orient', 'livestock'].includes(marker.stationType)) {
uni.navigateTo({
url: `/packDetail/pages/Site/detail?item=${encodeURIComponent(JSON.stringify(marker))}`
})
} else if (marker.stationType === 'device') {
// 处理设备点击事件
uni.showToast({
title: `设备: ${marker.stationName}`,
icon: 'none'
});
// 可以在这里添加设备详情页面的跳转逻辑
}
}
},
callouttap(e) {
const index = e.detail.markerId
const marker = this.markers.find(m => m.id === index);
if (marker) {
if (['orient', 'livestock'].includes(marker.stationType)) {
uni.navigateTo({
url: `/packDetail/pages/Site/detail?item=${encodeURIComponent(JSON.stringify(marker))}`
})
} else if (marker.stationType === 'device') {
// 处理设备点击事件
uni.showToast({
title: `设备: ${marker.stationName}`,
icon: 'none'
});
}
}
},
qiyeChange() {
for (let i = 0; i < this.cusLocation.length; i++) {
const item = this.cusLocation[i];
item.iconPath = '/static/qiyeIcon.png';
item.latitude = Number(item.stationLatitude);
item.longitude = Number(item.stationLongitude);
item.width = 35;
item.height = 52;
const markerItem = {
id: item.id,
iconPath: item.iconPath,
latitude: item.latitude,
longitude: item.longitude,
width: item.width,
height: item.height,
customCallout: {
anchorY: 0,
anchorX: 0,
display: 'ALWAYS'
},
stationType: item.stationType,
stationName: item.stationName,
stationCode: item.stationCode,
joinCluster: true
};
this.markers.push(markerItem);
}
},
switch2Change(name) {
if (name === "团柏河") {
this.issscd = !this.issscd;
} else {
this.isssscd = !this.isssscd;
}
// 根据当前状态调整地图视野
if ((name === "团柏河" && !this.issscd) || (name === "对竹河" && !this.isssscd)) {
// 如果是显示状态(绿色圆点),调整到指定区域
setTimeout(() => {
this.adjustMapToRegion(name);
}, 300);
} else {
// 如果是隐藏状态(灰色圆点),调整到所有标记点
setTimeout(() => {
this.adjustMapToFitAllMarkers();
}, 300);
}
},
list1query() {
// 先清除所有orient类型的标记
this.markers = this.markers.filter(item => item.stationType !== 'orient');
if (!this.issscd) {
for (let i = 0; i < this.stationlist.length; i++) {
const item = this.stationlist[i];
if (item.stationType === 'orient') {
item.iconPath = this.baseUrl +'/'+ item.stationIcon;
item.latitude = Number(item.stationLatitude);
item.longitude = Number(item.stationLongitude);
item.width = 172;
item.height = 148;
const markerItem = {
id: item.id,
iconPath: item.iconPath,
latitude: item.latitude,
longitude: item.longitude,
width: item.width,
height: item.height,
customCallout: {
anchorY: 0,
anchorX: 0,
display: 'ALWAYS'
},
stationType: item.stationType,
stationName: item.stationName,
stationCode: item.stationCode,
joinCluster: true
};
this.markers.push(markerItem);
}
}
}
},
}
}
</script>
<style lang="scss">
/* 样式保持不变 */
.bgimg {
position: fixed;
// z-index: -99;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
.customCallout {
width: 349rpx;
height: 49rpx;
color: #fff;
font-size: 24rpx;
text-shadow: 1rpx 2rpx 0rpx #0D1934;
font-weight: bold;
// background-image: url('@/static/zhandian_icon_nongtian_title.png');
background-repeat: no-repeat;
background-size: 100%;
display: flex;
align-items: center;
justify-content: center;
}
.customCallout1 {
width: 349rpx;
height: 49rpx;
color: #fff;
font-size: 24rpx;
text-shadow: 1rpx 2rpx 0rpx #0D1934;
font-weight: bold;
// background-image: url('@/static/zhandian_icon_xuqin_title.png');
background-repeat: no-repeat;
background-size: 100%;
display: flex;
align-items: center;
justify-content: center;
}
.zdxq {
position: fixed;
left: 33rpx;
bottom: 36rpx;
}
.zdxqitem {
padding: 8rpx 0 7rpx 17rpx;
display: flex;
align-items: center;
justify-content: space-between;
font-size: 22rpx;
font-family: Source Han Sans SC;
font-weight: 500;
color: #FFFFFF;
text-shadow: 0rpx 1rpx 0rpx rgba(4, 49, 52, 0.55);
width: 359rpx;
background: rgba(0, 0, 0, 0.52);
border: 1px solid #4B677C;
border-radius: 6rpx;
margin-bottom: 10rpx;
position: relative;
}
.crildon {
width: 10rpx;
height: 10rpx;
border-radius: 50%;
margin-right: 15rpx;
}
.zdxqitem:last-child {
margin-bottom: 0;
}
.zdxqitem image {
width: 33rpx;
height: 50rpx;
margin-right: 15rpx;
}
.zdxqitem:first-child image {
width: 30rpx;
height: 40rpx;
margin-right: 15rpx;
}
.textss {
flex: 1;
text-align: left;
}
.imgs {
position: absolute;
width: 100%;
height: 100%;
z-index: -9;
}
.switchs {
position: fixed;
top: 36rpx;
right: 40rpx;
display: flex;
align-items: center;
}
</style>