文档开发者中心
我的
|

HarmonyOS 5

使用高德地图 SDK 实现路径规划功能

在开发导航类应用时,需要实现多种出行方式的路径规划:

1. 支持步行、骑行、驾车、公交四种出行方式

2. 返回路线距离、预计用时、详细导航步骤

3. 驾车路线需要显示路况信息(拥堵状态)

4. 公交路线需要显示换乘方案、途经站点

5. 返回路线坐标点用于地图上绑制路线

点赞
收藏
回复
1
分享
举报
浏览137 发布于2025-12-24 07:21湖北
全部评论
最多点赞
最新发布
最早发布

1. 配置高德地图 SDK

在 oh-package.json5 中添加依赖:

收起
自动换行
深色代码主题
复制
{
  "dependencies": {
    "@amap/amap_lbs_search": "^1.0.0",
    "@amap/amap_lbs_map3d": "^1.0.0"
  }
}

2. 定义路线数据模型

收起
自动换行
深色代码主题
复制
// models/RouteModels.ets
import { LatLng } from '@amap/amap_lbs_map3d'
import { TMC } from '@amap/amap_lbs_search'

/**
 * 路线类型
 */
export type RouteType = 'walk' | 'ride' | 'drive' | 'bus'

/**
 * 路线步骤
 */
export interface RouteStep {
  instruction: string   // 导航指令
  action: string        // 动作(如"左转"、"直行")
  distance: number      // 距离(米)
  duration: number      // 时长(秒)
}

/**
 * 路线结果
 */
export interface RouteResult {
  type: RouteType
  distance: number      // 总距离(米)
  duration: number      // 总时长(秒)
  steps: RouteStep[]    // 导航步骤
  polyline: LatLng[]    // 路线坐标点(用于绑制)
  
  // 驾车专用
  taxiCost?: number     // 打车费用
  tolls?: number        // 过路费
  tmcs?: TMC[]          // 路况信息
  
  // 公交专用
  busLines?: string[]   // 公交线路名称
  walkDistance?: number // 步行距离
}

3. 创建路线搜索服务

收起
自动换行
深色代码主题
复制
// services/RouteSearchService.ets
import {
  RouteSearchV2,
  WalkRouteQueryV2,
  DriveRouteQueryV2,
  RideRouteQueryV2,
  BusRouteQueryV2,
  WalkRouteResultV2,
  DriveRouteResultV2,
  RideRouteResultV2,
  BusRouteResultV2,
  FromAndTo,
  LatLonPoint,
  DrivingStrategy,
  AMapException,
  OnRouteSearchListenerV2,
  RouteSearch
} from '@amap/amap_lbs_search'
import { LatLng } from '@amap/amap_lbs_map3d'
import { RouteResult, RouteStep } from '../models/RouteModels'

/**
 * 路线搜索回调
 */
export interface RouteSearchCallback {
  onSuccess: (routes: RouteResult[]) => void
  onError: (message: string) => void
}

/**
 * 路线搜索服务类
 */
export class RouteSearchService {
  private routeSearch?: RouteSearchV2
  private context: Context
  private callback?: RouteSearchCallback

  constructor(context: Context) {
    this.context = context
  }
}

4. 实现步行路线搜索

收起
自动换行
深色代码主题
复制
/**
 * 搜索步行路线
 */
searchWalkRoute(start: LatLonPoint, end: LatLonPoint, callback: RouteSearchCallback): void {
  this.callback = callback
  
  console.info(`[RouteSearch] 🚶 开始搜索步行路线`)
  console.info(`  起点: ${start.getLatitude()}, ${start.getLongitude()}`)
  console.info(`  终点: ${end.getLatitude()}, ${end.getLongitude()}`)
  
  this.routeSearch = new RouteSearchV2(this.context)
  this.routeSearch.setRouteSearchListener(this.createWalkListener())
  
  const fromAndTo = new FromAndTo(start, end)
  const query = new WalkRouteQueryV2(fromAndTo)
  query.setShowFields(0b0011111)  // 返回所有字段
  
  this.routeSearch.calculateWalkRouteAsyn(query)
}

/**
 * 创建步行路线监听器
 */
