Skip to content

Commit 15361f8

Browse files
authored
feat: Migrates the boundaries-text-search sample. (#894)
1 parent 7368db0 commit 15361f8

File tree

6 files changed

+191
-0
lines changed

6 files changed

+191
-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-text-search`
15+
`npm start`
16+
17+
### Build an individual example
18+
19+
`cd samples/boundaries-text-search`
20+
`npm run build`
21+
22+
From 'samples':
23+
24+
`npm run build --workspace=boundaries-text-search/`
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-text-search`
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_text_search] -->
8+
<html>
9+
<head>
10+
<title>Boundaries Text Search</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: "beta"});</script>
17+
</head>
18+
<body>
19+
<gmp-map center="41.059,-124.151" zoom="15" map-id="8b37d7206ccf0121d4414bb0"></gmp-map>
20+
</body>
21+
</html>
22+
<!-- [END maps_boundaries_text_search] -->
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/**
2+
* @license
3+
* Copyright 2025 Google LLC. All Rights Reserved.
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
// [START maps_boundaries_text_search]
8+
let innerMap;
9+
let featureLayer;
10+
let center;
11+
12+
async function initMap() {
13+
// Load the needed libraries.
14+
await google.maps.importLibrary('maps') as google.maps.MapsLibrary;
15+
16+
center = { lat: 41.059, lng: -124.151 }; // Trinidad, CA
17+
18+
// Get the gmp-map element.
19+
const mapElement = document.querySelector(
20+
'gmp-map'
21+
) as google.maps.MapElement;
22+
23+
// Get the inner map.
24+
innerMap = mapElement.innerMap;
25+
26+
// Get the LOCALITY feature layer.
27+
featureLayer = innerMap.getFeatureLayer(google.maps.FeatureType.LOCALITY);
28+
29+
findBoundary();
30+
}
31+
// [START maps_boundaries_text_search_find_region]
32+
async function findBoundary() {
33+
const request = {
34+
textQuery: 'Trinidad, CA',
35+
fields: ['id', 'location'],
36+
includedType: 'locality',
37+
locationBias: center,
38+
};
39+
40+
const { Place } = (await google.maps.importLibrary(
41+
'places'
42+
)) as google.maps.PlacesLibrary;
43+
const { places } = await Place.searchByText(request);
44+
45+
if (places.length) {
46+
const place = places[0];
47+
styleBoundary(place.id);
48+
innerMap.setCenter(place.location);
49+
} else {
50+
console.log('No results');
51+
}
52+
}
53+
54+
function styleBoundary(placeid) {
55+
// Define a style of transparent purple with opaque stroke.
56+
const styleFill = {
57+
strokeColor: '#810FCB',
58+
strokeOpacity: 1.0,
59+
strokeWeight: 3.0,
60+
fillColor: '#810FCB',
61+
fillOpacity: 0.5,
62+
};
63+
64+
// Define the feature style function.
65+
featureLayer.style = (params) => {
66+
if (params.feature.placeId == placeid) {
67+
return styleFill;
68+
}
69+
};
70+
}
71+
// [END maps_boundaries_text_search_find_region]
72+
initMap();
73+
// [END maps_boundaries_text_search]
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-text-search",
3+
"version": "1.0.0",
4+
"scripts": {
5+
"build": "tsc && bash ../jsfiddle.sh boundaries-text-search && bash ../app.sh boundaries-text-search && bash ../docs.sh boundaries-text-search && npm run build:vite --workspace=. && bash ../dist.sh boundaries-text-search",
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_text_search] */
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_text_search] */
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)