Driving Routing
This guide describes how to plan driving routes using the Navigation SDK. You can generate route plans based on a start point, destination, waypoints, and route calculation strategies.
Before You Begin
Before you start, familiarize yourself with the following core classes:
* AMapNavi: The navigation management class. It provides methods for route planning, pre-trip route selection, and rerouting during navigation. This class is a singleton. Obtain the instance using getInstance() and destroy it using destroy().
* AMapNaviListener: The protocol for navigation events and data. It provides callbacks for events during route calculation and navigation (e.g., route calculation success/failure, TTS strings, weak GPS signal, arrival at destination) and real-time data (e.g., guidance information NaviInfo, location information, electronic eye information).
Important Notes
* Route planning requires a network connection.
* Start and end points 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.
* As of Navigation SDK V5.0.0, you can set up to 16 waypoints. Note: The navigation component supports a maximum of 3 waypoints.
* Route calculation strategies include single-strategy and multi-strategy modes. With multi-strategy mode, the SDK can calculate multiple routes. Use the AMapNavi.getNaviPaths() method to retrieve these routes.
Step 1: Choose a Route Calculation Strategy
The Navigation SDK provides 21 driving strategies, which are divided into two types: strategies that return a single route and strategies that return multiple routes. These are defined in the PathPlanningStrategy enum.
Strategy Reference
Using the Strategy Converter
You can use the strategy IDs from the table above directly. To replicate the checkbox-based strategy selection in the AMAP app, use the strategyConvert method in AMapNavi.
Step 2: Calculate a Route
You can calculate a driving route using coordinates, POI information, or a start angle.
Calculate a Route by Coordinates
Pass the latitude and longitude of the start and end points.
// Get the navigation manager
AMapNavi mAMapNavi = AMapNavi.getInstance(this);
// Start point
List<NaviLatLng> startList = new ArrayList<NaviLatLng>();
startList.add(new NaviLatLng(39.993308, 116.473199));
// End point
List<NaviLatLng> endList = new ArrayList<NaviLatLng>();
endList.add(new NaviLatLng(39.917834, 116.397036));
// Calculate the route
mAMapNavi.calculateDriveRoute(startList, endList, null, PathPlanningStrategy.DRIVING_MULTIPLE_ROUTES_DEFAULT);Calculate a Route by POI
Pass the POI information of the start and end points. You can obtain POI information using the POI search service provided by the AMAP Platform.
AMapNavi mAMapNavi = AMapNavi.getInstance(this);
// Start point
NaviPoi start = new NaviPoi("Longcheng Garden", null, "B000A8UF3J");
// End point
NaviPoi end = new NaviPoi("Peking University", null, "B000A816R6");
// Calculate the route
mAMapNavi.calculateDriveRoute(start, end, null, PathPlanningStrategy.DRIVING_MULTIPLE_ROUTES_DEFAULT);Note: The NaviPoi object can also accept latitude and longitude coordinates. The POI ID takes priority. If the POI ID is invalid, the SDK uses the coordinates for route calculation.
Calculate a Route with a Start Angle
When using POI-based route calculation, you can specify a custom start angle for the start point via the NaviPoi object. This allows the SDK to plan a route that is appropriate for the given departure direction.
Important: To use a custom start angle, do not provide a poiId. You must provide latitude and longitude coordinates and a valid angle value.
AMapNavi mAMapNavi = AMapNavi.getInstance(this);
// Start point
NaviPoi start = new NaviPoi("Longcheng Garden", new LatLng(39.993308, 116.473199), "");
// Set the start angle
start.setDirection(35);
// End point
NaviPoi end = new NaviPoi("Peking University", new LatLng(39.917834, 116.397036), "");
// Calculate the route
mAMapNavi.calculateDriveRoute(start, end, null, PathPlanningStrategy.DRIVING_MULTIPLE_ROUTES_DEFAULT);Calculate a Route with Waypoints
As of Navigation SDK V5.0.0, you can set up to 16 waypoints.
AMapNavi mAMapNavi = AMapNavi.getInstance(this);
// Start point
NaviPoi start = new NaviPoi("Longcheng Garden", null, "B000A8UF3J");
// End point
NaviPoi end = new NaviPoi("Peking University", null, "B000A816R6");
// Waypoints
List<NaviPoi> waysPoiIds = new ArrayList<NaviPoi>();
waysPoiIds.add(new NaviPoi("Waypoint 1", null, "B000A805M7"));
waysPoiIds.add(new NaviPoi("Waypoint 2", null, "B0FFFAADBU"));
waysPoiIds.add(new NaviPoi("Waypoint 3", null, "B0FFF5BER7"));
// Calculate the route
mAMapNavi.calculateDriveRoute(start, end, waysPoiIds, PathPlanningStrategy.DRIVING_MULTIPLE_ROUTES_DEFAULT);Step 3: Handle the Result
On Success
When route calculation is successful, the AMapNaviListener.onCalculateRouteSuccess callback is triggered. In this callback, you can retrieve the route objects and display them on the map.
@Override
public void onCalculateRouteSuccess(AMapCalcRouteResult routeResult) {
// Get the route data objects
HashMap<Integer, AMapNaviPath> naviPaths = AMapNavi.getInstance(this).getNaviPaths();
// Draw and display the routes
// ...
}You can also start navigation directly:
@Override
public void onCalculateRouteSuccess(AMapCalcRouteResult routeResult) {
// Start navigation
AMapNavi.getInstance(this).startNavi(NaviType.GPS);
}On Failure
If route calculation fails, the AMapNaviListener.onCalculateRouteFailure callback is triggered. Implement your error handling logic in this callback.