Calculate Route
This example demonstrates how to create a web application that calculates a route between two locations and displays it on a map using Maps SDK for TypeScript.
How it works
The example app demonstrates the following features:
- Calculate a route between two landmarks (Paris to Brussels).
- Display the route on a map and allow user interaction with the route.
- Provide options to cancel route calculation or clear the routes from the map.
- Select alternative routes by clicking on them.
Live Demo
Try the interactive demo below. Click "Build Route" to calculate a route between Paris and Brussels:
Code Implementation
Route Calculation and Map Integration
This code handles the calculation of routes between two landmarks, displays the routes on the map, and provides options to cancel or clear the routes. The map is centered on the calculated routes, and a label showing the distance and duration is displayed.
Clear and Cancel Functionality
Handle clearing routes from the map and canceling ongoing calculations:
Route Selection
Enable the user to select a specific route on the map by clicking on it. The selected route becomes the main route displayed:
Displaying Route Information
Utility functions to format and display route information:
Key Features
- Route Calculation: Calculates the best route between two points
- Visual Feedback: Routes are displayed on the map with distance and time labels
- Interactive Selection: Click on alternative routes to make them the main route
- Cancellation: Cancel route calculation while it's in progress
- Clean UI: Clear all routes from the map with one click
Next Steps
- Try the Route Profile example to see different route preferences
- Explore Navigate Route for turn-by-turn navigation
- Check out Route Instructions to display detailed directions