Skip to content

Commit 3ea93b8

Browse files
authored
feat: Migrates the dds-datasets-polygon-click sample. (#1080)
* feat: Migrates the dds-datasets-polygon-click sample. * Update index.ts * Refactor event listener addition for dataset layer * Refactor applyStyle function to simplify checks Remove unnecessary check for datasetAttributes in applyStyle function. * Comment out mousemove listener in index.ts Temporarily comment out mousemove event listener for testing. * Restore commented stanza Done testing, sorry to say that didn't move the needle. * Swap 'globalid' for 'typecategory' in feature ID checks temporary change from 'globalid' to 'typecategory' in feature ID checks. This will help us to determine if globalid has a role in the 400 errors in the tests. * Restore usage of 'globalid' in feature checks Reverted changes to use 'globalid' attribute for feature identification.
1 parent 759589b commit 3ea93b8

6 files changed

Lines changed: 235 additions & 0 deletions

File tree

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Google Maps JavaScript Sample
2+
3+
## dds-datasets-polygon-click
4+
5+
This sample shows how to make data features respond to mouse events.
6+
7+
## Setup
8+
9+
### Before starting run:
10+
11+
`npm i`
12+
13+
### Run an example on a local web server
14+
15+
`cd samples/dds-datasets-polygon-click`
16+
`npm start`
17+
18+
### Build an individual example
19+
20+
`cd samples/dds-datasets-polygon-click`
21+
`npm run build`
22+
23+
From 'samples':
24+
25+
`npm run build --workspace=dds-datasets-polygon-click/`
26+
27+
### Build all of the examples.
28+
29+
From 'samples':
30+
31+
`npm run build-all`
32+
33+
### Run lint to check for problems
34+
35+
`cd samples/dds-datasets-polygon-click`
36+
`npx eslint index.ts`
37+
38+
## Feedback
39+
40+
For feedback related to this sample, please open a new issue on
41+
[GitHub](https://github.com/googlemaps-samples/js-api-samples/issues).
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<!doctype html>
2+
<!--
3+
@license
4+
Copyright 2026 Google LLC. All Rights Reserved.
5+
SPDX-License-Identifier: Apache-2.0
6+
-->
7+
<!-- [START maps_dds_datasets_polygon_click] -->
8+
<html>
9+
<head>
10+
<title>Style a polygon data feature</title>
11+
12+
<link rel="stylesheet" type="text/css" href="./style.css" />
13+
<script type="module" src="./index.js"></script>
14+
<!-- prettier-ignore -->
15+
<script>(g=>{var h,a,k,p="The Google Maps JavaScript API",c="google",l="importLibrary",q="__ib__",m=document,b=window;b=b[c]||(b[c]={});var d=b.maps||(b.maps={}),r=new Set,e=new URLSearchParams,u=()=>h||(h=new Promise(async(f,n)=>{await (a=m.createElement("script"));e.set("libraries",[...r]+"");for(k in g)e.set(k.replace(/[A-Z]/g,t=>"_"+t[0].toLowerCase()),g[k]);e.set("callback",c+".maps."+q);a.src=`https://maps.${c}apis.com/maps/api/js?`+e;d[q]=f;a.onerror=()=>h=n(Error(p+" could not load."));a.nonce=m.querySelector("script[nonce]")?.nonce||"";m.head.append(a)}));d[l]?console.warn(p+" only loads once. Ignoring:",g):d[l]=(f,...n)=>r.add(f)&&u().then(()=>d[l](f,...n))})
16+
({key: "AIzaSyA6myHzS10YXdcazAFalmXvDkrYCp5cLc8", v: "weekly"});</script>
17+
</head>
18+
<body>
19+
<gmp-map
20+
center="40.780101, -73.967780"
21+
zoom="13"
22+
map-id="5cd2c9ca1cf05670"
23+
map-type-control="false">
24+
<div id="attribution" slot="control-block-end-inline-start">
25+
Data source: NYC Open Data
26+
</div>
27+
</gmp-map>
28+
</body>
29+
</html>
30+
<!-- [END maps_dds_datasets_polygon_click] -->
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
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]
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"name": "@js-api-samples/dds-datasets-polygon-click",
3+
"version": "1.0.0",
4+
"scripts": {
5+
"build": "tsc && bash ../jsfiddle.sh dds-datasets-polygon-click && bash ../app.sh dds-datasets-polygon-click && bash ../docs.sh dds-datasets-polygon-click && npm run build:vite --workspace=. && bash ../dist.sh dds-datasets-polygon-click",
6+
"test": "tsc && npm run build:vite --workspace=.",
7+
"start": "tsc && vite build --base './' && vite",
8+
"build:vite": "vite build --base './'",
9+
"preview": "vite preview"
10+
},
11+
"dependencies": {
12+
13+
}
14+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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+
/*
8+
* Optional: Makes the sample page fill the window.
9+
*/
10+
html,
11+
body {
12+
height: 100%;
13+
margin: 0;
14+
padding: 0;
15+
}
16+
17+
#attribution {
18+
background-color: rgba(255, 255, 255, 0.7);
19+
font-family: 'Roboto', 'Arial', 'sans-serif';
20+
font-size: 10px;
21+
padding: 2px;
22+
margin: 2px;
23+
}
24+
/* [END maps_dds_datasets_polygon_click] */
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"compilerOptions": {
3+
"module": "esnext",
4+
"target": "esnext",
5+
"strict": true,
6+
"noImplicitAny": false,
7+
"lib": [
8+
"es2015",
9+
"esnext",
10+
"es6",
11+
"dom",
12+
"dom.iterable"
13+
],
14+
"moduleResolution": "Node",
15+
"jsx": "preserve"
16+
}
17+
}

0 commit comments

Comments
 (0)