Files
2026-02-04 16:11:55 +08:00

129 lines
4.4 KiB
JavaScript
Raw Permalink 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.
const app = getApp()
const db = wx.cloud.database()
Page({
data: {
roomId: '',
center: { latitude: 39.9, longitude: 116.4 },
markers: [],
recommendations: [],
scale: 13,
selectedIndex: -1 // 当前选中的索引
},
onLoad: function (options) {
this.setData({ roomId: options.roomId })
this.fetchResult()
},
async fetchResult() {
wx.showLoading({ title: '加载结果...' })
try {
const res = await db.collection('rooms').doc(this.data.roomId).get()
const data = res.data
if (data && data.result && data.result.success) {
const result = data.result
const center = result.center
const recommendations = result.recommendations || []
// 构建 Markers
const markers = []
// 中心点 Marker
markers.push({
id: 0,
latitude: center.lat,
longitude: center.lng,
iconPath: '',
width: 0,
height: 0,
callout: {
content: '最佳中心点',
display: 'ALWAYS',
padding: 8,
borderRadius: 4,
bgColor: '#FF6B6B',
color: '#FFFFFF',
fontSize: 12
}
})
// 推荐点 Markers
recommendations.forEach((place, index) => {
markers.push({
id: index + 1, // ID 从 1 开始
latitude: place.location.lat,
longitude: place.location.lng,
iconPath: '',
width: 0,
height: 0,
callout: {
content: `${index + 1}`, // 默认只显示序号
display: 'ALWAYS',
padding: 6,
borderRadius: 4,
bgColor: '#4CAF50',
color: '#FFFFFF',
fontSize: 11
}
})
})
this.setData({
center: { latitude: center.lat, longitude: center.lng },
recommendations: recommendations,
markers: markers
})
} else {
wx.showToast({ title: '暂无结果', icon: 'none' })
}
wx.hideLoading()
} catch (err) {
console.error(err)
wx.hideLoading()
wx.showToast({ title: '加载失败', icon: 'none' })
}
},
onListTap(e) {
const index = e.currentTarget.dataset.index
const place = this.data.recommendations[index]
// 如果点击已选中的,取消选中
const newSelectedIndex = this.data.selectedIndex === index ? -1 : index
// 更新选中状态
this.setData({ selectedIndex: newSelectedIndex })
// 更新地图标记的 callout
const markers = this.data.markers
markers.forEach((marker, markerIndex) => {
if (marker.id > 0) { // 推荐点 MarkerID 从 1 开始)
const placeIndex = marker.id - 1
if (newSelectedIndex === placeIndex) {
// 选中的地点显示名称
markers[markerIndex].callout.content = `${index + 1}. ${place.name}`
markers[markerIndex].callout.bgColor = '#FF6B6B' // 红色背景
markers[markerIndex].callout.fontSize = 12
} else {
// 未选中的地点只显示序号
markers[markerIndex].callout.content = `${placeIndex + 1}`
markers[markerIndex].callout.bgColor = '#4CAF50' // 绿色背景
markers[markerIndex].callout.fontSize = 11
}
}
})
// 更新 markers 数据
this.setData({ markers })
// 移动地图到选中地点
if (newSelectedIndex >= 0) {
this.setData({
center: { latitude: place.location.lat, longitude: place.location.lng }
})
}
}
})