-
Notifications
You must be signed in to change notification settings - Fork 823
/
index.ts
79 lines (63 loc) · 2.18 KB
/
index.ts
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
/**
* @license
* Copyright 2024 Google LLC. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/
// [START maps_place_nearby_search]
let map;
async function initMap() {
const { Map, InfoWindow } = await google.maps.importLibrary('maps') as google.maps.MapsLibrary;
let center = new google.maps.LatLng(52.369358, 4.889258);
map = new Map(document.getElementById('map') as HTMLElement, {
center: center,
zoom: 11,
mapId: 'DEMO_MAP_ID',
});
nearbySearch();
}
async function nearbySearch() {
//@ts-ignore
const { Place, SearchNearbyRankPreference } = await google.maps.importLibrary('places') as google.maps.PlacesLibrary;
const { AdvancedMarkerElement } = await google.maps.importLibrary("marker") as google.maps.MarkerLibrary;
// [START maps_place_nearby_search_request]
// Restrict within the map viewport.
let center = new google.maps.LatLng(52.369358, 4.889258);
const request = {
// required parameters
fields: ['displayName', 'location', 'businessStatus'],
locationRestriction: {
center: center,
radius: 500,
},
// optional parameters
includedPrimaryTypes: ['restaurant'],
maxResultCount: 5,
rankPreference: SearchNearbyRankPreference.POPULARITY,
language: 'en-US',
region: 'us',
};
//@ts-ignore
const { places } = await Place.searchNearby(request);
// [END maps_place_nearby_search_request]
if (places.length) {
console.log(places);
const { LatLngBounds } = await google.maps.importLibrary("core") as google.maps.CoreLibrary;
const bounds = new LatLngBounds();
// Loop through and get all the results.
places.forEach((place) => {
const markerView = new AdvancedMarkerElement({
map,
position: place.location,
title: place.displayName,
});
bounds.extend(place.location as google.maps.LatLng);
console.log(place);
});
map.fitBounds(bounds);
} else {
console.log("No results");
}
}
initMap();
// [END maps_place_nearby_search]
export { };