Finger Route
This example demonstrates how to draw a route by tapping or clicking on the map to place waypoints, then calculating and displaying a route through those points using the Maps SDK for TypeScript.
Live Demo
Overview
The example demonstrates the following features:
- Enabling draw markers mode to collect waypoints
- Drawing routes by tapping/clicking on the map
- Calculating routes through multiple waypoints
- Displaying route information (distance and duration)
- Managing route calculation state
Code Implementation
Initialize GemKit and Setup
Enable Draw Markers Mode
Build Route from Waypoints
Clear and Cancel Operations
UI Management
Utility Functions
Key Features
- Draw Markers Mode: Interactive waypoint placement by tapping/clicking on the map
- Multi-Waypoint Routes: Calculate routes through multiple user-defined points
- Route Preferences: Customizable routing options for finger-drawn routes
- State Management: Clear UI state transitions between drawing, calculating, and viewing routes
- Cancellation Support: Cancel ongoing route calculations
Route Preferences for Finger Routes
When calculating routes from user-drawn waypoints, specific preferences are used:
const routePreferences = new RoutePreferences({
accurateTrackMatch: false, // Don't require precise track matching
ignoreRestrictionsOverTrack: true // Ignore road restrictions
});
These settings are important for finger-drawn routes because:
- Users may not place waypoints precisely on roads
- The route should follow the general path indicated by waypoints
- Road restrictions may prevent routing through some waypoints
Workflow
- Start Drawing: Click "Draw Route" to enable waypoint placement
- Add Waypoints: Tap/click on the map to add waypoints along your desired route
- Build Route: Click "Build Route" to calculate a route through all waypoints
- View Results: The route is displayed with distance and duration labels
- Clear/Restart: Clear routes to start over or cancel during calculation
UI States
The example manages four distinct UI states:
- Initial: Show "Draw Route" button
- Drawing: Show "Build Route" button (while placing waypoints)
- Calculating: Show "Cancel" button (during route calculation)
- Complete: Show "Clear Routes" button (when routes are displayed)
Tips for Best Results
- Place waypoints on or near roads for better routing
- Use at least 2 waypoints (start and end points)
- More waypoints give you more control over the route path
- Zoom in for more precise waypoint placement
- Routes are calculated to follow roads between waypoints
Next Steps
- Calculate Route - Basic route calculation
- Route Instructions - Get turn-by-turn directions
- Navigate Route - Start navigation along a route