Android NavKit API User Guide
Navigation Class
Navigation class is the main class of NavKit and acts as the controller of navigation. It creates and controls the life cycle of a navigation session and other major navigation related operations, such as pause/stop, set active route, play announcement, and so on.
The following table describes the major functions and commonly used methods in the Navigation class.
Method | Description |
---|---|
|
Updates the navigation session with the location. The first position update triggers a route generation |
|
Starts a plan route from the origin location |
|
Pauses the current navigation session |
|
Resumes the current navigation session |
|
Stops the current navigation session |
|
Selects/activates the route for the current navigation session |
|
Triggers the announcement for the current step within the current session. It has overloaded parameters for the specific maneuver along with the route |
|
Recalculates the current route session |
|
Calculates a detour route from the current position |
|
Cancels the detour request |
Navigation Event Listeners
The four sets of navigation events in the Android NavKit are described in the following table.
Navigation Event | Description |
---|---|
Session Events |
Notifies the subscriber about route states like, new route received, route generation progress, end of route, and so on |
Navigation Events |
Notifies the subscriber about user position updates on the route like, maneuver remaining distance, trip remaining distance, current road name, and so on |
Announcement Events |
Notifies the subscriber about announcements which should be played based on the current user position |
Traffic Events |
Notifies the subscriber about the traffic alerts and traffic changes during navigation |
The listeners should be registered to the navigation session in order to receive any of the event notifications. The following table describes all the event listeners.
Navigation Event Listener | Description |
---|---|
|
Receives notifications when an announcement needs to be played |
|
Receives lane guidance enabled/disabled notifications |
|
Receives notifications associated with a maneuver change |
|
Receives notifications associated with the navigation session, which are not directly linked to the position or maneuver updates |
|
Receives notifications on position changes with respect to the entire route and the current maneuver |
|
Receives notifications that are generated whenever the position changes |
|
Receives road sign enabled/disabled notifications |
|
Receives session related notifications |
|
Receives speed limit enabled/disabled notifications |
|
Receives traffic notifications |
Note
|
All events are sent within the main Android User Interface (UI) thread, which allows you to make instantaneous UI updates. |
SessionListener
SessionListener is the basic listener for a Navigation session and provides navigation session related notifications like route updates, errors and so on. The following code snippet registers the SessionListener to Navigation.
navigation.addSessionListener(new SessionListener() {
// SessionListener interface implementation
...
}
The following table describes the SessionListener events.
Event | Description |
---|---|
|
Informs the subscriber that the locations being received by the navigation session are not considered on route to the destination |
|
Informs the subscriber that the locations being received by the navigation session are considered on route to the destination |
|
Provides the list of possible routes for the current navigation session. When navigation succeeds, it generates a callback to get the route information |
|
Informs the subscriber that new route is requested with a reason. When requesting navigation, it generates a callback |
|
Provides the route generation progress |
|
Indicates that an error occurred and that the navigation session is no longer valid |
|
Informs the subscriber that the current navigation session has ended. On reaching the destination, a callback gets generated |
|
Informs the subscriber that route is going to be updated by recalculation |
NavigationUpdateListener
NavigationUpdateListener provides navigation related notifications like current road name, remaining trip distance and so on. It is triggered only after navigation route activation. The following code snippet registers the NavigationUpdateListener to Navigation.
navigation.addNavigationUpdateListener(new NavigationUpdateListener() {
// NavigationUpdateListener interface implementation
...
}
The following table describes the NavigationUpdateListener events.
Event | Description |
---|---|
|
Provides updates to the current road name |
|
Provides updates to the current maneuver exit number |
|
Provides maneuver image ID |
|
Provides turn point coordinates |
|
Provides updates to the remaining maneuver delay, in seconds |
|
Provides updates to the remaining distance to next maneuver in meters |
|
Provides updates to the remaining maneuver time in seconds |
|
Provides updates to the current maneuver type |
|
Provides updates to the next road name |
|
Notifies that the position on the route has been changed |
|
Provides stack turn image text in True Type Font (TTF) format |
|
Provides updates to the remaining trip delay. Using this method, the current remaining delay of navigation can be known |
|
Provides updates to the remaining trip distance. Using this method, the current remaining distance of navigation can be obtained |
|
Provides updates to the remaining trip time. Using this method, the current remaining navigation time can be obtained |
|
Notifies when maneuver list gets updated. Using this method, the remaining maneuver list can be obtained |
AnnouncementListener
This listener provides the navigation announcement notifications. The following code snippet registers the AnnouncementListener to Navigation.
navigation.addAnnouncementListener(new AnnouncementListener() {
// AnnouncementListener interface implementation
...
}
It has a single event which is described as follows.
void announce(AnnouncementInformation announcement)
- Informs the subscriber that an announcement needs to be played.
RouteInformation Class
The RouteInformation class provides details of the received route after route calculation, which is obtained through the SessionListener.routeReceived() as shown in the following code snippet.
@Override
public void routeReceived(int reason, RouteInformation[] routeInfos) {
// List of routes as RouteInformation array
RouteInformation routeInformation = routeInfos[0];
}
RouteInformation class has the methods to get information about the origin, destination, distance, time, and so on, described in the following table.
Method | Description |
---|---|
|
Gets route origin place |
|
Gets route destination place |
|
Gets total route time in seconds |
|
Gets total route distance in meters |
|
Gets total route delay time in seconds |
|
Gets the active route’s remaining time in seconds |
|
Gets the active route’s remaining distance in meters |
|
Gets maneuver list |
|
Returns the coordinates on route polyline for the entire route |
|
Gets description of route traffic segments |
|
Returns the overall traffic color based on the delay |
|
Returns traffic events |
|
Gets route error code |
|
Returns the data corresponding to the given voice name |
ManeuverList Class
The ManeuverList class returns a list of navigation maneuvers. It is received through getManeuverList method of the RouteInformation object as shown in the following code snippet.
@Override
public void routeReceived(int reason, RouteInformation[] routeInfos) {
ManeuverList maneuverList = routeInfos[0].getManeuverList();
}
The following table describes the methods available in ManeuverList class.
Method | Description |
---|---|
|
Get maneuver origin |
|
Gets maneuver destination place |
|
Returns the total trip distance |
|
Returns total actual time for maneuvers list |
|
Returns number of maneuvers |
|
Returns maneuver by the given index |
|
Returns total delay time for maneuvers list |
|
Returns BoundingBox that contains all of the maneuvers |
Maneuver Class
The Maneuver class represents the information of an individual navigation maneuver. It is received through getManeuver method of the ManeuverList object as shown in the following code snippet.
ManeuverList maneuverList = routeInformations[0].getManeuverList();
Maneuver maneuver = maneuverList.getManeuver(0);
The following table describes the methods which obtain maneuver details.
Method | Description |
---|---|
|
Gets point coordinates |
|
Returns the coordinates on route polyline for this maneuver |
|
Returns primary street name of current road |
|
Returns secondary street name of current road |
|
Returns primary street |
|
Returns secondary description |
|
Returns distance |
|
Returns speed |
|
Returns the maneuver actual time |
|
Returns maneuver description |
|
Gets the command |
|
Gets ID of routing icon to use for the maneuver |
|
Returns the maneuver delay time |
|
Returns traffic events |
Supported Maneuvers
The following table describes all the supported maneuvers in Android NavKit SDK.
Command | Description | Usage | LG Font | NG Font | Example |
---|---|---|---|---|---|
DT.L , DT.R |
Destination |
Destination |
Y |
N |
Your destination is on the left/right |
ER.L , ER.R |
Take ramp |
Highway |
N |
N |
Take ramp on the left |
EX.L , EX.R |
Exit highway |
Highway |
Y |
N |
Take exit 13A on the left |
KH.L , KH.R |
Keep (highway version) |
Highway |
Y |
N |
In 1 ½ miles take I-405 |
KS.L , KS.R |
Keep (normal road version) |
Normal |
Y |
Y |
Get ready to keep to the right at the fork and take Main street |
MR.L , MR.R |
Merge |
Highway |
Y |
N |
Merge {highway} on the left |
SH.L , SH.R |
Stay (highway version) |
Highway |
Y |
Y |
In 2 ½ miles continue on I-5 |
BE.L , BE.R |
Cross bridge |
Normal |
Y |
N |
— |
EC.L , EC.R |
Enter country |
Normal |
Y |
N |
In 5 miles you will enter Canada |
EN.L , EN.R |
Enter highway |
Normal |
Y |
Y |
Get ready to take the ramp straight ahead to I-5 |
FE. |
Enter ferry |
Normal |
Y |
N |
Get ready to take the Avalon ferry |
FX. |
Exit ferry |
Normal |
N |
N |
Exit the ferry |
KP.L , KP.R |
Keep (current version which will be deprecated) |
Normal |
Y |
Y |
Get ready to keep right onto Main street |
NC. |
Name change |
Normal |
Y |
N |
Not used in announcements |
NR.L , NR.R |
Enter private road |
Normal |
Y |
Y |
Enter private on the left |
OR. |
Origin (startup case) |
Normal |
Y |
N |
Not used in announcements |
PE. |
Continue by foot |
Normal |
Y |
N |
Get ready to find parking then continue by foot to Main street |
RE. |
Enter roundabout |
Normal |
Y |
N |
Get ready to take the 4th exit at the roundabout onto Main street |
RT. |
Continue straight through roundabout |
Normal |
Y |
N |
In 1 mile continue straight through the roundabout onto Main street |
RX.N |
Exit traffic circle, where N represents the exit number |
Normal |
Y |
N |
Not used in announcements |
ST.L , ST.R |
Stay (normal version) |
Normal |
Y |
Y |
In 1 ¼ miles stay on Main street |
TE.L , TE.R |
Enter tunnel |
Normal |
N |
N |
— |
TR.L , TR.R , TR.SL , TR.SR , TR.HL , TR.HR |
Turn, slight turn, hard turn |
Normal |
Y |
Y |
Now turn right |
UT. |
U-turn |
Normal |
Y |
N |
Make the next legal U-turn |
SC.S |
Speed camera |
Other |
N |
N |
— |
TC.S |
Traffic congestion |
Other |
N |
N |
— |
TL.S |
Traffic incident |
Other |
N |
N |
— |
EE.L , EE.R |
Take escalator |
Pedestrian |
Y |
N |
Take escalator on the left |
ES.L , ES.R |
Take stairs |
Pedestrian |
Y |
N |
Take stairs on the left |
AnnouncementInformation
AnnouncementInformation class provides audio data to be played. It is comprised of both audio files and audio text to be fed to the Text to Speech (TTS) engine. The following code snippet shows a sample method to play an announcement using Android TTS.
private TextToSpeech tts;
private void playAnnouncement(final AnnouncementInformation announcement) {
tts = new TextToSpeech(this, new TextToSpeech.OnInitListener() {
@Override
public void onInit(int status) {
if (status == TextToSpeech.SUCCESS) {
tts.speak(announcement.getText(),
TextToSpeech.QUEUE_ADD, null);
}
}
});
tts.setLanguage(new Locale(ltkContext.getLanguage(),
ltkContext.getCountryCode()));
}
Selecting the Route
Multiple routes are returned along with the primary route if alternative route has been enabled by setting setMultipleRoutes(true) in the navigation Preference class, through the SessionListener.routeReceived(). After receiving the routes, you can activate a single route from the available routes.
The following code snippet activates the first route as the current navigation route, from the available routes.
@Override
public void routeReceived(int reason, RouteInformation[] routeInfos) {
if (routeInfos != null && routeInfos.length > 0) {
navigation.setActiveRoute(routeInfos[0]);
}
}
Off-Route Processing
A Navigation session is considered “off route” when the location being received is not considered to be “on route” to the destination. In this situation, the SessionListener.offroute() event is sent to the subscriber and NavKit starts route recalculation automatically. SessionListener.routeReceived() is triggered once a new route is received.
navigation.addSessionListener(new SessionListener() {
@Override
public void offroute() {
// Navigation is offroute, show off-route notification to user
}
@Override
public void routeReceived(int reason, RouteInformation[] routeInformations) {
// New routes received
}
// Other SessionListener method implementations
...
}
Error Codes
NavKit will display different error codes if there is an occurrence of an error during navigation session initiation or progress.
LTKException
The error codes are thrown through SessionListener.routeError() as shown in the following code snippet.
public void routeError(LTKException exception) {
if (exception != null) {
int errorCode = exception.getErrorCode();
}
}
The following table describes the navigation related LTKException codes.
Error | Description |
---|---|
|
The request failed for an unknown reason |
|
An error response was received from the server |
|
No response was received from the server within the allotted time |
|
An unknown low-level error was generated by the app |
|
An error occurred trying to connect to the server |
|
Failed to write to a network socket |
|
Failed to read from a network socket |
|
Unable to load template library |
|
Malformed packet |
|
Malformed Template Packet System (TPS) document |
|
Internal server error |
|
Requested feature is unsupported |
|
Unsupported vehicle type |
|
Unsupported avoid element |
|
Database error |
|
User database error |
|
Unable to process request |
|
Too busy to process request |
|
Unable to contact external server |
|
Value specified in request is not valid |
|
Latitude or longitude is out of range |
|
The maximum daily queries limit for this API Key has been reached |
|
The specified API Key is not valid |
|
The maximum number of users for this API Key has been reached |
|
Feature not available in this country |
|
Feature not supported |
RouteErrorCode
Navigation could be returned successful in SessionListener.routeReceived(), but the route returned or the maneuver list could be null. In such scenarios, the route errors can be retrieved from the RouteInformation object as shown in the following code snippet.
public void routeReceived(int reason, RouteInformation[] routes) {
if (routes[0].getManeuverList() == null ||
routes[0].getManeuverList().getNumberOfManeuvers() == 0) {
if(routes[0].getRouteError() == RouteErrorCode.ROUTE_NONE){
// Show No Route
}
}
}
The following table describes the available route errors.
Error | Description |
---|---|
|
No error/No route |
|
Route timed out error |
|
Routing destination is not near a routable road |
|
Origin is not near a routable road |
|
No route can be found between the origin and the destination |
|
Empty route code |
|
Network error |
|
Unknown error |
|
Route does not match |
|
Server error |
|
No route detour |
|
Route too long error |
|
No pedestrian route detour |
|
Origin not in supported countries |
|
Destination not in supported countries |
NavKit_SampleApp Demonstration
Comtech provides an example application NavKit_SampleApp, that demonstrates various features of the Android NavKit SDK. The following sections describe how to build and run the NavKit_SampleApp.
Importing the Project
The following steps are performed to import the NavKit_SampleApp project.
-
Click Open existing Android Studio project from Studio Welcome Screen. If another project is already open in Studio, from the File menu, select Open.
-
Browse to the path androidSDK/sample/NavKit_SampleApp, select the option NavKit_SampleApp from the dropdown and click OK as shown in the following screenshot.
-
Click OK in the Gradle Sync dialog to auto configure the Gradle wrapper for the project.
-
After Gradle sync is completed, NavKit_SampleApp and the libraries can be seen in the project explorer, as shown in the following screenshot.
Building and Running the NavKit_SampleApp
The following steps describe building and running the NavKit_SampleApp.
-
Before building the project, set the value for the API key.
-
Open the path com.locationtoolkit.navkit.sampleapp.common.Credential as shown in the following screenshot. Replace your_server_token with the API key assigned to you by Comtech.
-
Run the NavKit_SampleApp to explore various navigation features. The following screenshot shows the main screen which has various route options.
-
Tapping Navigate on the main screen starts a Navigation session with all the selected options. The following screenshot shows a running navigation session.
Note
|
To know more about all the interfaces and methods of Android NavKit, see the NavKit Android API Reference Document. |