|
| 1 | +/* |
| 2 | + * @license |
| 3 | + * Copyright 2025 Google LLC. All Rights Reserved. |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | +// [START maps_routes_markers] |
| 7 | +let mapPolylines: google.maps.Polyline[] = []; |
| 8 | +const mapElement = document.querySelector('gmp-map') as google.maps.MapElement; |
| 9 | +let innerMap; |
| 10 | + |
| 11 | +// Initialize and add the map. |
| 12 | +async function initMap() { |
| 13 | + // Request the needed libraries. |
| 14 | + await google.maps.importLibrary('maps'); |
| 15 | + |
| 16 | + innerMap = await mapElement.innerMap; |
| 17 | + innerMap.setOptions({ |
| 18 | + mapTypeControl: false, |
| 19 | + mapId: 'DEMO_MAP_ID', |
| 20 | + }); |
| 21 | + |
| 22 | + // Call the function after the map is loaded. |
| 23 | + google.maps.event.addListenerOnce(innerMap, 'idle', () => { |
| 24 | + getDirections(); |
| 25 | + }); |
| 26 | +} |
| 27 | + |
| 28 | +async function getDirections() { |
| 29 | + //@ts-ignore |
| 30 | + // Request the needed libraries. |
| 31 | + const [{ Route }, { PinElement }] = await Promise.all([ |
| 32 | + google.maps.importLibrary('routes'), |
| 33 | + google.maps.importLibrary('marker'), |
| 34 | + ]); |
| 35 | + |
| 36 | + // [START maps_routes_markers_request_full] |
| 37 | + // [START maps_routes_markers_request] |
| 38 | + // Define routes request with an intermediate stop. |
| 39 | + const request = { |
| 40 | + origin: 'Parking lot, Christmas Tree Point Rd, San Francisco, CA 94131', |
| 41 | + destination: '100 Spinnaker Dr, Sausalito, CA 94965', // We're having a yummy lunch! |
| 42 | + intermediates: [{ location: '300 Finley Rd San Francisco, CA 94129' }], // But first, we golf! |
| 43 | + travelMode: 'DRIVING', |
| 44 | + fields: ['path', 'legs', 'viewport'], |
| 45 | + }; |
| 46 | + // [END maps_routes_markers_request] |
| 47 | + |
| 48 | + // Call computeRoutes to get the directions. |
| 49 | + const result = await Route.computeRoutes(request); |
| 50 | + if (!result.routes || result.routes.length === 0) { |
| 51 | + console.warn("No routes found"); |
| 52 | + return; |
| 53 | + } |
| 54 | + // [END maps_routes_markers_request_full] |
| 55 | + |
| 56 | + // [START maps_routes_markers_style_maker] |
| 57 | + // Alter style based on marker index. |
| 58 | + function markerOptionsMaker( |
| 59 | + defaultOptions: google.maps.marker.AdvancedMarkerElementOptions, |
| 60 | + //@ts-ignore |
| 61 | + waypointMarkerDetails: google.maps.routes.WaypointMarkerDetails |
| 62 | + ) { |
| 63 | + const { index, totalMarkers, leg } = waypointMarkerDetails; |
| 64 | + |
| 65 | + // Style the origin waypoint. |
| 66 | + if (index === 0) { |
| 67 | + return { |
| 68 | + ...defaultOptions, |
| 69 | + map: innerMap, |
| 70 | + content: new PinElement({ |
| 71 | + glyph: (index + 1).toString(), |
| 72 | + glyphColor: 'white', |
| 73 | + background: 'green', |
| 74 | + borderColor: 'green', |
| 75 | + }).element |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + // Style all intermediate waypoints. |
| 80 | + if (!(index === 0 || index === totalMarkers - 1)) { |
| 81 | + return { |
| 82 | + ...defaultOptions, |
| 83 | + map: innerMap, |
| 84 | + content: new PinElement({ |
| 85 | + glyph: (index + 1).toString(), |
| 86 | + glyphColor: 'white', |
| 87 | + background: 'blue', |
| 88 | + borderColor: 'blue', |
| 89 | + }).element |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + // Style the destination waypoint. |
| 94 | + if (index === totalMarkers - 1) { |
| 95 | + return { |
| 96 | + ...defaultOptions, |
| 97 | + map: innerMap, |
| 98 | + content: new PinElement({ |
| 99 | + glyph: (index + 1).toString(), |
| 100 | + glyphColor: 'white', |
| 101 | + background: 'red', |
| 102 | + borderColor: 'red', |
| 103 | + }).element |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + return { ...defaultOptions, map: innerMap }; |
| 108 | + } |
| 109 | + |
| 110 | + const markers = await result.routes[0].createWaypointAdvancedMarkers(markerOptionsMaker); |
| 111 | + // [END maps_routes_markers_style_maker] |
| 112 | + |
| 113 | + |
| 114 | + // Fit the map to the route. |
| 115 | + innerMap.fitBounds(result.routes[0].viewport); |
| 116 | + innerMap.setHeading(270); |
| 117 | + |
| 118 | + // Create polylines and add them to the map. |
| 119 | + mapPolylines = result.routes[0].createPolylines(); |
| 120 | + mapPolylines.forEach((polyline) => polyline.setMap(innerMap)); |
| 121 | +} |
| 122 | + |
| 123 | +initMap(); |
| 124 | +// [END maps_routes_markers] |
0 commit comments