-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathindex.ts
More file actions
73 lines (62 loc) · 1.72 KB
/
index.ts
File metadata and controls
73 lines (62 loc) · 1.72 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
/**
* @license
* Copyright 2025 Google LLC. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/
// [START maps_boundaries_text_search]
let innerMap;
let featureLayer;
let center;
async function initMap() {
// Load the needed libraries.
await google.maps.importLibrary('maps') as google.maps.MapsLibrary;
center = { lat: 41.059, lng: -124.151 }; // Trinidad, CA
// Get the gmp-map element.
const mapElement = document.querySelector(
'gmp-map'
) as google.maps.MapElement;
// Get the inner map.
innerMap = mapElement.innerMap;
// Get the LOCALITY feature layer.
featureLayer = innerMap.getFeatureLayer(google.maps.FeatureType.LOCALITY);
findBoundary();
}
// [START maps_boundaries_text_search_find_region]
async function findBoundary() {
const request = {
textQuery: 'Trinidad, CA',
fields: ['id', 'location'],
includedType: 'locality',
locationBias: center,
};
const { Place } = (await google.maps.importLibrary(
'places'
)) as google.maps.PlacesLibrary;
const { places } = await Place.searchByText(request);
if (places.length) {
const place = places[0];
styleBoundary(place.id);
innerMap.setCenter(place.location);
} else {
console.log('No results');
}
}
function styleBoundary(placeid) {
// Define a style of transparent purple with opaque stroke.
const styleFill = {
strokeColor: '#810FCB',
strokeOpacity: 1.0,
strokeWeight: 3.0,
fillColor: '#810FCB',
fillOpacity: 0.5,
};
// Define the feature style function.
featureLayer.style = (params) => {
if (params.feature.placeId == placeid) {
return styleFill;
}
};
}
// [END maps_boundaries_text_search_find_region]
initMap();
// [END maps_boundaries_text_search]