Skip to main content

Quick navigation

Contents

The guide takes your through the basic steps needed to create a route and begin navigation.

Before getting started, make sure your project is set up correctly, permissions are granted, and authentication is completed.

Step 1: Set the layout

In this tutorial, we are going to create a dedicated activity for the Navigation UI. As part of this, a NavigationView view will be added to the layout for rendering and visual purposes. We will just name this activity NavigationSampleActivity and the layout file ‘activity_navigation_sample’.

Inside of the layout file, add the following view to the xml.

<com.trimblemaps.navigation.ui.NavigationView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/navigation_view" />

This view is where the navigation UI and components will be attached and rendered.

Step 2: Initialize the NavigationView and some Stops

The NavigationView effectively brings the navigation experience and UI to the screen, attaching to the view we made above. In this example, it’s being set up in the activity’s OnCreate method.

First, add an instance of NavigationView as a class instance variable.

In preparation for the route calculation, create two stops—your origin and destination. (Typically, you would set the user’s current location as the first stop.)

Besides adding the coordinates of the stops manually, you can retrieve the coordinates (latitude and longitude) of a location using geocoding. For more information on how to do this, refer to the Geocoding Example.

Next, it’s time to find the NavigationView from the layout file and do some initialization.

If you use the code above, you will likely see an error with passing in this as the first parameter for navigationView.initialize(). In Step 3 of this tutorial, we will implement the onNavigationReadyCallback for this Activity. For the final step of this section, we will use the Point objects we created before to plan our trip.

At this point, the NavigationView has been set up and you’ve planned your trip. Your code should look something like this:

Next, we will need to implement some methods.

Step 3: Implementing onNavigationReadyCallback and NavigationListener

In the previous section, you will have likely received an error in your code when trying to pass this into navigationView.initialize(). To resolve this error, you’ll need to implement the OnNavigationReadyCallback in your activity. In this section, we also want to implement the NavigationListener for the activity. This will be used later.

Typically an IDE like Android Studio will help you import the methods you need into your Activity to meet the implemented classes requirements. You can also add the below yourself:

This is also a good place to implement the Android activity lifecycle methods to ensure the navigation adheres to Android’s lifecycle. See below:

Step 4: Navigate

With the methods implemented, it’s time to actually start navigation. This can be done from within the onNavigationReady method that was implemented earlier.

The code above creates the options we’ll be passing into the navigation. Most importantly, the trip we created earlier with the provider is retrieved and passed in. We also set the NavigationListener to this, which were the other methods implemented previously. Finally, navigation starts.

Before running the code in Simulator, make sure to setup the theme as below in values/themes.xml.

<resources>
   <style name="Theme.Example" parent="Theme.AppCompat" />
</resources>
Quick Navigation
At this point, we now have an app with embedded navigation taking the user from A to B.

Step 5: Finishing touches

The user is now navigating to their destination, but there’s one other small piece that can be added—controlling what happens when navigation is canceled.

Should the user press the end navigation button End Navigation, this will trigger the onCancelNavigation() method that was implemented earlier. Here, code can be added to stop navigation and kill the current activity.

Full code for all steps

Last updated March 28, 2025.
Contents