修改位置交互,修改代码逻辑
This commit is contained in:
@@ -1,6 +1,5 @@
|
||||
// 地图服务 - 处理地图相关的功能,包括定位、路线规划、地理编码等
|
||||
import apiService from './apiService';
|
||||
import { isMockMode } from './apiService';
|
||||
|
||||
import { AMapRegeoResponse, RoutePlanResult, SearchResult, LocationData } from '../types';
|
||||
|
||||
/**
|
||||
@@ -20,14 +19,7 @@ class MapService {
|
||||
// 高德地图实例
|
||||
private amapInstance: any;
|
||||
|
||||
// 定时器引用,用于模拟实时位置更新
|
||||
private locationUpdateTimer: number | null;
|
||||
|
||||
// 位置更新回调函数
|
||||
private locationUpdateCallback: ((location: LocationData) => void) | null;
|
||||
|
||||
// 取消订阅函数
|
||||
private unsubscribeFunction: (() => void) | null;
|
||||
|
||||
|
||||
/**
|
||||
* 构造函数,初始化地图实例
|
||||
@@ -40,9 +32,6 @@ class MapService {
|
||||
latitude: 24.880095
|
||||
};
|
||||
this.amapInstance = new this.amapFile.AMapWX({ key: this.MAP_KEY });
|
||||
this.locationUpdateTimer = null;
|
||||
this.locationUpdateCallback = null;
|
||||
this.unsubscribeFunction = null;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -50,15 +39,6 @@ class MapService {
|
||||
* @returns 用户位置坐标
|
||||
*/
|
||||
async getLocation(): Promise<{ longitude: number; latitude: number }> {
|
||||
if (isMockMode) {
|
||||
// 模拟位置 - 昆明市中心
|
||||
console.log('[MOCK] 获取用户位置信息');
|
||||
return {
|
||||
longitude: 102.7123,
|
||||
latitude: 25.0409
|
||||
};
|
||||
}
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
console.log('开始调用高德地图SDK获取位置...');
|
||||
|
||||
@@ -149,18 +129,6 @@ class MapService {
|
||||
speed?: number;
|
||||
altitude?: number;
|
||||
}> {
|
||||
if (isMockMode) {
|
||||
// 模拟位置 - 昆明市中心
|
||||
console.log('[MOCK] 获取当前位置信息(兼容格式)');
|
||||
return {
|
||||
latitude: 25.0409,
|
||||
longitude: 102.7123,
|
||||
accuracy: 50,
|
||||
speed: 0,
|
||||
altitude: 1891
|
||||
};
|
||||
}
|
||||
|
||||
const location = await this.getLocation();
|
||||
return {
|
||||
latitude: location.latitude,
|
||||
@@ -178,23 +146,6 @@ class MapService {
|
||||
* @returns 逆地理编码结果
|
||||
*/
|
||||
async getLocationInfo(longitude: number, latitude: number): Promise<AMapRegeoResponse> {
|
||||
if (isMockMode) {
|
||||
// 模拟逆地理编码结果 - 完全符合AMapRegeoResponse接口
|
||||
console.log('[MOCK] 获取位置详细信息(逆地理编码),坐标:', longitude, latitude);
|
||||
return {
|
||||
regeocode: {
|
||||
addressComponent: {
|
||||
province: '云南省',
|
||||
city: '昆明市',
|
||||
district: '五华区',
|
||||
street: '',
|
||||
township: ''
|
||||
}
|
||||
},
|
||||
status: '1'
|
||||
};
|
||||
}
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
this.amapInstance.getRegeo({
|
||||
location: `${longitude.toFixed(6)},${latitude.toFixed(6)}`,
|
||||
@@ -225,32 +176,6 @@ class MapService {
|
||||
* @returns 搜索结果列表
|
||||
*/
|
||||
async searchPoi(keyword: string, city: string): Promise<SearchResult[]> {
|
||||
if (isMockMode) {
|
||||
// 模拟搜索结果
|
||||
console.log('[MOCK] 搜索地点,关键词:', keyword, '城市:', city);
|
||||
const mockResults: SearchResult[] = [
|
||||
{
|
||||
id: '1',
|
||||
name: '昆明火车站',
|
||||
address: '昆明市官渡区北京路',
|
||||
longitude: 102.7222,
|
||||
latitude: 25.0157,
|
||||
phone: ''
|
||||
},
|
||||
{
|
||||
id: '2',
|
||||
name: '昆明长水国际机场',
|
||||
address: '昆明市官渡区长水村',
|
||||
longitude: 102.9292,
|
||||
latitude: 25.1012,
|
||||
phone: ''
|
||||
}
|
||||
];
|
||||
return mockResults.filter(item =>
|
||||
item.name.includes(keyword) || item.address.includes(keyword)
|
||||
);
|
||||
}
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
this.amapInstance.getPoiAround({
|
||||
querykeywords: keyword,
|
||||
@@ -289,22 +214,6 @@ class MapService {
|
||||
* @returns 路线规划结果
|
||||
*/
|
||||
async getDrivingRoute(origin: string, destination: string): Promise<RoutePlanResult> {
|
||||
if (isMockMode) {
|
||||
// 模拟路线规划结果
|
||||
console.log('[MOCK] 规划驾车路线,起点:', origin, '终点:', destination);
|
||||
const [originLng, originLat] = origin.split(',').map(Number);
|
||||
const [destLng, destLat] = destination.split(',').map(Number);
|
||||
|
||||
const distance = this.calculateDistance(originLat, originLng, destLat, destLng);
|
||||
const duration = Math.round(distance / 10); // 假设平均速度10米/秒
|
||||
|
||||
return {
|
||||
polyline: '',
|
||||
distance,
|
||||
duration
|
||||
};
|
||||
}
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
this.amapInstance.getDrivingRoute({
|
||||
origin,
|
||||
@@ -369,41 +278,16 @@ class MapService {
|
||||
* 获取路线规划(通用版本)
|
||||
* @param origin 起点坐标
|
||||
* @param destination 终点坐标
|
||||
* @param mode 交通方式
|
||||
* @returns 路线规划结果
|
||||
*/
|
||||
async getRoute(
|
||||
origin: { latitude: number; longitude: number },
|
||||
destination: { latitude: number; longitude: number },
|
||||
mode: 'driving' | 'walking' | 'bicycling' = 'driving'
|
||||
destination: { latitude: number; longitude: number }
|
||||
): Promise<{
|
||||
distance: number; // 距离(米)
|
||||
duration: number; // 时间(秒)
|
||||
polyline: { latitude: number; longitude: number }[];
|
||||
}> {
|
||||
if (isMockMode) {
|
||||
// 模拟路线规划
|
||||
console.log('[MOCK] 获取路线规划,起点:', origin, '终点:', destination, '模式:', mode);
|
||||
const distance = this.calculateDistance(
|
||||
origin.latitude, origin.longitude,
|
||||
destination.latitude, destination.longitude
|
||||
);
|
||||
|
||||
const duration = Math.round(distance / 10); // 假设平均速度10米/秒
|
||||
|
||||
// 简单的直线路径
|
||||
const polyline = [
|
||||
origin,
|
||||
destination
|
||||
];
|
||||
|
||||
return {
|
||||
distance,
|
||||
duration,
|
||||
polyline
|
||||
};
|
||||
}
|
||||
|
||||
// 对于真实模式,目前只支持驾车路线
|
||||
const originStr = `${origin.longitude},${origin.latitude}`;
|
||||
const destStr = `${destination.longitude},${destination.latitude}`;
|
||||
@@ -442,34 +326,6 @@ class MapService {
|
||||
longitude: number;
|
||||
formattedAddress: string;
|
||||
}> {
|
||||
if (isMockMode) {
|
||||
// 模拟地理编码结果
|
||||
console.log('[MOCK] 地理编码,地址:', address);
|
||||
const mockLocations: { [key: string]: { latitude: number; longitude: number; formattedAddress: string } } = {
|
||||
'昆明市五华区二环西路599号': {
|
||||
latitude: 25.055281,
|
||||
longitude: 102.705745,
|
||||
formattedAddress: '云南省昆明市五华区二环西路599号'
|
||||
},
|
||||
'昆明市盘龙区北京路1188号': {
|
||||
latitude: 25.042498,
|
||||
longitude: 102.728421,
|
||||
formattedAddress: '云南省昆明市盘龙区北京路1188号'
|
||||
},
|
||||
'昆明市西山区滇池路1234号': {
|
||||
latitude: 25.028234,
|
||||
longitude: 102.689190,
|
||||
formattedAddress: '云南省昆明市西山区滇池路1234号'
|
||||
}
|
||||
};
|
||||
|
||||
return mockLocations[address] || {
|
||||
latitude: 25.0409,
|
||||
longitude: 102.7123,
|
||||
formattedAddress: '云南省昆明市'
|
||||
};
|
||||
}
|
||||
|
||||
// 对于真实模式,使用搜索API来模拟地理编码
|
||||
const results = await this.searchPoi(address, '昆明');
|
||||
if (results.length > 0) {
|
||||
@@ -502,18 +358,6 @@ class MapService {
|
||||
district: string;
|
||||
street: string;
|
||||
}> {
|
||||
if (isMockMode) {
|
||||
console.log('[MOCK] 逆地理编码,坐标:', latitude, longitude);
|
||||
return {
|
||||
formattedAddress: '云南省昆明市五华区',
|
||||
country: '中国',
|
||||
province: '云南省',
|
||||
city: '昆明市',
|
||||
district: '五华区',
|
||||
street: ''
|
||||
};
|
||||
}
|
||||
|
||||
const result = await this.getLocationInfo(longitude, latitude);
|
||||
|
||||
return {
|
||||
@@ -548,39 +392,6 @@ class MapService {
|
||||
address: string;
|
||||
distance: number;
|
||||
}>> {
|
||||
if (isMockMode) {
|
||||
// 模拟附近地点
|
||||
console.log('[MOCK] 获取附近的地点,坐标:', latitude, longitude, '半径:', radius, '类型:', type);
|
||||
const mockPlaces = [
|
||||
{
|
||||
id: '1',
|
||||
name: '昆明火车站',
|
||||
latitude: 25.0157,
|
||||
longitude: 102.7222,
|
||||
address: '昆明市官渡区北京路',
|
||||
distance: this.calculateDistance(latitude, longitude, 25.0157, 102.7222)
|
||||
},
|
||||
{
|
||||
id: '2',
|
||||
name: '昆明长水国际机场',
|
||||
latitude: 25.1012,
|
||||
longitude: 102.9292,
|
||||
address: '昆明市官渡区长水村',
|
||||
distance: this.calculateDistance(latitude, longitude, 25.1012, 102.9292)
|
||||
},
|
||||
{
|
||||
id: '3',
|
||||
name: '翠湖公园',
|
||||
latitude: 25.0486,
|
||||
longitude: 102.7042,
|
||||
address: '昆明市五华区翠湖公园',
|
||||
distance: this.calculateDistance(latitude, longitude, 25.0486, 102.7042)
|
||||
}
|
||||
];
|
||||
|
||||
return mockPlaces.filter(place => place.distance <= radius);
|
||||
}
|
||||
|
||||
// 对于真实模式,使用搜索API
|
||||
const keyword = type ? type : '';
|
||||
const results = await this.searchPoi(keyword, '昆明');
|
||||
@@ -595,164 +406,13 @@ class MapService {
|
||||
})).filter(place => place.distance <= radius);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取位置历史记录
|
||||
* @param userId 用户ID
|
||||
* @param limit 记录数量限制
|
||||
* @returns 位置历史记录列表
|
||||
*/
|
||||
async getLocationHistory(userId: number, limit: number = 50): Promise<Array<{
|
||||
id: number;
|
||||
latitude: number;
|
||||
longitude: number;
|
||||
timestamp: number;
|
||||
accuracy?: number;
|
||||
speed?: number;
|
||||
altitude?: number;
|
||||
}>> {
|
||||
if (isMockMode) {
|
||||
// 模拟位置历史记录
|
||||
console.log('[MOCK] 获取位置历史记录,用户ID:', userId, '限制:', limit);
|
||||
const history: Array<{ id: number; latitude: number; longitude: number; timestamp: number; accuracy?: number; speed?: number; altitude?: number }> = [];
|
||||
|
||||
const baseLat = 25.0409;
|
||||
const baseLng = 102.7123;
|
||||
|
||||
for (let i = 0; i < limit; i++) {
|
||||
history.push({
|
||||
id: i + 1,
|
||||
latitude: baseLat + (Math.random() - 0.5) * 0.01,
|
||||
longitude: baseLng + (Math.random() - 0.5) * 0.01,
|
||||
timestamp: Date.now() - i * 3600000, // 每小时一个记录
|
||||
accuracy: 20 + Math.random() * 30,
|
||||
speed: Math.random() * 10,
|
||||
altitude: 1891 + (Math.random() - 0.5) * 20
|
||||
});
|
||||
}
|
||||
|
||||
return history.sort((a, b) => b.timestamp - a.timestamp);
|
||||
}
|
||||
// 修复参数名不匹配问题,并转换返回值类型以匹配mapService定义
|
||||
const apiResults = await apiService.getLocationHistory(userId, limit);
|
||||
return apiResults.map((result, index) => ({
|
||||
...result,
|
||||
id: index + 1, // 添加id字段
|
||||
altitude: 1891 + (Math.random() - 0.5) * 20 // 添加默认海拔高度
|
||||
})).sort((a, b) => b.timestamp - a.timestamp);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 批量更新货运人员位置
|
||||
* @param locations 位置信息数组
|
||||
*/
|
||||
async batchUpdateDeliveryPersonLocations(
|
||||
locations: Array<{ userId: number; latitude: number; longitude: number; timestamp: number }>
|
||||
): Promise<void> {
|
||||
if (isMockMode) {
|
||||
console.log('[MOCK] 批量更新货运人员位置:', locations);
|
||||
return;
|
||||
}
|
||||
return apiService.batchUpdateDeliveryPersonLocations(locations);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 订阅实时位置更新
|
||||
* @param callback 位置更新回调函数
|
||||
*/
|
||||
async subscribeToRealTimeLocations(callback: (location: LocationData) => void): Promise<void> {
|
||||
if (isMockMode) {
|
||||
// 模拟模式下,定期生成随机位置更新
|
||||
console.log('[MOCK] 订阅实时位置更新');
|
||||
|
||||
// 保存回调引用
|
||||
this.locationUpdateCallback = callback;
|
||||
|
||||
// 启动模拟更新定时器
|
||||
if (!this.locationUpdateTimer) {
|
||||
this.locationUpdateTimer = setInterval(() => {
|
||||
// 生成随机位置更新数据
|
||||
const randomLocation: LocationData = {
|
||||
userId: Math.floor(Math.random() * 100) + 100, // 模拟货运人员ID
|
||||
longitude: 102.714585 + (Math.random() - 0.5) * 0.05, // 昆明附近随机经度
|
||||
latitude: 25.046321 + (Math.random() - 0.5) * 0.05, // 昆明附近随机纬度
|
||||
timestamp: Date.now()
|
||||
};
|
||||
|
||||
// 调用回调函数
|
||||
if (this.locationUpdateCallback) {
|
||||
this.locationUpdateCallback(randomLocation);
|
||||
}
|
||||
}, 3000); // 每3秒更新一次
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
// 真实环境中调用API服务的订阅方法
|
||||
try {
|
||||
// 获取当前用户信息
|
||||
const userInfo = wx.getStorageSync('userInfo');
|
||||
if (!userInfo || !userInfo.id) {
|
||||
throw new Error('用户未登录,无法订阅位置更新');
|
||||
}
|
||||
|
||||
// 初始化WebSocket连接
|
||||
await apiService.initLocationWebSocket();
|
||||
|
||||
// 订阅位置更新
|
||||
await apiService.subscribeToLocationUpdates(userInfo.id);
|
||||
|
||||
// 使用新的onLocationUpdate方法来注册回调
|
||||
this.unsubscribeFunction = apiService.onLocationUpdate((location) => {
|
||||
callback(location as LocationData);
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('订阅实时位置更新失败:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 取消订阅实时位置更新
|
||||
*/
|
||||
async unsubscribeFromRealTimeLocations(): Promise<void> {
|
||||
if (isMockMode) {
|
||||
// 模拟模式下,清除定时器
|
||||
console.log('[MOCK] 取消订阅实时位置更新');
|
||||
|
||||
if (this.locationUpdateTimer) {
|
||||
clearInterval(this.locationUpdateTimer);
|
||||
this.locationUpdateTimer = null;
|
||||
}
|
||||
|
||||
// 清除回调引用
|
||||
this.locationUpdateCallback = null;
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
// 真实环境中调用API服务的取消订阅方法
|
||||
try {
|
||||
// 获取当前用户信息
|
||||
const userInfo = wx.getStorageSync('userInfo');
|
||||
if (userInfo && userInfo.id) {
|
||||
// 取消订阅
|
||||
await apiService.unsubscribeFromLocationUpdates(userInfo.id);
|
||||
}
|
||||
|
||||
// 调用取消订阅函数
|
||||
if (this.unsubscribeFunction) {
|
||||
this.unsubscribeFunction();
|
||||
this.unsubscribeFunction = null;
|
||||
}
|
||||
|
||||
// 关闭WebSocket连接
|
||||
apiService.closeLocationWebSocket();
|
||||
} catch (error) {
|
||||
console.error('取消订阅实时位置更新失败:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 格式化路线距离
|
||||
|
Reference in New Issue
Block a user