private createWalkListener(): OnRouteSearchListenerV2 {
  return {
    onWalkRouteSearched: (result: WalkRouteResultV2, errorCode: number) => {
      if (errorCode === AMapException.CODE_AMAP_SUCCESS) {
        const paths = result.getPaths()
        if (paths && paths.length > 0) {
          const routeResults: RouteResult[] = []
          
          for (const path of paths) {
            const steps: RouteStep[] = []
            const pathSteps = path.getSteps()
            
            if (pathSteps) {
              for (const step of pathSteps) {
                steps.push({
                  instruction: step.getInstruction() || '',
                  action: step.getAction() || '',
                  distance: step.getDistance() || 0,
                  duration: step.getDuration() || 0
                })
              }
            }

            // 获取路线坐标点
            const polylinePoints: LatLng[] = []
            const polylineData = path.getPolyline()
            if (polylineData) {
              for (const point of polylineData) {
                polylinePoints.push(new LatLng(point.getLatitude(), point.getLongitude()))
              }
            }

            routeResults.push({
              type: 'walk',
              distance: path.getDistance() || 0,
              duration: path.getDuration() || 0,
              steps: steps,
              polyline: polylinePoints
            })
          }
          
          console.info(`[RouteSearch] ✅ 找到 ${routeResults.length} 条步行路线`)
          this.callback?.onSuccess(routeResults)
        } else {
          this.callback?.onError('未找到步行路线')
        }
      } else {
        this.callback?.onError(`路线搜索失败,错误码: ${errorCode}`)
      }
    },
    // 其他回调留空
    onDriveRouteSearched: () => {},
    onRideRouteSearched: () => {},
    onBusRouteSearched: () => {}
  }
}

5. 实现驾车路线搜索

收起
自动换行
深色代码主题
复制
/**
 * 搜索驾车路线
 * @param strategy 驾车策略(可选)
 */
searchDriveRoute(
  start: LatLonPoint,
  end: LatLonPoint,
  callback: RouteSearchCallback,
  strategy: number = DrivingStrategy.DEFAULT.getValue()
): void {
  this.callback = callback
  
  console.info(`[RouteSearch] 🚗 开始搜索驾车路线(策略: ${strategy})`)
  
  this.routeSearch = new RouteSearchV2(this.context)
  this.routeSearch.setRouteSearchListener(this.createDriveListener())
  
  const fromAndTo = new FromAndTo(start, end)
  const query = new DriveRouteQueryV2(fromAndTo, strategy, undefined, undefined, "")
  query.setShowFields(0b0011111)
  
  this.routeSearch.calculateDriveRouteAsyn(query)
}

/**
 * 创建驾车路线监听器
 */
private createDriveListener(): OnRouteSearchListenerV2 {
  return {
    onDriveRouteSearched: (result: DriveRouteResultV2 | undefined, errorCode: number) => {
      if (errorCode === AMapException.CODE_AMAP_SUCCESS && result) {
        const paths = result.getPaths()
        if (paths && paths.length > 0) {
          const routeResults: RouteResult[] = []
          
          for (const path of paths) {
            const steps: RouteStep[] = []
            const tmcs: TMC[] = []
            const pathSteps = path.getSteps()
            
            if (pathSteps) {
              for (const step of pathSteps) {
                const navi = step.getNavi()
                steps.push({
                  instruction: step.getInstruction() || '',
                  action: navi?.getAction() || '',
                  distance: this.parseDistanceFromInstruction(step.getInstruction() || ''),
                  duration: 0
                })
                
                // 提取路况信息(TMC)
                const tmcList = step.getTMCs()
                if (tmcList) {
                  for (const tmc of tmcList) {
                    tmcs.push(tmc)
                  }
                }
              }
            }

            const polylinePoints: LatLng[] = []
            const polylineData = path.getPolyline()
            if (polylineData) {
              for (const point of polylineData) {
                polylinePoints.push(new LatLng(point.getLatitude(), point.getLongitude()))
              }
            }

            routeResults.push({
              type: 'drive',
              distance: path.getDistance() || 0,
              duration: path.getCost()?.getDuration() || 0,
              steps: steps,
              polyline: polylinePoints,
              taxiCost: Math.floor(result.getTaxiCost() || 0),
              tolls: 0,
              tmcs: tmcs
            })
          }
          
          console.info(`[RouteSearch] ✅ 找到 ${routeResults.length} 条驾车路线`)
          this.callback?.onSuccess(routeResults)
        } else {
          this.callback?.onError('未找到驾车路线')
        }
      } else {
        this.callback?.onError(`路线搜索失败,错误码: ${errorCode}`)
      }
    },
    onWalkRouteSearched: () => {},
    onRideRouteSearched: () => {},
    onBusRouteSearched: () => {}
  }
}

