import { NavigationManager } from "@/path-to/navigation-manager";

NavigationManager

The NavigationManager class is responsible for handling real-time navigation, including location tracking, heading updates, route progress, and arrival detection.

Installation

Ensure you have expo-location installed:

sh

yarn add expo-location

or

sh

npm install expo-location

Usage

tsx

import { NavigationManager } from "@/path-to/navigation-manager"; const navigationManager = new NavigationManager();

Constructor

new NavigationManager()

Creates an instance of NavigationManager and initializes location tracking.

Methods

initializeRoute(destination: Destination)

Initializes a new route to the specified destination.

Parameters:

  • destination: { latitude: number, longitude: number } - The target location.

handleRouteReady(result: any)

Processes the received route data and extracts navigation steps.

Parameters:

  • result: any - The route data from the navigation API.

endNavigation()

Stops the navigation and clears the route information.

setCallbacks(callbacks: Callbacks)

Registers callback functions for location updates, heading updates, route changes, arrival detection, and more.

Parameters:

  • callbacks: { onLocationUpdate?: Function, onHeadingUpdate?: Function, ... }

cleanup()

Removes all active location and heading subscriptions.

getCurrentStep(): number

Returns the current step index of the navigation route.

getRouteInfo(): RouteInfo | null

Returns the active route information.

isNearDestinationStatus(): boolean

Indicates whether the user is near the destination.

Events (Callbacks)

onLocationUpdate(location: LocationObject)

Triggered when the user's location changes.

onHeadingUpdate(heading: number)

Triggered when the user's heading changes.

onRouteUpdate(routeInfo: RouteInfo | null, stepIndex?: number)

Triggered when the route updates or a new step is reached.

onArrival(distance: string, duration: string)

Triggered when the user arrives at the destination.

onNearDestinationChange(isNear: boolean)

Triggered when the user is within 3 miles of the destination.

onMapRotationUpdate(rotation: number)

Triggered when the map should rotate based on the user's direction.

Internal Utilities

calculateDistance(point1, point2): number

Calculates the distance between two geographical points in meters.

calculateBearing(start, end): number

Computes the bearing angle between two geographical points.

Example

tsx

const navigationManager = new NavigationManager(); navigationManager.setCallbacks({ onLocationUpdate: (location) => console.log("New location:", location), onRouteUpdate: (routeInfo) => console.log("Route updated:", routeInfo), onArrival: (distance, duration) => console.log("Arrived!", distance, duration), });

License

MIT