Overview
The driving route planning feature generates travel routes based on the origin, destination, waypoints, and route strategy settings. It also incorporates real-time traffic information to help drivers avoid congested sections, providing a more convenient and user-friendly driving experience.
To implement driving route planning, you need to understand the following classes:
* AMapNaviDriveManager — The driving navigation management class. It provides methods for route planning, pre-trip route selection, and rerouting during navigation.
Note: Starting from Navigation SDK 5.4.0, the init method of AMapNaviDriveManager is deprecated. Use the singleton [AMapNaviDriveManager sharedInstance] instead. Call [AMapNaviDriveManager destroyInstance] in the class's dealloc method or at an appropriate time (e.g., when the navigation view controller is popped) to destroy the singleton. If the singleton is not destroyed successfully, check whether it is strongly referenced.
* <AMapNaviDriveManagerDelegate> — The driving navigation management delegate protocol. It provides callback interfaces for events during route calculation and navigation, such as route planning success/failure, TTS strings, weak GPS signal, and arrival at the destination.
* <AMapNaviDriveDataRepresentable> — The driving navigation data protocol. It provides callback interfaces for real-time data during route calculation and navigation, such as guidance information (NaviInfo), location data, and electronic eye information.
Notes
* Route planning requires a network connection.
* Origin and destination information can be obtained in several ways, such as using the coordinate picker to query coordinates, or using the POI search in the Search SDK to find points of interest.
* Starting from Navigation SDK 5.0.0, you can set up to 16 waypoints. Note that the navigation component supports a maximum of 3 waypoints.
* Four methods are available for driving route planning, as shown in the table below:
Method | Parameters | Return Value | Description |
calculateDriveRouteWithEndPoints:wayPoints:drivingStrategy: | endPoints: Destination coordinates; wayPoints: Waypoint coordinates; strategy: Route calculation strategy | BOOL — indicates whether the parameters are valid, not whether the route calculation succeeded | Driving/truck route planning with destination coordinates (uses the current location as the origin). |
calculateDriveRouteWithStartPoints:endPoints:wayPoints:drivingStrategy: | startPoints: Origin coordinates; endPoints: Destination coordinates; wayPoints: Waypoint coordinates; strategy: Route calculation strategy | BOOL — indicates whether the parameters are valid, not whether the route calculation succeeded | Driving/truck route planning with origin and destination coordinates. |
calculateDriveRouteWithStartPointPOIId:endPointPOIId:wayPointsPOIId:drivingStrategy: | startPOIId: Origin POI ID; endPOIId: Destination POI ID; wayPOIIds: Waypoint POI IDs; strategy: Route calculation strategy | BOOL — indicates whether the parameters are valid, not whether the route calculation succeeded | Driving/truck route planning based on AMAP POI IDs. For better accuracy, use this method when possible. |
calculateDriveRouteWithStartPOIInfo:endPOIInfo:wayPOIInfos:drivingStrategy: | startPOIInfo: Origin POI info; endPOIInfo: Destination POI info; wayPOIInfos: Waypoint POI info; strategy: Route calculation strategy | BOOL — indicates whether the parameters are valid, not whether the route calculation succeeded | Driving route planning based on AMAP POI info. For better accuracy, use this method when possible. |
* It is recommended to use the -calculateDriveRouteWithStartPOIInfo:endPOIInfo:wayPOIInfos:drivingStrategy: method for route planning. The SDK automatically completes additional information based on the POI ID or startPOIInfo.startAngle, resulting in a more reasonable route and reducing detours.
* Route calculation strategies include single-strategy and multi-strategy modes. Multi-strategy mode can calculate multiple routes. Use the naviRoutes property of AMapNaviDriveManager to retrieve these routes.
Route Planning with Origin Angle
The route planning method based on AMAP POI info supports passing an origin angle to prevent the route from starting on the wrong side of the road (e.g., mistaking the route's origin for the opposite side of the street).
AMapNaviPOIInfo (since 6.4.0)
@interface AMapNaviPOIInfo : NSObject <NSCopying>
/// POI ID
@property (nonatomic, copy) NSString *mid;
/// POI coordinate
@property (nonatomic, strong) AMapNaviPoint *locPoint;
/// Vehicle heading angle. Default is -1. 0 is north, increasing clockwise.
/// Note: This only takes effect when mid == nil && locPoint != nil && startAngle >= 0. (since 6.6.0)
@property (nonatomic, assign) double startAngle;
@end
Example
AMapNaviPOIInfo *startInfo = [[AMapNaviPOIInfo alloc] init];
startInfo.locPoint = [AMapNaviPoint locationWithLatitude:39.913578 longitude:116.473565];
startInfo.startAngle = 270;
AMapNaviPOIInfo *endInfo = [[AMapNaviPOIInfo alloc] init];
endInfo.locPoint = [AMapNaviPoint locationWithLatitude:40.095326 longitude:116.341895];
endInfo.mid = @"B000A8UIN8";
[[AMapNaviDriveManager sharedInstance] calculateDriveRouteWithStartPOIInfo:startInfo endPOIInfo:endInfo wayPOIInfos:nil drivingStrategy:AMapNaviDrivingStrategyMultipleDefault];
Route Calculation Strategies
The Navigation SDK provides 21 driving strategies, divided into two types: strategies that return a single route and strategies that return multiple routes. These are defined in the AMapNaviDrivingStrategy enum.
Strategy ID | Constant | Description |
0 | AMapNaviDrivingStrategySingleDefault | Speed priority. Ignores current traffic conditions. Returns the route with the shortest travel time, which may not be the shortest distance. |
1 | AMapNaviDrivingStrategySingleAvoidCost | Cost priority. Avoids toll roads and selects the route with the shortest travel time. |
2 | AMapNaviDrivingStrategySinglePrioritiseDistance | Distance priority. Ignores traffic conditions. Returns the shortest distance route, which may pass through small roads or residential areas. |
3 | AMapNaviDrivingStrategySingleAvoidExpressway | Speed priority. Avoids expressways (e.g., Jingtong Expressway). Due to strategy iteration, it is recommended to use strategy 13. |
4 | AMapNaviDrivingStrategySingleAvoidCongestion | Avoids congestion. May result in detours and longer travel time. |
5 | AMapNaviDrivingStrategySinglePrioritiseSpeedCostDistance | Multi-strategy. Simultaneously uses speed priority, cost priority, and distance priority. Depending on traffic conditions, 1 to 3 route plans may be returned. |
6 | AMapNaviDrivingStrategySingleAvoidHighway | Speed priority. Avoids highways but may still use other toll roads. |
7 | AMapNaviDrivingStrategySingleAvoidHighwayAndCost | Cost priority. Avoids highways and all toll roads. |
8 | AMapNaviDrivingStrategySingleAvoidCostAndCongestion | Avoids congestion and toll roads. May include highways. Considers traffic conditions to avoid congested routes, but may result in detours and longer travel time. |
9 | AMapNaviDrivingStrategySingleAvoidHighwayAndCostAndCongestion | Avoids congestion and toll roads. Does not use highways. |
10 | AMapNaviDrivingStrategyMultipleDefault | Avoids congestion, shortens distance, and minimizes travel time. Consistent with the default strategy of the AMap app (no checkboxes selected). |
11 | AMapNaviDrivingStrategyMultipleShortestTimeDistance | Returns three results: shortest time, shortest distance, and avoid congestion. Due to better algorithms, it is recommended to use strategy 10. |
12 | AMapNaviDrivingStrategyMultipleAvoidCongestion | Considers traffic conditions and avoids congestion. Consistent with the "Avoid Congestion" strategy of the AMap app. |
13 | AMapNaviDrivingStrategyMultipleAvoidHighway | Does not use highways. Consistent with the "Avoid Highways" strategy of the AMap app. |
14 | AMapNaviDrivingStrategyMultipleAvoidCost | Plans routes with low or no toll costs. Consistent with the "Avoid Tolls" strategy of the AMap app. |
15 | AMapNaviDrivingStrategyMultipleAvoidHighwayAndCongestion | Avoids congestion and does not use highways. Consistent with the "Avoid Congestion & Avoid Highways" strategy of the AMap app. |
16 | AMapNaviDrivingStrategyMultipleAvoidHighwayAndCost | Avoids highways and plans routes with low or no toll costs. Consistent with the "Avoid Tolls & Avoid Highways" strategy of the AMap app. |
17 | AMapNaviDrivingStrategyMultipleAvoidCostAndCongestion | Avoids congestion and plans routes with low or no toll costs. Consistent with the "Avoid Congestion & Avoid Tolls" strategy of the AMap app. |
18 | AMapNaviDrivingStrategyMultipleAvoidHighwayAndCostAndCongestion | Avoids congestion, plans routes with low or no toll costs, and avoids highways. Consistent with the "Avoid Congestion & Avoid Tolls & Avoid Highways" strategy of the AMap app. |
19 | AMapNaviDrivingStrategyMultiplePrioritiseHighway | Prioritizes highways. Consistent with the "Highway Priority" strategy of the AMap app. |
20 | AMapNaviDrivingStrategyMultiplePrioritiseHighwayAvoidCongestion | Prioritizes highways and avoids congestion. Consistent with the "Avoid Congestion & Highway Priority" strategy of the AMap app. |
You can use the strategies listed above for route calculation. To replicate the checkbox options of the AMap app, use the ConvertDrivingPreferenceToDrivingStrategy method to obtain the strategy value.
Method | Parameters | Return Value | Description | Notes |
ConvertDrivingPreferenceToDrivingStrategy | multipleRoute: Whether to use multi-route planning; avoidCongestion: Whether to avoid congestion; avoidHighway: Whether to avoid highways; avoidCost: Whether to avoid tolls; prioritiseHighway: Whether to prioritize highways | NSInteger | Converts the specified preferences into an AMapNaviDrivingStrategy enum value. | avoidHighway and prioritiseHighway cannot both be true. prioritiseHighway and avoidCost cannot both be true. |
Usage Guide
Step 1: Set Origin and Destination Information
// Initialize origin and destination using POI
self.startPOIInfo = [[AMapNaviPOIInfo alloc] init];
self.startPOIInfo.mid = @"B0FFFAB6J2";
self.endPOIInfo = [[AMapNaviPOIInfo alloc] init];
self.endPOIInfo.mid = @"B0FFIA9OE8";
Step 2: Calculate the Route
// Calculate the route using origin and destination POI
[[AMapNaviDriveManager sharedInstance] calculateDriveRouteWithStartPOIInfo:self.startPOIInfo endPOIInfo:self.endPOIInfo wayPOIInfos:nil drivingStrategy:AMapNaviDrivingStrategySingleDefault];
Step 3: Handle the Result
When the route calculation succeeds, the -driveManager:onCalculateRouteSuccessWithType: callback is triggered. In this callback, you can display the planned route or start navigation directly.
- (void)driveManager:(AMapNaviDriveManager *)driveManager onCalculateRouteSuccessWithType:(AMapNaviRoutePlanType)type
{
NSLog(@"onCalculateRouteSuccess");
// Display the route or start navigation
}