# Launch NavigationView

A complete turn-by-turn experience using the default NavigationLauncher

This example shows how to launch Navigation using `NavigationLauncher`

-   How to fetch a route using `NBNavigation.fetchRoute` with origin and destination

-   How to config `NavLauncherConfig` and launch Navigation using `NavigationLauncher` with the given route


![documentation image](https://static.nextbillion.io/docs-next/docs/navigation/android/launch-navigation.jpg)

For all code examples, refer to [Navigation Code Examples](https://github.com/nextbillion-ai/nb-navigation-android-demo)

**activity_main.xml [view source](https://github.com/nextbillion-ai/nb-navigation-android-demo/blob/main/app/src/main/res/layout/activity_main.xml)**

```xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="10dp">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <Button
            android:id="@+id/fetchRoute"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/fetch_route"/>

        <Button
            android:id="@+id/startNav"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="15dp"
            android:text="@string/start_navigation"/>
    </LinearLayout>

    <TextView
        android:id="@+id/routeGeometry"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"/>

    <ProgressBar
        android:id="@+id/progress"
        android:visibility="gone"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"/>

</LinearLayout>
```

**NavigationActivity view source**

```java
package ai.nextbillion.navigation.demo.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;

import ai.nextbillion.kits.directions.models.DirectionsResponse;
import ai.nextbillion.kits.directions.models.DirectionsRoute;
import ai.nextbillion.kits.geojson.Point;
import ai.nextbillion.maps.location.modes.RenderMode;
import ai.nextbillion.navigation.demo.R;
import ai.nextbillion.navigation.ui.NBNavigation;
import ai.nextbillion.navigation.ui.NavLauncherConfig;
import ai.nextbillion.navigation.ui.NavigationLauncher;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class NavigationActivity extends AppCompatActivity implements View.OnClickListener {

    private Button fetchRoute;
    private Button startNav;
    private TextView routeGeometry;
    private DirectionsRoute directionsRoute;
    private ProgressBar progress;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        fetchRoute = findViewById(R.id.fetchRoute);
        startNav = findViewById(R.id.startNav);
        routeGeometry = findViewById(R.id.routeGeometry);
        progress = findViewById(R.id.progress);
        fetchRoute.setOnClickListener(this);
        startNav.setOnClickListener(this);
        startNav.setEnabled(false);

    }

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.fetchRoute) {
            progress.setVisibility(View.VISIBLE);
            Point origin = Point.fromLngLat(103.75986708439264, 1.312533169133601);
            Point destination = Point.fromLngLat(103.77982271935586, 1.310473772283314);

            NBNavigation.fetchRoute(origin, destination, new Callback<DirectionsResponse>() {
                @Override
                public void onResponse(@NonNull Call<DirectionsResponse> call, @NonNull Response<DirectionsResponse> response) {
                    progress.setVisibility(View.GONE);
                    //start navigation with the route we just fetched.
                    if (response.body() != null && !response.body().routes().isEmpty()) {
                        directionsRoute = response.body().routes().get(0);
                        routeGeometry.setText(String.format("Route Geometry: %s", directionsRoute.geometry()));
                        startNav.setEnabled(true);
                    }
                }

                @Override
                public void onFailure(@NonNull Call<DirectionsResponse> call, @NonNull Throwable t) {
                    progress.setVisibility(View.GONE);
                }
            });
        } else if (view.getId() == R.id.startNav) {
            NavLauncherConfig.Builder configBuilder = NavLauncherConfig.builder(directionsRoute);
            configBuilder.locationLayerRenderMode(RenderMode.GPS);
            configBuilder.shouldSimulateRoute(true);
            NavigationLauncher.startNavigation(NavigationActivity.this, configBuilder.build());
        }
    }
}
```

### Code Highlights

The example code represents a **NavigationActivity** class that allows users to fetch a route and start navigation using the **NavigationLauncher** class provided by the navigation SDK. Code Explanation The code example defines a layout file `activity_main.xml` that contains a vertical `LinearLayout` with two Button views (`fetchRoute` and `startNav`), a `TextView` (`routeGeometry`), and a `ProgressBar` (`progress`).

The associated Java class `NavigationActivity` extends `AppCompatActivity` and implements the `OnClickListener` interface. In the `onCreate` method, the layout elements are initialized and click listeners are set for the buttons.

When the `fetchRoute` button is clicked, a network request is made using the `NBNavigation` class to fetch a route between two geographic points (origin and destination). The response is received asynchronously in the `onResponse` callback. If the response is successful and contains at least one route, the first route is stored in the `directionsRoute` variable, and its geometry is displayed in the `routeGeometry` text view. The `startNav` button is enabled to allow navigation to be started.

When the `startNav` button is clicked, a `NavLauncherConfig` is created with the stored `directionsRoute` and some configuration options. The navigation is then started using the `NavigationLauncher.startNavigation` method.

### Code summary

-   Fetch a route

    -   Using the `NBNavigation.fetchRoute` method to retrieve the directions response for the given **origin** and **destination** points. If the response is successful and it will contain at least one route
-   Start navigation

    -   `NavLauncherConfig` object is created with the previously fetched route and additional configuration options such as location layer render mode and route simulation. The `NavigationLauncher.startNavigation` method is called to start the navigation activity with the provided configuration.
