Skip to content

Commit 1018750

Browse files
committed
feat: Adds an infowindow for a richer experience.
1 parent 82502b3 commit 1018750

File tree

1 file changed

+21
-13
lines changed

1 file changed

+21
-13
lines changed

samples/place-class/index.ts

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,19 @@
66

77
// [START maps_place_class]
88
let map: google.maps.Map;
9-
let centerCoordinates = { lat: 37.4161493, lng: -122.0812166 };
9+
let infoWindow;
10+
const center = { lat: 32.7360353, lng: -117.1509849 }; // San Diego Zoo
1011

1112
async function initMap() {
12-
const { Map } = await google.maps.importLibrary("maps") as google.maps.MapsLibrary;
13+
const { Map, InfoWindow } = await google.maps.importLibrary("maps") as google.maps.MapsLibrary;
1314

1415
map = new Map(document.getElementById('map') as HTMLElement, {
15-
center: centerCoordinates,
16+
center: center,
1617
zoom: 14,
17-
// [START_EXCLUDE]
18-
mapId: '4504f8b37365c3d0',
19-
// [END_EXCLUDE]
18+
mapId: 'DEMO_MAP_ID',
2019
});
2120

21+
infoWindow = new InfoWindow();
2222
getPlaceDetails();
2323
}
2424

@@ -28,23 +28,31 @@ async function getPlaceDetails() {
2828
const { AdvancedMarkerElement } = await google.maps.importLibrary("marker") as google.maps.MarkerLibrary;
2929
// Use place ID to create a new Place instance.
3030
const place = new Place({
31-
id: 'ChIJN5Nz71W3j4ARhx5bwpTQEGg',
32-
requestedLanguage: 'en', // optional
31+
id: 'ChIJyYB_SZVU2YARR-I1Jjf08F0', // San Diego Zoo
3332
});
3433

3534
// Call fetchFields, passing the desired data fields.
36-
await place.fetchFields({ fields: ['displayName', 'formattedAddress', 'location'] });
37-
38-
// Log the result
39-
console.log(place.displayName);
40-
console.log(place.formattedAddress);
35+
await place.fetchFields({ fields: ['displayName', 'formattedAddress', 'location', 'googleMapsLinks'] });
4136

4237
// Add an Advanced Marker
4338
const marker = new AdvancedMarkerElement({
4439
map,
4540
position: place.location,
4641
title: place.displayName,
4742
});
43+
44+
// Assemble the info window content.
45+
//@ts-ignore
46+
const content = `${place.formattedAddress}<br/>${place.id}<br/><a href="${place.googleMapsLinks.placeURI}">View Details on Google Maps</a>`;
47+
48+
// Display an info window.
49+
infoWindow.setHeaderContent(place.displayName);
50+
infoWindow.setContent(content);
51+
infoWindow.open({
52+
map,
53+
anchor: marker,
54+
shouldFocus: false,
55+
});
4856
}
4957
// [END maps_place_class_fetchfields]
5058

0 commit comments

Comments
 (0)