6. 实现骑行路线搜索

收起
自动换行
深色代码主题
复制
/**
 * 搜索骑行路线
 */
searchRideRoute(start: LatLonPoint, end: LatLonPoint, callback: RouteSearchCallback): void {
  this.callback = callback
  
  console.info(`[RouteSearch] 🚴 开始搜索骑行路线`)
  
  this.routeSearch = new RouteSearchV2(this.context)
  this.routeSearch.setRouteSearchListener(this.createRideListener())
  
  const fromAndTo = new FromAndTo(start, end)
  const query = new RideRouteQueryV2(fromAndTo)
  query.setShowFields(0b0011111)
  
  this.routeSearch.calculateRideRouteAsyn(query)
}

/**
 * 创建骑行路线监听器
 */
private createRideListener(): OnRouteSearchListenerV2 {
  return {
    onRideRouteSearched: (result: RideRouteResultV2, errorCode: number) => {
      if (errorCode === AMapException.CODE_AMAP_SUCCESS) {
        const paths = result.getPaths()
        if (paths && paths.length > 0) {
          const routeResults: RouteResult[] = paths.map(path => {
            const steps: RouteStep[] = []
            const pathSteps = path.getSteps()
            
            if (pathSteps) {
              for (const step of pathSteps) {
                steps.push({
                  instruction: step.getInstruction() || '',
                  action: step.getAction() || '',
                  distance: step.getDistance() || 0,
                  duration: step.getDuration() || 0
                })
              }
            }

            const polylinePoints: LatLng[] = []
            const polylineData = path.getPolyline()
            if (polylineData) {
              for (const point of polylineData) {
                polylinePoints.push(new LatLng(point.getLatitude(), point.getLongitude()))
              }
            }

            return {
              type: 'ride' as const,
              distance: path.getDistance() || 0,
              duration: path.getDuration() || 0,
              steps: steps,
              polyline: polylinePoints
            }
          })
          
          console.info(`[RouteSearch] ✅ 找到 ${routeResults.length} 条骑行路线`)
          this.callback?.onSuccess(routeResults)
        } else {
          this.callback?.onError('未找到骑行路线')
        }
      } else {
        this.callback?.onError(`路线搜索失败,错误码: ${errorCode}`)
      }
    },
    onWalkRouteSearched: () => {},
    onDriveRouteSearched: () => {},
    onBusRouteSearched: () => {}
  }
}

7. 实现公交路线搜索

收起
自动换行
深色代码主题
复制
/**
 * 搜索公交路线
 * @param cityCode 城市代码(如北京 '010')
 */
searchBusRoute(
  start: LatLonPoint,
  end: LatLonPoint,
  callback: RouteSearchCallback,
  cityCode: string
): void {
  this.callback = callback
  
  console.info(`[RouteSearch] 🚌 开始搜索公交路线(城市: ${cityCode})`)
  
  this.routeSearch = new RouteSearchV2(this.context)
  this.routeSearch.setRouteSearchListener(this.createBusListener())
  
  const fromAndTo = new FromAndTo(start, end)
  const query = new BusRouteQueryV2(fromAndTo, RouteSearch.BusDefault, cityCode, 0)
  query.setCityd(cityCode)  // 目的地城市代码
  query.setShowFields(0b0011111)
  
  this.routeSearch.calculateBusRouteAsyn(query)
}

/**
 * 创建公交路线监听器
 */
