FenXiNspUniapp/pages/maps/index.vue

545 lines
14 KiB
Vue

<template>
<view class="container">
<map id="map" style="width: 100%; height:2000rpx;" scale="13" :markers="markers" longitude="110.628279"
latitude="35.546105" show-location @markertap="markertap" @callouttap="callouttap">
<cover-view slot="callout">
<cover-view v-for="(item,index) in markers" :key="index" class="customCallout" :marker-id="item.id">
<cover-image class="imgs" src="@/static/zhandian_icon_xuqin_title.png"
v-if="item.stationType == 'livestock'"></cover-image>
<cover-image class="imgs" src="@/static/zhandian_icon_nongtian_title.png"
v-if="item.stationType == 'orient'"></cover-image>
<cover-image class="imgs" src="@/static/zhandian_icon_qiye_title.png"
v-if="item.stationType == 'cusLocaltion'"></cover-image>
<cover-view class="content"
v-if="item.stationType == 'livestock' || item.stationType == 'orient'|| item.stationType == 'cusLocaltion'">
{{item.stationName}}
</cover-view>
</cover-view>
</cover-view>
</map>
<view class="zdxq">
<view class="zdxqitem" @click="switch1Change">
<image src="../../static/zhandian_sahchong_dingwei.png" mode=""></image>
<view class="textss">
联网式太阳能杀虫灯
</view>
<view class="crildon" :style="!isscd?'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="!issscd?'background:#00ff37':'background: #ccc;'"></view>
</view>
<view class="zdxqitem" @click="switch3Change">
<image src="../../static/zhandian_nongtian_dingwei.png" mode=""></image>
<view class="textss">
农田面源污染监测站
</view>
<view class="crildon" :style="!isssscd?'background:#00ff37':'background: #ccc;'"></view>
</view>
</view>
</view>
</template>
<script>
import api from '@/api/api'
import selectSwitch from "@/components/xuan-switch/xuan-switch.vue";
export default {
components: {
selectSwitch
},
data() {
return {
markers: [],
markersNoPest: [],
list: [],
pointers: [],
imgurl1: '@/static/zhandian_icon_nongtian_title.png',
imgurl2: '@/static/zhandian_icon_xuqin_title.png',
isscd: true,
issscd: true,
isssscd: true,
pestlight: [],
cusLocation: [],
stationInfo: [],
pestlightMarker: null,
stationlist: [],
timerSet: null,
// 点聚合相关
mapContext: null,
clusterMarkerIds: [], // 参与聚合的marker id列表
isClusterInited: false // 是否已初始化聚合
};
},
onLoad() {
this.$nextTick(() => {
this.getList()
})
},
onShow() {
var that = this
if (uni.getStorageSync('third_session')) {
uni.login({
success: function(res) {
console.log(res.code)
this.code = res.code
let params = {}
params.jsCode = this.code
console.log(params, 152)
api.wxlogin(params).then(res => {
console.log(res, 165)
if (res.data.code == 0) {
let userInfo = res.data.data;
if (!userInfo.id) {
uni.reLaunch({
url: '/packDetail/pages/login/login'
})
}
}
})
}
});
} else if (!uni.getStorageSync('third_session')) {
uni.reLaunch({
url: '/pages/loginindex'
})
}
},
onReady() {
// 获取地图上下文
this.mapContext = uni.createMapContext("map", this);
// 初始化点聚合
this.initCluster()
},
methods: {
// ===== 新增:初始化点聚合 =====
initCluster() {
if (!this.mapContext) {
setTimeout(() => {
this.initCluster()
}, 500)
return
}
// 1. 初始化聚合配置
this.mapContext.initMarkerCluster({
enableDefaultStyle: false, // 使用默认样式
zoomOnClick: true, // 点击聚合点时是否放大地图
gridSize: 60, // 聚合网格大小
complete: (res) => {
console.log('initMarkerCluster success', res)
this.isClusterInited = true
// 聚合初始化完成后,添加杀虫灯标记
this.addPestMarkersToCluster()
}
})
// 2. 监听聚合簇创建事件,自定义聚合簇样式
this.mapContext.on('markerClusterCreate', (res) => {
console.log('markerClusterCreate', res)
const clusters = res.clusters
const clusterMarkers = clusters.map((cluster) => {
// 获取该聚合簇中第一个marker的坐标作为中心点
const centerLat = cluster.center.latitude
const centerLng = cluster.center.longitude
const count = cluster.markerIds.length
return {
id: cluster.clusterId,
latitude: centerLat,
longitude: centerLng,
width: 40,
height: 40,
iconPath: '/static/zhandian_sahchong_dingwei.png',
label: {
content: count + '',
color: '#ffffff',
fontSize: 12,
fontWeight: 'bold',
anchorX: 0,
anchorY: -8,
borderWidth: 0,
bgColor: 'rgba(0,0,0,0.6)',
borderRadius: 10,
padding: 2
}
}
})
// 更新聚合簇样式
this.mapContext.addMarkers({
markers: clusterMarkers,
clear: false,
complete: (res) => {
console.log('add cluster markers success', res)
}
})
})
},
// ===== 新增:添加杀虫灯到聚合 =====
addPestMarkersToCluster() {
if (!this.isClusterInited) return
// 先移除之前的聚合标记
if (this.clusterMarkerIds.length > 0) {
this.mapContext.removeMarkers({
markerIds: this.clusterMarkerIds,
complete: (res) => {
console.log('remove old cluster markers', res)
}
})
this.clusterMarkerIds = []
}
// 如果杀虫灯开关是关闭状态,不添加
if (!this.isscd || this.pestlight.length === 0) return
// 准备聚合标记数据
const pestMarkers = []
this.pestlight.forEach((item, index) => {
const markerId = 10000 + index
pestMarkers.push({
id: markerId,
latitude: Number(item.latitude),
longitude: Number(item.longitude),
width: 15,
height: 20,
iconPath: '/static/zhandian_sahchong_dingwei.png',
joinCluster: true, // 参与聚合
inType: 'pestlight',
stationName: item.stationName,
stationCode: item.stationCode
})
this.clusterMarkerIds.push(markerId)
})
// 通过 addMarkers 添加参与聚合的标记
this.mapContext.addMarkers({
markers: pestMarkers,
clear: false,
complete: (res) => {
console.log('add pest markers to cluster success', res)
}
})
},
// ===== 修改:移除杀虫灯从聚合 =====
removePestMarkersFromCluster() {
if (this.clusterMarkerIds.length > 0 && this.mapContext) {
this.mapContext.removeMarkers({
markerIds: this.clusterMarkerIds,
complete: (res) => {
console.log('remove pest markers from cluster', res)
}
})
this.clusterMarkerIds = []
}
},
getList() {
this.markers = []
this.markersNoPest = []
this.pointers = []
this.list = []
uni.showLoading({
title: '加载中'
});
this.$http.get('/applet/survStationInfo/stationMap').then(res => {
this.stationlist = [...res.data.data.pestlight, ...res.data.data.cusLocation, ...res.data.data.stationInfo]
this.stationlist.forEach((item,index) => {
item.id = index;
if(item.inType == 'pestlight'){
this.pestlight.push(item)
}
if(item.stationType == 'cusLocaltion'){
this.cusLocation.push(item)
}
if(item.stationType == 'orient' || item.stationType == 'livestock'){
this.stationInfo.push(item)
}
this.list.push({
latitude:Number(item.stationLatitude),
longitude: Number(item.stationLongitude),
stationType:item.stationType,
sortNo: item.sortNo,
stationName: item.stationName,
stationCode: item.stationCode
})
})
this.issscd = false
this.isssscd = false
this.qiyeChange()
this.list1query()
this.list2query()
// 如果聚合已初始化,直接添加杀虫灯;否则等待初始化完成
if (this.isClusterInited) {
this.addPestMarkersToCluster()
}
uni.hideLoading();
})
},
// ===== 修改:杀虫灯开关控制 =====
switch1Change() {
this.isscd = !this.isscd;
if (this.isscd) {
// 打开 - 添加杀虫灯到聚合
this.addPestMarkersToCluster()
} else {
// 关闭 - 从聚合中移除杀虫灯
this.removePestMarkersFromCluster()
}
},
// ===== 以下方法完全保持原样 =====
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;
this.list.push({
latitude:Number(item.stationLatitude),
longitude: Number(item.stationLongitude),
stationType:item.stationType,
sortNo: item.sortNo,
stationName: item.stationName,
stationCode: item.stationCode
})
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() {
this.issscd = !this.issscd
this.list1query()
},
switch3Change() {
this.isssscd = !this.isssscd
this.list2query()
},
list1query(){
if (!this.issscd) {
for (let i = 0; i < this.stationInfo.length; i++) {
const item = this.stationInfo[i];
if(item.stationType === 'orient'){
item.iconPath = '/static/zhandian_nongtian_dingwei.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);
}
}
} else {
this.markers = this.markers.filter((item)=>{
return item.stationType !== 'orient'
})
}
},
list2query(){
if (!this.isssscd) {
for (let i = 0; i < this.stationInfo.length; i++) {
const item = this.stationInfo[i];
if(item.stationType === 'livestock'){
item.iconPath = '/static/zhandian_xuqin_dingwei.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);
}
}
} else {
this.markers = this.markers.filter((item)=>{
return item.stationType !== 'livestock'
})
}
},
markertap(e) {
const index = e.detail.markerId
console.log(e.detail,'index')
// 判断是否点击的是聚合点
if (e.detail && e.detail.isCluster) {
console.log('点击了聚合点:', e.detail)
return
}
if (['orient', 'livestock'].includes(this.list[index]?.stationType)) {
uni.navigateTo({
url: `/packDetail/pages/Site/detail?item=${encodeURIComponent(JSON.stringify(this.list[index]))}`
})
}
},
callouttap(e) {
const index = e.detail.markerId
if (['orient', 'livestock'].includes(this.list[index]?.stationType)) {
uni.navigateTo({
url: `/packDetail/pages/Site/detail?item=${encodeURIComponent(JSON.stringify(this.list[index]))}`
})
}
},
todetails(item) {
console.log(item, '还是个然后')
}
}
}
</script>
<style lang="scss">
.bgimg {
position: fixed;
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-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-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: 299rpx;
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>