first commit
This commit is contained in:
226
miniprogram/pages/index/modules/mapModule.ts
Normal file
226
miniprogram/pages/index/modules/mapModule.ts
Normal file
@@ -0,0 +1,226 @@
|
||||
// 地图模块 - 处理地图显示、定位、标记点管理
|
||||
import mapService from '../../../services/mapService';
|
||||
import userService from '../../../services/userService';
|
||||
import { showToast } from '../../../utils/helpers';
|
||||
import { Marker } from '../../../types';
|
||||
import { DataModule } from './dataModule';
|
||||
|
||||
export class MapModule {
|
||||
private pageContext: any;
|
||||
private dataModule: DataModule;
|
||||
|
||||
constructor(pageContext: any, dataModule: DataModule) {
|
||||
this.pageContext = pageContext;
|
||||
this.dataModule = dataModule;
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化地图数据
|
||||
*/
|
||||
async initMap(): Promise<void> {
|
||||
try {
|
||||
// 检查并请求位置权限
|
||||
await this.checkAndRequestLocationPermission();
|
||||
|
||||
// 加载地图数据
|
||||
this.loadMapData();
|
||||
|
||||
} catch (error) {
|
||||
console.error('初始化地图失败:', error);
|
||||
showToast('地图初始化失败,请重试');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查并请求位置权限
|
||||
*/
|
||||
async checkAndRequestLocationPermission(): Promise<void> {
|
||||
try {
|
||||
// 检查位置权限
|
||||
const hasPermission = await userService.checkLocationPermission();
|
||||
|
||||
if (hasPermission) {
|
||||
// 已授权,开始定位
|
||||
await this.startLocation();
|
||||
} else {
|
||||
// 未授权,请求权限
|
||||
const permissionGranted = await userService.requestLocationPermission();
|
||||
|
||||
if (permissionGranted) {
|
||||
await this.startLocation();
|
||||
} else {
|
||||
// 授权失败,使用默认位置
|
||||
this.setDefaultLocation();
|
||||
showToast('已使用默认位置');
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('检查位置权限失败:', error);
|
||||
this.setDefaultLocation();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 开始定位
|
||||
*/
|
||||
async startLocation(): Promise<void> {
|
||||
try {
|
||||
console.log('[MAP MODULE] 开始获取用户位置...');
|
||||
const location = await mapService.getCurrentLocation();
|
||||
|
||||
console.log('[MAP MODULE] 获取位置成功,原始坐标:', location);
|
||||
console.log('[MAP MODULE] 详细位置信息:');
|
||||
console.log(`- 经度: ${location.longitude}`);
|
||||
console.log(`- 纬度: ${location.latitude}`);
|
||||
console.log(`- 精度: ${location.accuracy}`);
|
||||
console.log(`- 速度: ${location.speed}`);
|
||||
|
||||
// 验证获取的坐标是否有效
|
||||
if (isNaN(location.latitude) || isNaN(location.longitude)) {
|
||||
console.error('[MAP MODULE] 获取的位置坐标无效:', location);
|
||||
this.setDefaultLocation();
|
||||
showToast('获取的位置无效,已使用默认位置');
|
||||
return;
|
||||
}
|
||||
|
||||
// 更新数据模块中的位置信息和缩放级别
|
||||
this.dataModule.updateUserLocation(location.latitude, location.longitude);
|
||||
this.dataModule.setMapScale(15); // 定位成功后放大到更详细的级别
|
||||
|
||||
console.log('[MAP MODULE] 定位成功,坐标已更新:', location);
|
||||
} catch (error) {
|
||||
console.error('[MAP MODULE] 定位失败:', error);
|
||||
this.setDefaultLocation();
|
||||
showToast('定位失败,已使用默认位置');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置默认位置
|
||||
*/
|
||||
setDefaultLocation(): void {
|
||||
const defaultLatitude = 24.880095;
|
||||
const defaultLongitude = 102.833722;
|
||||
|
||||
// 更新数据模块中的位置信息和缩放级别
|
||||
this.dataModule.updateUserLocation(defaultLatitude, defaultLongitude);
|
||||
this.dataModule.setMapScale(13);
|
||||
|
||||
console.log('[MAP MODULE] 默认位置设置完成');
|
||||
}
|
||||
|
||||
/**
|
||||
* 加载地图数据(仓库和货运人员)
|
||||
*/
|
||||
loadMapData(): void {
|
||||
// 这里会调用具体的服务来获取数据
|
||||
// 目前使用模拟数据
|
||||
const markers = this.generateInitialMarkers();
|
||||
|
||||
// 更新数据模块中的标记点
|
||||
this.dataModule.updateMarkers(markers);
|
||||
|
||||
console.log('地图数据加载完成');
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成初始标记点
|
||||
*/
|
||||
private generateInitialMarkers(): Marker[] {
|
||||
// 获取当前坐标
|
||||
const longitude = this.pageContext.data.longitude;
|
||||
const latitude = this.pageContext.data.latitude;
|
||||
|
||||
// 检查坐标是否为有效数字
|
||||
let validLongitude = longitude;
|
||||
let validLatitude = latitude;
|
||||
|
||||
if (isNaN(longitude) || isNaN(latitude)) {
|
||||
console.warn(`无效的坐标值: longitude=${longitude}, latitude=${latitude}, 使用默认坐标`);
|
||||
validLongitude = 102.833722; // 默认经度
|
||||
validLatitude = 24.880095; // 默认纬度
|
||||
|
||||
// 更新页面数据中的坐标
|
||||
this.dataModule.updateUserLocation(validLatitude, validLongitude);
|
||||
}
|
||||
|
||||
|
||||
|
||||
// 生成初始标记点
|
||||
return [
|
||||
{
|
||||
id: -1, // 用户位置标记
|
||||
title: '用户位置',
|
||||
longitude: validLongitude,
|
||||
latitude: validLatitude,
|
||||
iconPath: '/images/trucks.png',
|
||||
width: 40,
|
||||
height: 40,
|
||||
zIndex: 99
|
||||
}
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新用户标记图标为用户头像
|
||||
*/
|
||||
updateUserMarkerIcon(): void {
|
||||
const { authStatus, userInfo, markers } = this.pageContext.data;
|
||||
|
||||
if (!authStatus.hasWxCode || !userInfo) {
|
||||
console.warn('未登录或用户信息为空,无法更新头像');
|
||||
return;
|
||||
}
|
||||
|
||||
// 使用默认头像
|
||||
const avatarUrl = '/images/trucks.png';
|
||||
|
||||
const updatedMarkers = markers.map((marker: Marker) => {
|
||||
if (marker.id === -1) {
|
||||
return {
|
||||
...marker,
|
||||
iconPath: avatarUrl
|
||||
};
|
||||
}
|
||||
return marker;
|
||||
});
|
||||
|
||||
// 更新数据模块中的标记点
|
||||
this.dataModule.updateMarkers(updatedMarkers);
|
||||
|
||||
console.log('用户头像已更新');
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理地图点击事件
|
||||
*/
|
||||
onMapTap(e: any): void {
|
||||
console.log('地图被点击:', e);
|
||||
// 可以在这里添加地图点击的处理逻辑
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理标记点点击事件
|
||||
*/
|
||||
onMarkerTap(e: any): void {
|
||||
const markerId = e.markerId;
|
||||
console.log('标记点被点击:', markerId);
|
||||
|
||||
// 根据标记点ID处理不同的点击逻辑
|
||||
this.handleMarkerClick(markerId);
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理标记点点击
|
||||
*/
|
||||
private handleMarkerClick(markerId: number): void {
|
||||
// 这里可以根据标记点ID进行不同的处理
|
||||
// 例如:显示订单详情、货运人员信息等
|
||||
|
||||
if (markerId === -1) {
|
||||
// 用户位置标记
|
||||
this.pageContext.showUserPanel();
|
||||
}
|
||||
// 可以添加其他标记点的处理逻辑
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user