Multi Map Routing
This example demonstrates how to create and manage multiple map views simultaneously, calculating and displaying different routes on each map using the Maps SDK for TypeScript.
Live Demo
Overview
The example showcases the following capabilities:
- Creating multiple independent map instances in a single application
- Managing separate routing calculations for each map
- Displaying different routes on different map views
- Synchronizing UI controls across multiple maps
- Handling multiple concurrent routing operations
Code Implementation
Initialize GemKit and Create Multiple Maps
Setup User Interface
Calculate Routes for Each Map
Handle Route Calculation Results
Route Label Helper
Remove Routes from All Maps
Display Messages
Key Features
- Multiple Map Instances: Create and manage two independent map views simultaneously
- Independent Routing: Calculate separate routes for each map
- Unique View IDs: Each map has its own view ID (7 and 8 in this example)
- Concurrent Operations: Handle multiple routing calculations in parallel
- Unified Controls: Single UI controls manage both maps
- Route Cancellation: Cancel ongoing route calculations when needed
- Route Labels: Display distance and time information for each route
Map Instance Management
Each map requires:
-
Unique View ID: Used to create the map instance
gemKit.createView(7, onMap1Created) // First map
gemKit.createView(8, onMap2Created) // Second map -
Separate Container: Each map needs its own DOM element
<div id="map1-container"></div>
<div id="map2-container"></div> -
Individual State: Each map maintains its own routes, preferences, and camera position
Route Calculation Flow
- User clicks on one of the map buttons (🗺️1 or 🗺️2)
- Waypoints are defined based on the selected map
- Route calculation starts with
RoutingService.calculateRoute() - Routing handler is stored for potential cancellation
- On completion, routes are added to the appropriate map
- Camera centers on the calculated routes
- Route labels display distance and duration
Sample Routes
Map 1 - California Route:
- Start: San Francisco (37.77903, -122.41991)
- End: San Jose (37.33619, -121.89058)
Map 2 - UK Route:
- Start: London (51.50732, -0.12765)
- End: Canterbury (51.27483, 0.52316)
UI Controls
- ✖ Button: Remove all routes from both maps
- 🗺️1 Button: Calculate route on the first map (San Francisco → San Jose)
- 🗺️2 Button: Calculate route on the second map (London → Canterbury)
Use Cases
Multiple map views are useful for:
- Route Comparison: Compare different routes in different regions
- Before/After Scenarios: Show the same area with different settings
- Multi-Location Monitoring: Track different areas simultaneously
- Journey Planning: Plan multiple trip segments side by side
- Training/Demos: Show examples of different routing scenarios
- Split-Screen Navigation: Display overview and detail views
Layout Structure
The example uses a vertical split layout:
display: flex;
flex-direction: column;
Each map takes 50% of the available height:
flex: 1;
min-height: 0;
You can modify this to horizontal layout by changing flex-direction: row.
Advanced Customization
Horizontal Layout:
main.style.flexDirection = 'row';
Different Map Sizes:
map1Container.style.flex = '2'; // 2/3 of space
map2Container.style.flex = '1'; // 1/3 of space
More Than Two Maps:
const wrapper3 = gemKit.createView(9, onMap3Created);
Performance Considerations
- Each map instance consumes memory and rendering resources
- Limit the number of simultaneous map views based on device capabilities
- Consider lazy loading maps that aren't immediately visible
- Use unique view IDs for each map instance
- Clean up map instances when no longer needed
Next Steps
- Calculate Route - Learn basic route calculation
- Multiview Map - Different perspective on multi-map views
- Route Preferences - Customize routing options