private createBusListener(): OnRouteSearchListenerV2 {
  return {
    onBusRouteSearched: (result: BusRouteResultV2 | undefined, errorCode: number) => {
      if (errorCode === AMapException.CODE_AMAP_SUCCESS && result) {
        const paths = result.getPaths()
        if (paths && paths.length > 0) {
          const routeResults: RouteResult[] = []
          
          for (const path of paths) {
            const steps: RouteStep[] = []
            const polylinePoints: LatLng[] = []
            const busLineNames: string[] = []
            let totalWalkDistance = 0
            
            const busSteps = path.getSteps()
            if (busSteps) {
              for (const busStep of busSteps) {
                // 步行段
                const walk = busStep.getWalk()
                if (walk) {
                  const walkDistance = walk.getDistance() || 0
                  totalWalkDistance += walkDistance
                  
                  const walkSteps = walk.getSteps()
                  if (walkSteps) {
                    for (const walkStep of walkSteps) {
                      steps.push({
                        instruction: `步行 ${walkStep.getInstruction() || ''}`,
                        action: 'walk',
                        distance: walkStep.getDistance() || 0,
                        duration: walkStep.getDuration() || 0
                      })
                    }
                  }
                }
                
                // 公交/地铁段
                const busLines = busStep.getBusLines()
                if (busLines && busLines.length > 0) {
                  for (const busLine of busLines) {
                    const lineName = busLine.getBusLineName() || ''
                    const departure = busLine.getDepartureBusStation()?.getBusStationName() || ''
                    const arrival = busLine.getArrivalBusStation()?.getBusStationName() || ''
                    const passCount = busLine.getPassStationNum() || 0
                    
                    busLineNames.push(lineName)
                    
                    steps.push({
                      instruction: `乘坐 ${lineName},${departure} 上车,${arrival} 下车,途经 ${passCount} 站`,
                      action: 'bus',
                      distance: busLine.getDistance() || 0,
                      duration: busLine.getDuration() || 0
                    })
                    
                    // 获取公交线路坐标
                    const busPolyline = busLine.getPolyline()
                    if (busPolyline) {
                      for (const point of busPolyline) {
                        polylinePoints.push(new LatLng(point.getLatitude(), point.getLongitude()))
                      }
                    }
                  }
                }
              }
            }

            routeResults.push({
              type: 'bus',
              distance: path.getDistance() || 0,
              duration: path.getDuration() || 0,
              steps: steps,
              polyline: polylinePoints,
              busLines: busLineNames,
              walkDistance: totalWalkDistance
            })
          }
          
          console.info(`[RouteSearch] ✅ 找到 ${routeResults.length} 条公交路线`)
          this.callback?.onSuccess(routeResults)
        } else {
          this.callback?.onError('未找到公交路线')
        }
      } else {
        this.callback?.onError(`路线搜索失败,错误码: ${errorCode}`)
      }
    },
    onWalkRouteSearched: () => {},
    onDriveRouteSearched: () => {},
    onRideRouteSearched: () => {}
  }
}

8. 在页面中使用

收起
自动换行
深色代码主题
复制
// pages/RoutePlanPage.ets
import { RouteSearchService, RouteSearchCallback } from '../services/RouteSearchService'
import { RouteResult, RouteType } from '../models/RouteModels'
import { LatLonPoint } from '@amap/amap_lbs_search'

@Entry
@Component
struct RoutePlanPage {
  @State selectedType: RouteType = 'walk'
  @State routeResult: RouteResult | null = null
  @State isSearching: boolean = false
  
  private routeService?: RouteSearchService
  
  // 起终点
  private start = new LatLonPoint(39.9042, 116.4074)  // 天安门
  private end = new LatLonPoint(39.9163, 116.3972)   // 北海公园
  
  aboutToAppear(): void {
    this.routeService = new RouteSearchService(getContext(this))
  }
  
