修改位置交互,修改代码逻辑

This commit is contained in:
2025-10-18 22:21:04 +08:00
parent c446df73b5
commit 39fa0b2d04
36 changed files with 2743 additions and 1995 deletions

View File

@@ -14,11 +14,32 @@ export class DataModule {
/**
* 初始化页面数据
*/
public initializeData(): void {
public async initializeData(): Promise<void> {
// 检查是否已静默登录,如果是则尝试获取真实位置
let initialLongitude = 102.833722;
let initialLatitude = 24.880095;
const app = getApp<any>();
if (app.globalData.isLoggedIn) {
try {
// 导入地图服务
const mapService = require('../../../services/mapService').default;
const location = await mapService.getCurrentLocation();
if (location && !isNaN(location.latitude) && !isNaN(location.longitude)) {
initialLongitude = location.longitude;
initialLatitude = location.latitude;
console.log('[DATA MODULE] 静默登录后使用真实位置:', location);
}
} catch (error) {
console.warn('[DATA MODULE] 获取真实位置失败,使用默认位置:', error);
}
}
this.pageContext.setData({
// 地图相关数据
longitude: 102.833722,
latitude: 24.880095,
longitude: initialLongitude,
latitude: initialLatitude,
scale: 13,
markers: [] as Marker[],
polyline: null,
@@ -148,7 +169,7 @@ export class DataModule {
*/
public updateMarkers(markers: Marker[]): void {
// 验证每个标记点的坐标
const validatedMarkers = markers.map((marker, index) => {
const validatedMarkers = markers.map((marker) => {
// 检查经纬度是否为有效数字
if (isNaN(marker.longitude) || isNaN(marker.latitude)) {
// 为无效坐标设置默认值
@@ -283,4 +304,26 @@ export class DataModule {
public resetAllData(): void {
this.initializeData();
}
/**
* 获取位置模块(用于其他模块访问位置模块)
*/
public getLocationModule(): any {
// 通过页面上下文获取位置模块
if (this.pageContext.data.mainPageModule) {
return this.pageContext.data.mainPageModule.getLocationModule();
}
return null;
}
/**
* 获取地图模块(用于其他模块访问地图模块)
*/
public getMapModule(): any {
// 通过页面上下文获取地图模块
if (this.pageContext.data.mainPageModule) {
return this.pageContext.data.mainPageModule.getMapModule();
}
return null;
}
}

View File

@@ -1,10 +1,12 @@
// 货运人员模块 - 处理货运人员管理、位置跟踪、交互
import deliveryPersonService from '../../../services/deliveryPersonService';
// 员模块 - 处理所有员工(管理员和货运人员管理、位置跟踪、交互
import employeeService from '../../../services/employeeService';
import { Role } from '../../../utils/roleUtils';
// getApp是微信小程序全局函数无需导入
import { showToast } from '../../../utils/helpers';
import { DataModule } from './dataModule';
export class DeliveryPersonModule {
export class EmployeeModule {
private pageContext: any;
private dataModule: DataModule;
@@ -14,210 +16,44 @@ export class DeliveryPersonModule {
}
/**
* 加载货运人员数据
* 加载所有员工数据(包括管理员和货运人员
*/
async loadDeliveryPersons(): Promise<void> {
async loadAllEmployees(): Promise<void> {
try {
const deliveryPersons = await deliveryPersonService.getDeliveryPersons();
// 获取所有员工数据
const allEmployees = await employeeService.getEmployees();
// 更新地图标记点
this.updateDeliveryPersonMarkers(deliveryPersons);
console.log('货运人员数据加载完成,数量:', deliveryPersons.length);
console.log('所有员工数据加载完成,数量:', allEmployees.length);
} catch (error) {
console.error('加载货运人员数据失败:', error);
showToast('加载货运人员数据失败');
}
}
/**
* 更新货运人员位置
*/
async updateDeliveryPersonLocation(personId: number, location: { longitude: number, latitude: number }): Promise<void> {
try {
await deliveryPersonService.updateDeliveryPersonLocation(personId, location);
console.log(`货运人员 ${personId} 位置更新:`, location);
// 重新加载货运人员数据
await this.loadDeliveryPersons();
} catch (error) {
console.error('更新货运人员位置失败:', error);
showToast('更新位置失败');
}
}
/**
* 开始实时位置跟踪
*/
async startRealTimeTracking(): Promise<void> {
try {
// 获取当前用户信息
const userInfo = this.dataModule.getData().userInfo;
if (!userInfo || !userInfo.id) {
throw new Error('用户信息获取失败');
}
// 使用新的WebSocket接口订阅位置更新
await deliveryPersonService.subscribeToRealTimeLocations(userInfo.id);
// 设置位置更新回调
deliveryPersonService.subscribeToRealTimeLocations(this.handleRealTimeLocationUpdate.bind(this));
showToast('开始实时跟踪');
console.log('开始实时跟踪货运人员位置');
} catch (error) {
console.error('开始实时跟踪失败:', error);
showToast('开始实时跟踪失败');
console.error('加载员数据失败:', error);
showToast('加载员数据失败');
}
}
handleRealTimeLocationUpdate(location: any): void {
console.log('收到实时位置更新:', location);
// 这里可以添加更新地图标记点的逻辑
// 根据位置更新信息更新对应的货运人员标记点
if (location && location.deliveryPersonId) {
this.updateSingleDeliveryPersonMarker(location);
}
}
/**
* 更新单个货运人员标记点
* 获取员工角色对应的图标
*/
private updateSingleDeliveryPersonMarker(location: any): void {
const { markers } = this.pageContext.data;
private getEmployeeIcon(role: string): string {
console.log(`获取员工图标,角色: ${role}`);
// 查找并更新对应的货运人员标记点
const updatedMarkers = markers.map((marker: any) => {
if (marker.type === 'delivery_person' && marker.data && marker.data.id === location.deliveryPersonId) {
// 更新标记点位置
return {
...marker,
longitude: location.longitude,
latitude: location.latitude,
data: {
...marker.data,
currentLocation: {
longitude: location.longitude,
latitude: location.latitude
}
}
};
}
return marker;
});
// 更新数据模块中的标记点
this.dataModule.updateMarkers(updatedMarkers);
}
/**
* 停止实时跟踪货运人员位置
*/
async stopRealTimeTracking(): Promise<void> {
try {
// 获取当前用户信息
const userInfo = this.dataModule.getData().userInfo;
if (!userInfo || !userInfo.id) {
throw new Error('用户信息获取失败');
}
// 使用新的WebSocket接口取消订阅
await deliveryPersonService.unsubscribeFromRealTimeLocations();
showToast('已停止实时跟踪');
console.log('停止实时跟踪货运人员位置');
} catch (error) {
console.error('停止实时跟踪失败:', error);
showToast('停止实时跟踪失败');
}
}
/**
* 更新货运人员标记点
*/
private updateDeliveryPersonMarkers(deliveryPersons: any[]): void {
console.log(`[DELIVERY PERSON MODULE] 开始更新货运人员标记点,货运人员总数: ${deliveryPersons.length}`);
const { markers } = this.pageContext.data;
// 移除现有的货运人员标记点
const filteredMarkers = markers.filter((marker: any) => marker.type !== 'delivery_person');
console.log(`[DELIVERY PERSON MODULE] 移除现有货运人员标记点后,剩余标记点数量: ${filteredMarkers.length}`);
// 添加新的货运人员标记点
const deliveryPersonMarkers = deliveryPersons.map((person, index) => {
// 验证货运人员坐标是否有效
// 注意坐标信息嵌套在currentLocation对象中
let validLongitude = person.currentLocation?.longitude;
let validLatitude = person.currentLocation?.latitude;
if (isNaN(validLongitude) || isNaN(validLatitude)) {
console.error(`[DELIVERY PERSON MODULE] 货运人员${index} (ID: ${person.id}) 坐标无效: (${validLongitude}, ${validLatitude}),使用默认坐标`);
validLongitude = 102.833722; // 默认经度
validLatitude = 24.880095; // 默认纬度
} else {
console.log(`[DELIVERY PERSON MODULE] 货运人员${index} (ID: ${person.id}) 坐标有效: (${validLongitude}, ${validLatitude})`);
}
const iconPath = this.getDeliveryPersonIcon(person.status);
return {
id: 2000 + index, // 货运人员标记点ID从2000开始
longitude: validLongitude,
latitude: validLatitude,
iconPath: iconPath,
width: 26,
height: 26,
zIndex: 20,
type: 'delivery_person',
data: person
};
});
console.log(`[DELIVERY PERSON MODULE] 生成新货运人员标记点数量: ${deliveryPersonMarkers.length}`);
// 更新数据模块中的标记点
const allMarkers = [...filteredMarkers, ...deliveryPersonMarkers];
console.log(`[DELIVERY PERSON MODULE] 准备更新所有标记点,总数: ${allMarkers.length}`);
this.dataModule.updateMarkers(allMarkers);
}
/**
* 获取货运人员状态对应的图标
*/
private getDeliveryPersonIcon(status: string): string {
console.log(`获取货运人员图标,状态: ${status}`);
// 根据报错信息,直接使用已知存在的备用图片路径
// 实际项目中,应确保相关图片资源正确放置在指定路径
const fallbackIconPath = '/images/trucks.png';
// 根据不同状态使用不同的图片路径
// 根据角色使用不同的图标
let iconPath = '';
switch (status) {
case 'idle':
// 为避免图片加载失败,暂时使用备用图片
iconPath = fallbackIconPath;
console.log('使用备用图标idle状态');
switch (role) {
case Role.ADMIN:
// 管理员使用皇冠图标
iconPath = '/images/crown.png';
console.log('使用管理员图标(👑');
break;
case 'busy':
// 为避免图片加载失败,暂时使用备用图片
iconPath = fallbackIconPath;
console.log('使用备用图标busy状态');
break;
case 'offline':
// 为避免图片加载失败,暂时使用备用图片
iconPath = fallbackIconPath;
console.log('使用备用图标offline状态');
case Role.DELIVERY_PERSON:
// 货运人员使用货车图标
iconPath = '/images/truck.png';
console.log('使用货运人员图标(🚚');
break;
default:
iconPath = fallbackIconPath;
console.log('使用备用图标(默认状态)');
// 默认使用货车图标
iconPath = '/images/truck.png';
console.log('使用默认图标(🚚)');
break;
}

View File

@@ -0,0 +1,278 @@
// 位置模块 - 处理位置追踪、位置更新和地图标记点更新
import { DataModule } from './dataModule';
import { showToast } from '../../../utils/helpers';
import locationTrackingService from '../../../services/locationTrackingService';
import { OnlineUserInfo } from '../../../services/locationTrackingService';
// 位置模块接口定义
export interface LocationModule {
initialize(): Promise<void>;
cleanup(): void;
startRealTimeTracking(): Promise<void>;
stopRealTimeTracking(): Promise<void>;
updateEmployeeLocation(employeeId: number, location: { longitude: number, latitude: number }): Promise<void>;
}
export class LocationModule {
private pageContext: any;
private dataModule: DataModule;
private isTracking: boolean;
constructor(pageContext: any, dataModule: DataModule) {
this.pageContext = pageContext;
this.dataModule = dataModule;
this.isTracking = false;
// 绑定回调方法
this.handleLocationUpdates = this.handleLocationUpdates.bind(this);
}
/**
* 初始化位置模块
*/
async initialize(): Promise<void> {
console.log('位置模块初始化');
// 订阅位置更新
this.subscribeToLocationUpdates();
console.log('位置模块初始化完成');
}
/**
* 清理位置模块
*/
cleanup(): void {
console.log('清理位置模块');
// 取消位置更新订阅
this.unsubscribeFromLocationUpdates();
// 停止实时跟踪
if (this.isTracking) {
this.stopRealTimeTracking().catch(error => {
console.error('停止实时跟踪失败:', error);
});
}
}
/**
* 开始实时跟踪位置
*/
async startRealTimeTracking(): Promise<void> {
try {
// 获取当前用户信息
const userInfo = this.dataModule.getData().userInfo;
if (!userInfo || !userInfo.id) {
throw new Error('用户信息获取失败');
}
// 启动位置跟踪订阅
await locationTrackingService.startTrackingAfterSignIn();
this.isTracking = true;
showToast('已开始实时跟踪');
console.log('开始实时跟踪位置');
} catch (error) {
console.error('开始实时跟踪失败:', error);
showToast('开始实时跟踪失败');
throw error;
}
}
/**
* 停止实时跟踪位置
*/
async stopRealTimeTracking(): Promise<void> {
try {
// 获取当前用户信息
const userInfo = this.dataModule.getData().userInfo;
if (!userInfo || !userInfo.id) {
throw new Error('用户信息获取失败');
}
// 停止位置跟踪
await locationTrackingService.stopTracking();
this.isTracking = false;
showToast('已停止实时跟踪');
console.log('停止实时跟踪位置');
} catch (error) {
console.error('停止实时跟踪失败:', error);
showToast('停止实时跟踪失败');
throw error;
}
}
/**
* 更新员工位置
*/
async updateEmployeeLocation(employeeId: number, location: { longitude: number, latitude: number }): Promise<void> {
try {
console.log(`员工 ${employeeId} 位置更新:`, location);
// 通过locationTrackingService更新位置
await locationTrackingService.updateUserLocation(location);
console.log(`员工 ${employeeId} 位置更新完成`);
} catch (error) {
console.error('更新员工位置失败:', error);
showToast('更新位置失败');
throw error;
}
}
/**
* 订阅位置更新
*/
private subscribeToLocationUpdates(): void {
console.log('订阅位置更新');
locationTrackingService.subscribeToLocationUpdates(this.handleLocationUpdates);
}
/**
* 取消订阅位置更新
*/
private unsubscribeFromLocationUpdates(): void {
console.log('取消订阅位置更新');
locationTrackingService.unsubscribeFromLocationUpdates(this.handleLocationUpdates);
}
/**
* 处理位置更新回调
*/
private handleLocationUpdates(locationData: any): void {
console.log('收到位置更新回调:', locationData);
if (locationData.type === 'onlineUserList' && locationData.users) {
// 处理在线用户列表更新
console.log('处理在线用户列表,用户数量:', locationData.users.length);
this.updateEmployeeMarkers(locationData.users);
} else if (Array.isArray(locationData)) {
// 处理位置更新数组(旧格式)
console.log('处理位置更新数组,用户数量:', locationData.length);
this.updateEmployeeMarkers(locationData);
} else if (locationData.userId && locationData.latitude && locationData.longitude) {
// 处理单个用户位置更新
console.log('处理单个用户位置更新:', locationData.userId);
this.updateSingleEmployeeMarker(locationData);
} else {
console.warn('未知的位置数据格式:', locationData);
}
}
/**
* 更新员工标记点
*/
private updateEmployeeMarkers(onlineUsers: any[]): void {
console.log('开始更新员工标记点,在线用户数量:', onlineUsers.length);
// 获取地图模块来更新标记点
const mapModule = this.dataModule.getMapModule();
if (!mapModule) {
console.error('地图模块未找到,无法更新员工标记点');
return;
}
// 为每个在线用户创建标记点
const employeeMarkers = onlineUsers.map(user => {
// 获取用户角色信息
const userRole = user.role || this.getUserRole(user.userId);
// 解析位置信息(支持多种格式)
const longitude = user.longitude || (user.lastLocation && user.lastLocation.longitude) || 0;
const latitude = user.latitude || (user.lastLocation && user.lastLocation.latitude) || 0;
const lastUpdateTime = user.lastUpdateTime || Date.now();
const employeeMarker = {
id: 10000 + user.userId, // 避免ID冲突
type: 'employee',
title: user.userName || `员工${user.userId}`,
longitude: longitude,
latitude: latitude,
iconPath: this.getEmployeeIcon(user.userId, userRole),
width: 32,
height: 32,
zIndex: 30,
data: {
userId: user.userId,
role: userRole,
lastUpdateTime: lastUpdateTime
}
};
return employeeMarker;
});
// 调用地图模块更新员工标记点
mapModule.updateEmployeeMarkers(employeeMarkers);
console.log(`成功更新了 ${employeeMarkers.length} 个员工标记点`);
}
/**
* 更新单个员工标记点
*/
private updateSingleEmployeeMarker(locationUpdate: any): void {
console.log('更新单个员工标记点:', locationUpdate.userId);
// 获取地图模块
const mapModule = this.dataModule.getMapModule();
if (!mapModule) {
console.error('地图模块未找到,无法更新员工标记点');
return;
}
// 获取用户角色信息
const userRole = this.getUserRole(locationUpdate.userId);
// 创建单个员工标记点
const employeeMarker = {
id: 10000 + locationUpdate.userId,
type: 'employee',
title: `员工${locationUpdate.userId}`,
longitude: locationUpdate.longitude,
latitude: locationUpdate.latitude,
iconPath: this.getEmployeeIcon(locationUpdate.userId, userRole),
width: 32,
height: 32,
zIndex: 30,
data: {
userId: locationUpdate.userId,
role: userRole,
lastUpdateTime: locationUpdate.timestamp || Date.now()
}
};
// 调用地图模块更新单个标记点
mapModule.updateSingleEmployeeMarker(employeeMarker);
console.log('单个员工标记点更新完成');
}
/**
* 获取用户角色
*/
private getUserRole(userId: number): string {
// 从数据模块获取用户信息
const userInfo = this.dataModule.getData().userInfo;
if (userInfo && userInfo.id === userId) {
return userInfo.role || 'employee';
}
// 如果是其他用户,默认为货运人员
return 'employee';
}
/**
* 获取员工图标
*/
private getEmployeeIcon(userId: number, userRole: string): string {
// 根据用户角色返回不同的图标
if (userRole === 'ADMIN') {
return '/images/crown.png'; // 管理员图标
} else {
return '/images/trucks.png'; // 货运人员图标
}
}
}

View File

@@ -234,9 +234,17 @@ export class LoginModule {
// 启动位置追踪服务
try {
// 先启动位置追踪服务
await locationTrackingService.startTrackingAfterSignIn();
console.log('位置追踪服务已启动');
// 然后调用位置模块的实时跟踪功能
const locationModule = this.dataModule.getLocationModule();
if (locationModule) {
await locationModule.startRealTimeTracking();
console.log('位置模块实时跟踪已启动');
}
// 订阅位置更新回调,采用统一方式更新所有用户位置
locationTrackingService.subscribeToLocationUpdates((onlineUsers) => {
console.log('🚚 位置更新回调 - 在线用户列表已更新,用户数量:', onlineUsers.length);
@@ -433,10 +441,8 @@ export class LoginModule {
// 根据服务器返回的状态映射到前端状态
switch (response.status) {
case 'signed_in':
case 'online':
return 'signed_in';
case 'signed_out':
case 'offline':
return 'signed_out';
case 'registered':
return 'registered';

View File

@@ -3,17 +3,32 @@ import { LoginModule } from './loginModule';
import { MapModule } from './mapModule';
import { OrderModule } from './orderModule';
import { WarehouseModule } from './warehouseModule';
import { DeliveryPersonModule } from './deliveryPersonModule';
import { EmployeeModule } from './deliveryPersonModule';
import { LocationModule } from './locationModule';
import { DataModule } from './dataModule';
import { showToast } from '../../../utils/helpers';
// 主页面模块接口定义
export interface MainPageModule {
getLoginModule(): LoginModule;
onLoad(): Promise<void>;
onShow(): void;
onHide(): void;
onLogout(): void;
hideAllPanels(): void;
resetMarkers(): void;
cleanup(): void;
refreshAllData(): Promise<void>;
}
export class MainPageModule {
private pageContext: any;
private loginModule: LoginModule;
private mapModule: MapModule;
private orderModule: OrderModule;
private warehouseModule: WarehouseModule;
private deliveryPersonModule: DeliveryPersonModule;
private employeeModule: EmployeeModule;
private locationModule: LocationModule;
private dataModule: DataModule;
constructor(pageContext: any) {
@@ -25,7 +40,8 @@ export class MainPageModule {
this.mapModule = new MapModule(pageContext, this.dataModule);
this.orderModule = new OrderModule(pageContext, this.dataModule);
this.warehouseModule = new WarehouseModule(pageContext, this.dataModule);
this.deliveryPersonModule = new DeliveryPersonModule(pageContext, this.dataModule);
this.employeeModule = new EmployeeModule(pageContext, this.dataModule);
this.locationModule = new LocationModule(pageContext, this.dataModule);
}
/**
@@ -38,6 +54,9 @@ export class MainPageModule {
// 初始化应用
await this.initApp();
// 初始化位置模块
await this.locationModule.initialize();
// 加载地图数据
await this.loadAllData();
@@ -58,12 +77,22 @@ export class MainPageModule {
this.refreshDataIfNeeded();
}
/**
* 页面隐藏
*/
onHide(): void {
console.log('主页面隐藏');
// 清理位置模块
this.locationModule.cleanup();
}
/**
* 初始化应用
*/
private async initApp(): Promise<void> {
// 初始化数据
this.dataModule.initializeData();
await this.dataModule.initializeData();
console.log('应用初始化');
}
@@ -119,7 +148,7 @@ export class MainPageModule {
// 并行加载各种业务数据(需要登录)
await Promise.all([
this.orderModule.loadPendingOrders(),
this.deliveryPersonModule.loadDeliveryPersons()
this.employeeModule.loadAllEmployees()
]);
console.log('所有业务数据加载完成');
@@ -160,7 +189,7 @@ export class MainPageModule {
this.dataModule.updatePendingOrders([]);
const filteredMarkers = this.pageContext.data.markers.filter((marker: any) =>
marker.type !== 'warehouse' && marker.type !== 'delivery_person'
marker.type !== 'warehouse' && marker.type !== 'employee'
);
this.dataModule.updateMarkers(filteredMarkers);
}
@@ -230,7 +259,7 @@ export class MainPageModule {
this.warehouseModule.onWarehouseMarkerClick(marker.data, e);
break;
case 'delivery_person':
this.deliveryPersonModule.onDeliveryPersonMarkerClick(marker.data, e);
this.employeeModule.onDeliveryPersonMarkerClick(marker.data, e);
break;
default:
this.mapModule.onMarkerTap(e);
@@ -267,10 +296,17 @@ export class MainPageModule {
}
/**
* 获取货运人员模块
* 获取员模块
*/
getDeliveryPersonModule(): DeliveryPersonModule {
return this.deliveryPersonModule;
getEmployeeModule(): EmployeeModule {
return this.employeeModule;
}
/**
* 获取位置模块
*/
getLocationModule(): LocationModule {
return this.locationModule;
}
/**

View File

@@ -83,11 +83,16 @@ export class MapModule {
return;
}
// 更新数据模块中的位置信息和缩放级别
// 强制重置地图视角到用户位置
// 1. 更新用户位置
this.dataModule.updateUserLocation(location.latitude, location.longitude);
this.dataModule.setMapScale(15); // 定位成功后放大到更详细级别
// 2. 重置地图缩放级别到15详细级别
this.dataModule.setMapScale(15);
// 3. 添加地图动画效果,让视角平滑回到用户位置
this.animateMapToUserLocation(location.latitude, location.longitude);
console.log('[MAP MODULE] 定位成功,坐标已更新:', location);
console.log('[MAP MODULE] 定位成功,坐标已更新,视角已重置:', location);
showToast('已定位到您的位置');
} catch (error) {
console.error('[MAP MODULE] 定位失败:', error);
this.setDefaultLocation();
@@ -105,9 +110,44 @@ export class MapModule {
// 更新数据模块中的位置信息和缩放级别
this.dataModule.updateUserLocation(defaultLatitude, defaultLongitude);
this.dataModule.setMapScale(13);
// 动画效果回到默认位置
this.animateMapToUserLocation(defaultLatitude, defaultLongitude);
console.log('[MAP MODULE] 默认位置设置完成');
}
/**
* 动画效果将地图视角平滑移动到用户位置
*/
private animateMapToUserLocation(latitude: number, longitude: number): void {
// 使用微信小程序的MapContext.moveToLocation方法实现地图移动
const mapContext = wx.createMapContext('myMap', this.pageContext);
// 先设置地图中心点
this.pageContext.setData({
latitude: latitude,
longitude: longitude
});
// 使用moveToLocation方法平滑移动地图视角
mapContext.moveToLocation({
latitude: latitude,
longitude: longitude,
success: () => {
console.log('[MAP MODULE] 地图视角移动成功,位置:', { latitude, longitude });
},
fail: (err: any) => {
console.error('[MAP MODULE] 地图视角移动失败:', err);
// 如果moveToLocation失败直接设置中心点
this.pageContext.setData({
latitude: latitude,
longitude: longitude
});
}
});
console.log('[MAP MODULE] 地图视角动画已启动,移动到位置:', { latitude, longitude });
}
/**
* 加载地图数据(仓库和货运人员)
@@ -126,39 +166,30 @@ export class MapModule {
/**
* 生成初始标记点
*/
private generateInitialMarkers(): Marker[] {
// 获取当前坐标
const longitude = this.pageContext.data.longitude;
const latitude = this.pageContext.data.latitude;
public generateInitialMarkers(): Marker[] {
console.log('生成初始标记点');
// 检查坐标是否为有效数字
let validLongitude = longitude;
let validLatitude = latitude;
const markers: Marker[] = [];
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, // 用户位置标记
// 获取用户位置
const userLocation = this.pageContext.data.userLocation;
if (userLocation && userLocation.longitude && userLocation.latitude) {
// 添加用户位置标记点
markers.push({
id: -1,
title: '用户位置',
longitude: validLongitude,
latitude: validLatitude,
longitude: userLocation.longitude,
latitude: userLocation.latitude,
iconPath: '/images/trucks.png',
width: 40,
height: 40,
zIndex: 99
}
];
});
console.log('已添加用户位置标记点');
}
return markers;
}
/**
@@ -191,6 +222,57 @@ export class MapModule {
console.log('用户头像已更新');
}
/**
* 更新员工标记点
*/
updateEmployeeMarkers(employeeMarkers: Marker[]): void {
console.log('开始更新员工标记点,数量:', employeeMarkers.length);
const { markers } = this.pageContext.data;
// 过滤掉现有的员工标记点
const filteredMarkers = markers.filter((marker: any) =>
marker.type !== 'employee'
);
// 合并标记点
const updatedMarkers = [...filteredMarkers, ...employeeMarkers];
// 更新数据模块中的标记点
this.dataModule.updateMarkers(updatedMarkers);
console.log('员工标记点更新完成,总标记点数量:', updatedMarkers.length);
}
/**
* 更新单个员工标记点
*/
updateSingleEmployeeMarker(employeeMarker: Marker): void {
console.log('更新单个员工标记点:', employeeMarker.id);
const { markers } = this.pageContext.data;
// 查找现有的员工标记点
const markerIndex = markers.findIndex((marker: any) =>
marker.type === 'employee' && marker.id === employeeMarker.id
);
let updatedMarkers;
if (markerIndex !== -1) {
// 更新现有的标记点
updatedMarkers = [...markers];
updatedMarkers[markerIndex] = employeeMarker;
} else {
// 添加新的标记点
updatedMarkers = [...markers, employeeMarker];
}
// 更新数据模块中的标记点
this.dataModule.updateMarkers(updatedMarkers);
console.log('单个员工标记点更新完成');
}
/**
* 处理地图点击事件
*/