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

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

@@ -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('单个员工标记点更新完成');
}
/**
* 处理地图点击事件
*/