  build() {
    Column({ space: 16 }) {
      // 出行方式选择
      Row({ space: 8 }) {
        ForEach(['walk', 'ride', 'drive', 'bus'], (type: RouteType) => {
          Button(this.getTypeLabel(type))
            .backgroundColor(this.selectedType === type ? '#A9846A' : '#E0E0E0')
            .fontColor(this.selectedType === type ? '#FFFFFF' : '#333333')
            .onClick(() => {
              this.selectedType = type
              this.searchRoute()
            })
        })
      }
      
      // 搜索状态
      if (this.isSearching) {
        LoadingProgress().width(40).height(40)
      }
      
      // 路线结果
      if (this.routeResult) {
        Column({ space: 8 }) {
          Text(`总距离: ${this.formatDistance(this.routeResult.distance)}`)
          Text(`预计用时: ${this.formatDuration(this.routeResult.duration)}`)
          
          if (this.routeResult.taxiCost) {
            Text(`预计打车费: ¥${this.routeResult.taxiCost}`)
          }
          
          Divider()
          
          // 导航步骤
          List() {
            ForEach(this.routeResult.steps, (step: RouteStep, index: number) => {
              ListItem() {
                Row({ space: 8 }) {
                  Text(`${index + 1}`)
                    .width(24)
                    .textAlign(TextAlign.Center)
                  Text(step.instruction)
                    .layoutWeight(1)
                  Text(this.formatDistance(step.distance))
                    .fontColor('#666666')
                }
                .padding(8)
              }
            })
          }
          .layoutWeight(1)
        }
        .alignItems(HorizontalAlign.Start)
      }
    }
    .width('100%')
    .height('100%')
    .padding(16)
  }
  
  private searchRoute(): void {
    if (!this.routeService) return
    
    this.isSearching = true
    this.routeResult = null
    
    const callback: RouteSearchCallback = {
      onSuccess: (routes) => {
        this.isSearching = false
        if (routes.length > 0) {
          this.routeResult = routes[0]
        }
      },
      onError: (message) => {
        this.isSearching = false
        console.error(message)
      }
    }
    
    switch (this.selectedType) {
      case 'walk':
        this.routeService.searchWalkRoute(this.start, this.end, callback)
        break
      case 'ride':
        this.routeService.searchRideRoute(this.start, this.end, callback)
        break
      case 'drive':
        this.routeService.searchDriveRoute(this.start, this.end, callback)
        break
      case 'bus':
        this.routeService.searchBusRoute(this.start, this.end, callback, '010')
        break
    }
  }
  
  private getTypeLabel(type: RouteType): string {
    const labels: Record<RouteType, string> = {
      'walk': '步行', 'ride': '骑行', 'drive': '驾车', 'bus': '公交'
    }
    return labels[type]
  }
  
  private formatDistance(meters: number): string {
    return meters >= 1000 ? `${(meters / 1000).toFixed(1)}km` : `${meters}m`
  }
  
  private formatDuration(seconds: number): string {
    const hours = Math.floor(seconds / 3600)
    const minutes = Math.floor((seconds % 3600) / 60)
    return hours > 0 ? `${hours}小时${minutes}分钟` : `${minutes}分钟`
  }
}

效果

控制台日志:

收起
自动换行
深色代码主题
复制
[RouteSearch] 🚗 开始搜索驾车路线(策略: 0)
  起点: 39.9042, 116.4074
  终点: 39.9163, 116.3972
[RouteSearch] ✅ 找到 3 条驾车路线

结果示例:

出行方式 距离 用时 其他信息
步行 2.1km 28分钟 9个导航步骤
骑行 2.3km 12分钟 7个导航步骤
驾车 3.5km 15分钟 打车费约¥18
公交 4.2km 25分钟 地铁1号线→步行500m

关键点总结

要点 说明
异步回调 使用 OnRouteSearchListenerV2 监听搜索结果
坐标点 LatLonPoint 表示起终点
路线坐标 getPolyline() 返回路线点用于地图绑制
驾车策略 DrivingStrategy 枚举(速度优先/费用优先等)
公交城市 必须传入城市代码(如北京 '010')
展开全部
1楼回复于2025-12-24 07:25 来自湖北
写回答
新增插入模板功能
一键使用模板,快速填写内容,轻松发帖~
知道了
  • 为了保障您的信息安全,请勿上传您的敏感个人信息(如您的密码等信息)和您的敏感资产信息(如关键源代码、签名私钥、调试安装包、业务日志等信息),且您需自行承担由此产生的信息泄露等安全风险。
  • 如您发布的内容为转载内容,请注明内容来源。

我要发帖子

了解社区公约,与您携手共创和谐专业的开发者社区。