-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathindex.ts
More file actions
108 lines (92 loc) · 3.07 KB
/
Copy pathindex.ts
File metadata and controls
108 lines (92 loc) · 3.07 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
103
104
105
106
107
108
/**
* @license
* Copyright 2026 Google LLC. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/
// [START maps_dds_datasets_polygon_click]
const mapElement = document.querySelector('gmp-map')!;
let innerMap: google.maps.Map;
let lastInteractedFeatureIds: string[] = [];
let lastClickedFeatureIds: string[] = [];
let datasetLayer: google.maps.FeatureLayer;
// [START maps_dds_datasets_polygon_click_eventhandler]
// Note, 'globalid' is an attribute in this Dataset.
function handleClick(event: google.maps.FeatureMouseEvent) {
lastClickedFeatureIds = event.features.map(
(f) => (f as google.maps.DatasetFeature).datasetAttributes.globalid
);
datasetLayer.style = applyStyle;
}
function handleMouseMove(event: google.maps.FeatureMouseEvent) {
lastInteractedFeatureIds = event.features.map(
(f) => (f as google.maps.DatasetFeature).datasetAttributes.globalid
);
datasetLayer.style = applyStyle;
}
// [END maps_dds_datasets_polygon_click_eventhandler]
async function init() {
// Request needed libraries.
await google.maps.importLibrary('maps');
// Get the inner map.
innerMap = mapElement.innerMap;
// Dataset ID for NYC park data.
const datasetId = 'a75dd002-ad20-4fe6-af60-27cd2ed636b4';
// [START maps_dds_datasets_polygon_click_addlistener]
datasetLayer = innerMap.getDatasetFeatureLayer(datasetId);
datasetLayer.style = applyStyle;
datasetLayer.addListener('click', handleClick);
datasetLayer.addListener('mousemove', handleMouseMove);
// Map event listener.
innerMap.addListener('mousemove', () => {
// If the map gets a mousemove, that means there are no feature layers
// with listeners registered under the mouse, so we clear the last
// interacted feature ids.
if (lastInteractedFeatureIds.length) {
lastInteractedFeatureIds = [];
datasetLayer.style = applyStyle;
}
});
// [END maps_dds_datasets_polygon_click_addlistener]
}
// [START maps_dds_datasets_polygon_click_stylefunction]
const styleDefault = {
strokeColor: 'green',
strokeWeight: 2.0,
strokeOpacity: 1.0,
fillColor: 'green',
fillOpacity: 0.3,
};
const styleClicked = {
...styleDefault,
strokeColor: 'blue',
fillColor: 'blue',
fillOpacity: 0.5,
};
const styleMouseMove = {
...styleDefault,
strokeWeight: 4.0,
};
function applyStyle(params: google.maps.FeatureStyleFunctionOptions) {
const datasetFeature = params.feature;
// Note, 'globalid' is an attribute in this dataset.
if (
lastClickedFeatureIds.includes(
(datasetFeature as google.maps.DatasetFeature).datasetAttributes
.globalid
)
) {
return styleClicked;
}
if (
lastInteractedFeatureIds.includes(
(datasetFeature as google.maps.DatasetFeature).datasetAttributes
.globalid
)
) {
return styleMouseMove;
}
return styleDefault;
}
// [END maps_dds_datasets_polygon_click_stylefunction]
void init();
// [END maps_dds_datasets_polygon_click]