|
| 1 | +/* |
| 2 | + * @license |
| 3 | + * Copyright 2025 Google LLC. All Rights Reserved. |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | +// [START maps_routes_get_directions_panel] |
| 7 | +// Initialize and add the map. |
| 8 | +let map; |
| 9 | +let mapPolylines: google.maps.Polyline[] = []; |
| 10 | +let markers: google.maps.marker.AdvancedMarkerElement[] = []; |
| 11 | +let center = { lat: 37.447646, lng: -122.113878 }; // Palo Alto, CA |
| 12 | + |
| 13 | +// Initialize and add the map. |
| 14 | +async function initMap(): Promise<void> { |
| 15 | + // Request the needed libraries. |
| 16 | + //@ts-ignore |
| 17 | + const [{Map}, {Route}] = await Promise.all([ |
| 18 | + google.maps.importLibrary('maps') as Promise<google.maps.MapsLibrary>, |
| 19 | + google.maps.importLibrary('routes') as Promise<google.maps.RoutesLibrary> |
| 20 | + ]); |
| 21 | + |
| 22 | + map = new Map(document.getElementById("map") as HTMLElement, { |
| 23 | + zoom: 12, |
| 24 | + center, |
| 25 | + mapTypeControl: false, |
| 26 | + mapId: 'DEMO_MAP_ID', |
| 27 | + }); |
| 28 | + |
| 29 | + // Define a simple directions request. |
| 30 | + const request = { |
| 31 | + origin: 'Mountain View, CA', |
| 32 | + destination: 'Sausalito, CA', |
| 33 | + intermediates: ['Half Moon Bay, CA', 'Pacifica Esplanade Beach'], |
| 34 | + travelMode: 'DRIVING', |
| 35 | + fields: ['legs', 'path'], |
| 36 | + }; |
| 37 | + |
| 38 | + // Call computeRoutes to get the directions. |
| 39 | + const { routes } = await Route.computeRoutes(request); |
| 40 | + |
| 41 | + // Display the raw JSON for the result in the console. |
| 42 | + console.log(`Response:\n ${JSON.stringify(routes, null, 2)}`); |
| 43 | + |
| 44 | + // Use createPolylines to create polylines for the route. |
| 45 | + mapPolylines = routes[0].createPolylines(); |
| 46 | + // Add polylines to the map. |
| 47 | + mapPolylines.forEach((polyline) => polyline.setMap(map)); |
| 48 | + |
| 49 | + fitMapToPath(routes[0].path!); |
| 50 | + |
| 51 | + // Add markers to all the points. |
| 52 | + const markers = await routes[0].createWaypointAdvancedMarkers({ map }); |
| 53 | + |
| 54 | + // [START maps_routes_get_directions_panel_steps] |
| 55 | + // Render navigation instructions |
| 56 | + const directionsPanel = document.getElementById("directions"); |
| 57 | + |
| 58 | + if (!routes || routes.length === 0) { |
| 59 | + if (directionsPanel) { |
| 60 | + directionsPanel.textContent = "No routes available."; |
| 61 | + } |
| 62 | + return; |
| 63 | + } |
| 64 | + |
| 65 | + const route = routes[0]; |
| 66 | + if (!route.legs || route.legs.length === 0) { |
| 67 | + if (directionsPanel) { |
| 68 | + directionsPanel.textContent = "The route has no legs."; |
| 69 | + } |
| 70 | + return; |
| 71 | + } |
| 72 | + |
| 73 | + const fragment = document.createDocumentFragment(); |
| 74 | + |
| 75 | + route.legs.forEach((leg, index) => { |
| 76 | + const legContainer = document.createElement("div"); |
| 77 | + legContainer.className = "directions-leg"; |
| 78 | + |
| 79 | + // Leg Title |
| 80 | + const legTitleElement = document.createElement("h3"); |
| 81 | + legTitleElement.textContent = `Leg ${index + 1} of ${route.legs.length}`; |
| 82 | + legContainer.appendChild(legTitleElement); |
| 83 | + |
| 84 | + if (leg.steps && leg.steps.length > 0) { |
| 85 | + // Add steps to an ordered list |
| 86 | + const stepsList = document.createElement("ol"); |
| 87 | + stepsList.className = "directions-steps"; |
| 88 | + |
| 89 | + leg.steps.forEach((step, stepIndex) => { |
| 90 | + const stepItem = document.createElement("li"); |
| 91 | + stepItem.className = "direction-step"; |
| 92 | + |
| 93 | + const directionWrapper = document.createElement("div"); |
| 94 | + directionWrapper.className = "direction"; |
| 95 | + |
| 96 | + // Maneuver |
| 97 | + if (step.maneuver) { |
| 98 | + const maneuverNode = document.createElement("p"); |
| 99 | + maneuverNode.textContent = step.maneuver; |
| 100 | + maneuverNode.className = "maneuver"; |
| 101 | + directionWrapper.appendChild(maneuverNode); |
| 102 | + } |
| 103 | + |
| 104 | + // Distance and Duration |
| 105 | + if (step.localizedValues) { |
| 106 | + const distanceNode = document.createElement("p"); |
| 107 | + distanceNode.textContent = `${step.localizedValues.distance} (${step.localizedValues.staticDuration})`; |
| 108 | + distanceNode.className = "distance"; |
| 109 | + directionWrapper.appendChild(distanceNode); |
| 110 | + } |
| 111 | + |
| 112 | + // Instructions |
| 113 | + if (step.instructions) { |
| 114 | + const instructionsNode = document.createElement("p"); |
| 115 | + instructionsNode.textContent = step.instructions; |
| 116 | + instructionsNode.className = "instruction"; |
| 117 | + directionWrapper.appendChild(instructionsNode); |
| 118 | + } |
| 119 | + |
| 120 | + stepItem.appendChild(directionWrapper); |
| 121 | + stepsList.appendChild(stepItem); |
| 122 | + }); |
| 123 | + legContainer.appendChild(stepsList); |
| 124 | + } |
| 125 | + |
| 126 | + fragment.appendChild(legContainer); |
| 127 | + directionsPanel?.appendChild(fragment); |
| 128 | + }); |
| 129 | + |
| 130 | +} |
| 131 | +// [END maps_routes_get_directions_panel_steps] |
| 132 | +// Helper function to fit the map to the path. |
| 133 | +async function fitMapToPath(path) { |
| 134 | + const { LatLngBounds } = await google.maps.importLibrary('core') as google.maps.CoreLibrary; |
| 135 | + const bounds = new LatLngBounds(); |
| 136 | + path.forEach((point) => { |
| 137 | + bounds.extend(point); |
| 138 | + }); |
| 139 | + map.fitBounds(bounds); |
| 140 | +} |
| 141 | + |
| 142 | +initMap(); |
| 143 | +// [END maps_routes_get_directions_panel] |
0 commit comments