|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2026 Google LLC. All Rights Reserved. |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | +// [START maps_dds_datasets_polygon_click] |
| 7 | +const mapElement = document.querySelector('gmp-map') as google.maps.MapElement; |
| 8 | +let innerMap; |
| 9 | +let lastInteractedFeatureIds: string[] = []; |
| 10 | +let lastClickedFeatureIds: string[] = []; |
| 11 | +let datasetLayer; |
| 12 | + |
| 13 | +// [START maps_dds_datasets_polygon_click_eventhandler] |
| 14 | +// Note, 'globalid' is an attribute in this Dataset. |
| 15 | +function handleClick(/* MouseEvent */ e) { |
| 16 | + if (e.features) { |
| 17 | + lastClickedFeatureIds = e.features.map( |
| 18 | + (f) => f.datasetAttributes['globalid'] |
| 19 | + ); |
| 20 | + } |
| 21 | + datasetLayer.style = applyStyle; |
| 22 | +} |
| 23 | + |
| 24 | +function handleMouseMove(/* MouseEvent */ e) { |
| 25 | + if (e.features) { |
| 26 | + lastInteractedFeatureIds = e.features.map( |
| 27 | + (f) => f.datasetAttributes['globalid'] |
| 28 | + ); |
| 29 | + } |
| 30 | + datasetLayer.style = applyStyle; |
| 31 | +} |
| 32 | +// [END maps_dds_datasets_polygon_click_eventhandler] |
| 33 | + |
| 34 | +async function initMap() { |
| 35 | + // Request needed libraries. |
| 36 | + (await google.maps.importLibrary('maps')) as google.maps.MapsLibrary; |
| 37 | + |
| 38 | + // Get the inner map. |
| 39 | + innerMap = mapElement.innerMap; |
| 40 | + |
| 41 | + // Dataset ID for NYC park data. |
| 42 | + const datasetId = 'a75dd002-ad20-4fe6-af60-27cd2ed636b4'; |
| 43 | + |
| 44 | + // [START maps_dds_datasets_polygon_click_addlistener] |
| 45 | + datasetLayer = innerMap.getDatasetFeatureLayer(datasetId); |
| 46 | + datasetLayer.style = applyStyle; |
| 47 | + |
| 48 | + datasetLayer.addListener('click', handleClick); |
| 49 | + datasetLayer.addListener('mousemove', handleMouseMove); |
| 50 | + |
| 51 | + // Map event listener. |
| 52 | + innerMap.addListener('mousemove', () => { |
| 53 | + // If the map gets a mousemove, that means there are no feature layers |
| 54 | + // with listeners registered under the mouse, so we clear the last |
| 55 | + // interacted feature ids. |
| 56 | + if (lastInteractedFeatureIds?.length) { |
| 57 | + lastInteractedFeatureIds = []; |
| 58 | + datasetLayer.style = applyStyle; |
| 59 | + } |
| 60 | + }); |
| 61 | + // [END maps_dds_datasets_polygon_click_addlistener] |
| 62 | +} |
| 63 | + |
| 64 | +// [START maps_dds_datasets_polygon_click_stylefunction] |
| 65 | +const styleDefault = { |
| 66 | + strokeColor: 'green', |
| 67 | + strokeWeight: 2.0, |
| 68 | + strokeOpacity: 1.0, |
| 69 | + fillColor: 'green', |
| 70 | + fillOpacity: 0.3, |
| 71 | +}; |
| 72 | + |
| 73 | +const styleClicked = { |
| 74 | + ...styleDefault, |
| 75 | + strokeColor: 'blue', |
| 76 | + fillColor: 'blue', |
| 77 | + fillOpacity: 0.5, |
| 78 | +}; |
| 79 | + |
| 80 | +const styleMouseMove = { |
| 81 | + ...styleDefault, |
| 82 | + strokeWeight: 4.0, |
| 83 | +}; |
| 84 | + |
| 85 | +function applyStyle(/* FeatureStyleFunctionOptions */ params) { |
| 86 | + const datasetFeature = params.feature; |
| 87 | + |
| 88 | + // Note, 'globalid' is an attribute in this dataset. |
| 89 | + if ( |
| 90 | + lastClickedFeatureIds.includes( |
| 91 | + datasetFeature.datasetAttributes['globalid'] |
| 92 | + ) |
| 93 | + ) { |
| 94 | + return styleClicked; |
| 95 | + } |
| 96 | + |
| 97 | + if ( |
| 98 | + lastInteractedFeatureIds.includes( |
| 99 | + datasetFeature.datasetAttributes['globalid'] |
| 100 | + ) |
| 101 | + ) { |
| 102 | + return styleMouseMove; |
| 103 | + } |
| 104 | + return styleDefault; |
| 105 | +} |
| 106 | +// [END maps_dds_datasets_polygon_click_stylefunction] |
| 107 | + |
| 108 | +initMap(); |
| 109 | +// [END maps_dds_datasets_polygon_click] |
0 commit comments