|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2025 Google LLC. All Rights Reserved. |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +// [START maps_boundaries_click_event] |
| 8 | +let innerMap; |
| 9 | +let featureLayer; |
| 10 | +let infoWindow; |
| 11 | +let lastInteractedFeatureIds = []; |
| 12 | +let lastClickedFeatureIds = []; |
| 13 | + |
| 14 | +// [START maps_boundaries_click_event_handler] |
| 15 | +function handleClick(/* MouseEvent */ e) { |
| 16 | + lastClickedFeatureIds = e.features.map((f) => f.placeId); |
| 17 | + lastInteractedFeatureIds = []; |
| 18 | + featureLayer.style = applyStyle; |
| 19 | + createInfoWindow(e); |
| 20 | +} |
| 21 | + |
| 22 | +function handleMouseMove(/* MouseEvent */ e) { |
| 23 | + lastInteractedFeatureIds = e.features.map((f) => f.placeId); |
| 24 | + featureLayer.style = applyStyle; |
| 25 | +} |
| 26 | +// [END maps_boundaries_click_event_handler] |
| 27 | + |
| 28 | +async function initMap() { |
| 29 | + // Request needed libraries. |
| 30 | + const { Map, InfoWindow } = (await google.maps.importLibrary( |
| 31 | + 'maps' |
| 32 | + )) as google.maps.MapsLibrary; |
| 33 | + |
| 34 | + // Get the gmp-map element. |
| 35 | + const mapElement = document.querySelector( |
| 36 | + 'gmp-map' |
| 37 | + ) as google.maps.MapElement; |
| 38 | + |
| 39 | + // Get the inner map. |
| 40 | + innerMap = mapElement.innerMap; |
| 41 | + |
| 42 | + // Set map options. |
| 43 | + innerMap.setOptions({ |
| 44 | + mapTypeControl: false, |
| 45 | + }); |
| 46 | + |
| 47 | + //[START maps_boundaries_click_event_add_layer] |
| 48 | + // Add the feature layer. |
| 49 | + featureLayer = innerMap.getFeatureLayer( |
| 50 | + google.maps.FeatureType.ADMINISTRATIVE_AREA_LEVEL_2 |
| 51 | + ); |
| 52 | + |
| 53 | + // Add the event listeners for the feature layer. |
| 54 | + featureLayer.addListener('click', handleClick); |
| 55 | + featureLayer.addListener('mousemove', handleMouseMove); |
| 56 | + |
| 57 | + // Map event listener. |
| 58 | + innerMap.addListener('mousemove', () => { |
| 59 | + // If the map gets a mousemove, that means there are no feature layers |
| 60 | + // with listeners registered under the mouse, so we clear the last |
| 61 | + // interacted feature ids. |
| 62 | + if (lastInteractedFeatureIds?.length) { |
| 63 | + lastInteractedFeatureIds = []; |
| 64 | + featureLayer.style = applyStyle; |
| 65 | + } |
| 66 | + }); |
| 67 | + //[END maps_boundaries_click_event_add_layer] |
| 68 | + |
| 69 | + // Create the infowindow. |
| 70 | + infoWindow = new InfoWindow({}); |
| 71 | + // Apply style on load, to enable clicking. |
| 72 | + featureLayer.style = applyStyle; |
| 73 | +} |
| 74 | + |
| 75 | +// Helper function for the infowindow. |
| 76 | +async function createInfoWindow(event) { |
| 77 | + let feature = event.features[0]; |
| 78 | + if (!feature.placeId) return; |
| 79 | + |
| 80 | + // Update the info window. |
| 81 | + // Get the place instance from the selected feature. |
| 82 | + const place = await feature.fetchPlace(); |
| 83 | + |
| 84 | + // Create a new div to hold the text content. |
| 85 | + let content = document.createElement('div'); |
| 86 | + |
| 87 | + // Get the text values. |
| 88 | + let nameText = document.createElement('span'); |
| 89 | + nameText.textContent = `Display name: ${place.displayName}`; |
| 90 | + let placeIdText = document.createElement('span'); |
| 91 | + placeIdText.textContent = `Place ID: ${feature.placeId}`; |
| 92 | + let featureTypeText = document.createElement('span'); |
| 93 | + featureTypeText.textContent = `Feature type: ${feature.featureType}`; |
| 94 | + |
| 95 | + // Append the text to the div. |
| 96 | + content.appendChild(nameText); |
| 97 | + content.appendChild(document.createElement('br')); |
| 98 | + content.appendChild(placeIdText); |
| 99 | + content.appendChild(document.createElement('br')); |
| 100 | + content.appendChild(featureTypeText); |
| 101 | + |
| 102 | + updateInfoWindow(content, event.latLng); |
| 103 | +} |
| 104 | + |
| 105 | +// [START maps_boundaries_click_event_style] |
| 106 | +// Define styles. |
| 107 | +// Stroke and fill with minimum opacity value. |
| 108 | +const styleDefault = { |
| 109 | + strokeColor: '#810FCB', |
| 110 | + strokeOpacity: 1.0, |
| 111 | + strokeWeight: 2.0, |
| 112 | + fillColor: 'white', |
| 113 | + fillOpacity: 0.1, // Polygons must be visible to receive events. |
| 114 | +}; |
| 115 | +// Style for the clicked polygon. |
| 116 | +const styleClicked = { |
| 117 | + ...styleDefault, |
| 118 | + fillColor: '#810FCB', |
| 119 | + fillOpacity: 0.5, |
| 120 | +}; |
| 121 | +// Style for polygon on mouse move. |
| 122 | +const styleMouseMove = { |
| 123 | + ...styleDefault, |
| 124 | + strokeWeight: 4.0, |
| 125 | +}; |
| 126 | + |
| 127 | +// Apply styles using a feature style function. |
| 128 | +function applyStyle(/* FeatureStyleFunctionOptions */ params) { |
| 129 | + const placeId = params.feature.placeId; |
| 130 | + //@ts-ignore |
| 131 | + if (lastClickedFeatureIds.includes(placeId)) { |
| 132 | + return styleClicked; |
| 133 | + } |
| 134 | + //@ts-ignore |
| 135 | + if (lastInteractedFeatureIds.includes(placeId)) { |
| 136 | + return styleMouseMove; |
| 137 | + } |
| 138 | + return styleDefault; |
| 139 | +} |
| 140 | +// [END maps_boundaries_click_event_style] |
| 141 | + |
| 142 | +// Helper function to create an info window. |
| 143 | +function updateInfoWindow(content, center) { |
| 144 | + infoWindow.setContent(content); |
| 145 | + infoWindow.setPosition(center); |
| 146 | + infoWindow.open({ |
| 147 | + map: innerMap, |
| 148 | + shouldFocus: false, |
| 149 | + }); |
| 150 | +} |
| 151 | + |
| 152 | +initMap(); |
| 153 | +// [END maps_boundaries_click_event] |
0 commit comments