-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathindex.ts
More file actions
102 lines (90 loc) · 2.78 KB
/
index.ts
File metadata and controls
102 lines (90 loc) · 2.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/*
* @license
* Copyright 2025 Google LLC. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/
// [START maps_routes_get_alternatives]
let mapPolylines: google.maps.Polyline[] = [];
const mapElement = document.querySelector('gmp-map') as google.maps.MapElement;
let innerMap;
// Initialize and add the map.
async function initMap() {
// Request the needed libraries.
await google.maps.importLibrary('maps') as google.maps.MapsLibrary;
innerMap = mapElement.innerMap;
innerMap.setOptions({
mapTypeControl: false,
mapId: 'DEMO_MAP_ID',
});
// Call the function after the map is loaded.
google.maps.event.addListenerOnce(innerMap, 'idle', () => {
getDirections();
});
}
async function getDirections() {
//@ts-ignore
// Request the needed libraries.
const [{ Route, RouteLabel }] = await Promise.all([
google.maps.importLibrary('routes')
]);
// [START maps_routes_get_alternatives_request_full]
// [START maps_routes_get_alternatives_request]
// Build a request.
const request = {
origin: 'San Francisco, CA',
destination: 'Sunset Dr Pacific Grove, CA 93950',
travelMode: 'DRIVING',
computeAlternativeRoutes: true,
fields: ['path', 'routeLabels', 'viewport'], // Request the routeLabels field.
};
// [END maps_routes_get_alternatives_request]
// [START maps_routes_get_alternatives_compute]
// Call computeRoutes to get the directions.
const result = await Route.computeRoutes(request);
if (!result.routes || result.routes.length === 0) {
console.warn("No routes found");
return;
}
let primaryRoute;
for (const route of result.routes) {
// Save the primary route for last so it's drawn on top.
if (
// Check for the default route.
route.routeLabels?.includes(RouteLabel.DEFAULT_ROUTE)
) {
primaryRoute = route;
} else {
drawRoute(route, false);
}
}
if (primaryRoute) {
drawRoute(primaryRoute, true);
await primaryRoute.createWaypointAdvancedMarkers({ map: innerMap });
innerMap.fitBounds(primaryRoute.viewport, 50);
innerMap.setHeading(70);
}
// [END maps_routes_get_alternatives_compute]
// [END maps_routes_get_alternatives_request_full]
// Display the raw JSON for the result in the console.
console.log(`Response:\n ${JSON.stringify(result, null, 2)}`);
}
function drawRoute(route, isPrimaryRoute) {
mapPolylines = mapPolylines.concat(
route.createPolylines({
polylineOptions: isPrimaryRoute
? {
map: innerMap,
strokeWeight: 5,
}
: {
map: innerMap,
strokeColor: "#669DF6",
strokeOpacity: 0.5,
strokeWeight: 5,
},
colorScheme: innerMap.get("colorScheme"),
}),
);
}
initMap();
// [END maps_routes_get_alternatives]