Skip to content

Commit e9ecf7b

Browse files
authored
feat: Migrates the boundaries-choropleth sample; standards update. (#878)
* feat: Migrates the boundaries-choropleth sample. * Update Google Maps API key in index.html
1 parent cce5650 commit e9ecf7b

File tree

6 files changed

+226
-0
lines changed

6 files changed

+226
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Google Maps JavaScript Sample
2+
3+
This sample is generated from @googlemaps/js-samples located at
4+
https://github.com/googlemaps-samples/js-api-samples.
5+
6+
## Setup
7+
8+
### Before starting run:
9+
10+
`npm i`
11+
12+
### Run an example on a local web server
13+
14+
`cd samples/boundaries-choropleth`
15+
`npm start`
16+
17+
### Build an individual example
18+
19+
`cd samples/boundaries-choropleth`
20+
`npm run build`
21+
22+
From 'samples':
23+
24+
`npm run build --workspace=boundaries-choropleth/`
25+
26+
### Build all of the examples.
27+
28+
From 'samples':
29+
30+
`npm run build-all`
31+
32+
### Run lint to check for problems
33+
34+
`cd samples/boundaries-choropleth`
35+
`npx eslint index.ts`
36+
37+
## Feedback
38+
39+
For feedback related to this sample, please open a new issue on
40+
[GitHub](https://github.com/googlemaps-samples/js-api-samples/issues).
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<!doctype html>
2+
<!--
3+
@license
4+
Copyright 2025 Google LLC. All Rights Reserved.
5+
SPDX-License-Identifier: Apache-2.0
6+
-->
7+
<!-- [START maps_boundaries_choropleth] -->
8+
<html>
9+
<head>
10+
<title>Choropleth Map</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 center="40.76,-101.64" zoom="5" map-id="8b37d7206ccf01219cd548d3">
20+
</body>
21+
</html>
22+
<!-- [END maps_boundaries_choropleth] -->
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
/**
2+
* @license
3+
* Copyright 2025 Google LLC. All Rights Reserved.
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
// [START maps_boundaries_choropleth]
8+
async function initMap() {
9+
// Request needed libraries.
10+
(await google.maps.importLibrary('maps')) as google.maps.MapsLibrary;
11+
12+
// Get the gmp-map element.
13+
const mapElement = document.querySelector(
14+
'gmp-map'
15+
) as google.maps.MapElement;
16+
17+
// Get the inner map.
18+
const innerMap = mapElement.innerMap;
19+
20+
const featureLayer = innerMap.getFeatureLayer(
21+
google.maps.FeatureType.ADMINISTRATIVE_AREA_LEVEL_1
22+
);
23+
24+
// [START maps_boundaries_choropleth_style_function]
25+
featureLayer.style = (featureStyleFunctionOptions) => {
26+
const placeFeature =
27+
featureStyleFunctionOptions.feature as google.maps.PlaceFeature;
28+
const population = states[placeFeature.placeId];
29+
30+
let fillColor;
31+
// Specify colors using any of the following:
32+
// * Named ('green')
33+
// * Hexadecimal ('#FF0000')
34+
// * RGB ('rgb(0, 0, 255)')
35+
// * HSL ('hsl(60, 100%, 50%)')
36+
37+
if (population < 2000000) {
38+
fillColor = 'green';
39+
} else if (population < 5000000) {
40+
fillColor = 'red';
41+
} else if (population < 10000000) {
42+
fillColor = 'blue';
43+
} else if (population < 40000000) {
44+
fillColor = 'yellow';
45+
}
46+
return {
47+
fillColor,
48+
fillOpacity: 0.5,
49+
};
50+
};
51+
// [END maps_boundaries_choropleth_style_function]
52+
// Population data by state.
53+
const states = {
54+
'ChIJdf5LHzR_hogR6czIUzU0VV4': 5039877, // Alabama
55+
'ChIJG8CuwJzfAFQRNduKqSde27w': 732673, // Alaska
56+
'ChIJaxhMy-sIK4cRcc3Bf7EnOUI': 7276316, // Arizona
57+
'ChIJYSc_dD-e0ocR0NLf_z5pBaQ': 3025891, // Arkansas
58+
'ChIJPV4oX_65j4ARVW8IJ6IJUYs': 39237836, // California
59+
'ChIJt1YYm3QUQIcR_6eQSTGDVMc': 5812069, // Colorado
60+
'ChIJpVER8hFT5okR5XBhBVttmq4': 3605597, // Connecticut
61+
'ChIJO9YMTXYFx4kReOgEjBItHZQ': 1003384, // Delaware
62+
'ChIJvypWkWV2wYgR0E7HW9MTLvc': 21781128, // Florida
63+
'ChIJV4FfHcU28YgR5xBP7BC8hGY': 10799566, // Georgia
64+
'ChIJBeB5Twbb_3sRKIbMdNKCd0s': 1441553, // Hawaii
65+
'ChIJ6Znkhaj_WFMRWIf3FQUwa9A': 1900923, // Idaho
66+
'ChIJGSZubzgtC4gRVlkRZFCCFX8': 12671469, // Illinois
67+
'ChIJHRv42bxQa4gRcuwyy84vEH4': 6805985, // Indiana
68+
'ChIJGWD48W9e7ocR2VnHV0pj78Y': 3193079, // Iowa
69+
'ChIJawF8cXEXo4cRXwk-S6m0wmg': 2934582, // Kansas
70+
'ChIJyVMZi0xzQogR_N_MxU5vH3c': 4509394, // Kentucky
71+
'ChIJZYIRslSkIIYRA0flgTL3Vck': 4624047, // Louisiana
72+
'ChIJ1YpTHd4dsEwR0KggZ2_MedY': 1372247, // Maine
73+
'ChIJ35Dx6etNtokRsfZVdmU3r_I': 6165129, // Maryland
74+
'ChIJ_b9z6W1l44kRHA2DVTbQxkU': 6984723, // Massachussetts
75+
'ChIJEQTKxz2qTE0Rs8liellI3Zc': 10050811, // Michigan
76+
'ChIJmwt4YJpbWE0RD6L-EJvJogI': 5707390, // Minnesota
77+
'ChIJGdRK5OQyKIYR2qbc6X8XDWI': 2949965, // Mississippi
78+
'ChIJfeMiSNXmwIcRcr1mBFnEW7U': 6168187, // Misssouri
79+
'ChIJ04p7LZwrQVMRGGwqz1jWcfU': 1104271, // Montana
80+
'ChIJ7fwMtciNk4cRxArzDwyQJ6E': 1963692, // Nebraska
81+
'ChIJcbTe-KEKmYARs5X8qooDR88': 3143991, // Nevada
82+
'ChIJ66bAnUtEs0wR64CmJa8CyNc': 1388992, // New Hampshire
83+
'ChIJn0AAnpX7wIkRjW0_-Ad70iw': 9267130, // New Jersey
84+
'ChIJqVKY50NQGIcRup41Yxpuv0Y': 2115877, // New Mexico
85+
'ChIJqaUj8fBLzEwRZ5UY3sHGz90': 19835913, // New York
86+
'ChIJgRo4_MQfVIgRGa4i6fUwP60': 10551162, // North Carolina
87+
'ChIJY-nYVxKD11IRyc9egzmahA0': 774948, // North Dakota
88+
'ChIJwY5NtXrpNogRFtmfnDlkzeU': 11780017, // Ohio
89+
'ChIJnU-ssRE5rIcRSOoKQDPPHF0': 3986639, // Oklahoma
90+
'ChIJVWqfm3xuk1QRdrgLettlTH0': 4246155, // Oregon
91+
'ChIJieUyHiaALYgRPbQiUEchRsI': 12964056, // Pennsylvania
92+
'ChIJD9cOYhQ15IkR5wbB57wYTh4': 1095610, // Rhode Island
93+
'ChIJ49ExeWml-IgRnhcF9TKh_7k': 5190705, // South Carolina
94+
'ChIJpTjphS1DfYcRt6SGMSnW8Ac': 895376, // South Dakota
95+
'ChIJA8-XniNLYYgRVpGBpcEgPgM': 6975218, // Tennessee
96+
'ChIJSTKCCzZwQIYRPN4IGI8c6xY': 29527941, // Texas
97+
'ChIJzfkTj8drTIcRP0bXbKVK370': 3337975, // Utah
98+
'ChIJ_87aSGzctEwRtGtUNnSJTSY': 645570, // Vermont
99+
'ChIJzbK8vXDWTIgRlaZGt0lBTsA': 8642274, // Virginia
100+
'ChIJ-bDD5__lhVQRuvNfbGh4QpQ': 7738692, // Washington
101+
'ChIJRQnL1KVUSogRQzrN3mjHALs': 1782959, // West Virginia
102+
'ChIJr-OEkw_0qFIR1kmG-LjV1fI': 5895908, // Wisconsin
103+
'ChIJaS7hSDTiXocRLzh90nkisCY': 578803, // Wyoming
104+
};
105+
}
106+
107+
initMap();
108+
// [END maps_boundaries_choropleth]
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"name": "@js-api-samples/boundaries-choropleth",
3+
"version": "1.0.0",
4+
"scripts": {
5+
"build": "tsc && bash ../jsfiddle.sh boundaries-choropleth && bash ../app.sh boundaries-choropleth && bash ../docs.sh boundaries-choropleth && npm run build:vite --workspace=. && bash ../dist.sh boundaries-choropleth",
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: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* @license
3+
* Copyright 2025 Google LLC. All Rights Reserved.
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
/* [START maps_boundaries_choropleth] */
7+
/*
8+
* Always set the map height explicitly to define the size of the div element
9+
* that contains the map.
10+
*/
11+
gmp-map {
12+
height: 100%;
13+
}
14+
15+
/*
16+
* Optional: Makes the sample page fill the window.
17+
*/
18+
html,
19+
body {
20+
height: 100%;
21+
margin: 0;
22+
padding: 0;
23+
}
24+
25+
/* [END maps_boundaries_choropleth] */
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)