-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathindex.js
More file actions
83 lines (83 loc) · 2.77 KB
/
index.js
File metadata and controls
83 lines (83 loc) · 2.77 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
"use strict";
/**
* @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;
let lastInteractedFeatureIds = [];
let lastClickedFeatureIds = [];
let datasetLayer;
// [START maps_dds_datasets_polygon_click_eventhandler]
// Note, 'globalid' is an attribute in this Dataset.
function handleClick(/* MouseEvent */ e) {
if (e.features) {
lastClickedFeatureIds = e.features.map((f) => f.datasetAttributes['globalid']);
}
datasetLayer.style = applyStyle;
}
function handleMouseMove(/* MouseEvent */ e) {
if (e.features) {
lastInteractedFeatureIds = e.features.map((f) => f.datasetAttributes['globalid']);
}
datasetLayer.style = applyStyle;
}
// [END maps_dds_datasets_polygon_click_eventhandler]
async function initMap() {
// 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(/* FeatureStyleFunctionOptions */ params) {
const datasetFeature = params.feature;
// Note, 'globalid' is an attribute in this dataset.
if (lastClickedFeatureIds.includes(datasetFeature.datasetAttributes['globalid'])) {
return styleClicked;
}
if (lastInteractedFeatureIds.includes(datasetFeature.datasetAttributes['globalid'])) {
return styleMouseMove;
}
return styleDefault;
}
// [END maps_dds_datasets_polygon_click_stylefunction]
initMap();
// [END maps_dds_datasets_polygon_click]