Skip to content

Commit 84d269a

Browse files
authored
feat: Quality and standards improvements (#676)
* feat: Updates to modern standards; adds an infowindow for a richer experience. Change-Id: I772b12f174ed365da6f159d63dd34e8ba546836e * Refactor infoWindow.open parameters Removed unnecessary parameters.
1 parent 3218483 commit 84d269a

File tree

2 files changed

+37
-24
lines changed

2 files changed

+37
-24
lines changed

samples/place-class/index.html

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@
1111

1212
<link rel="stylesheet" type="text/css" href="./style.css" />
1313
<script type="module" src="./index.js"></script>
14-
</head>
15-
<body>
16-
<div id="map"></div>
17-
1814
<!-- prettier-ignore -->
1915
<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))})
2016
({key: "AIzaSyA6myHzS10YXdcazAFalmXvDkrYCp5cLc8", v: "weekly"});</script>
17+
</head>
18+
<body>
19+
<gmp-map center="32.7360353,-117.1509849" zoom="14" map-id="DEMO_MAP_ID"><!-- Map id is required for Advanced Markers. -->
20+
</gmp-map>
2121
</body>
2222
</html>
2323
<!-- [END maps_place_class] -->

samples/place-class/index.ts

Lines changed: 33 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,15 @@
55
*/
66

77
// [START maps_place_class]
8-
let map: google.maps.Map;
9-
let centerCoordinates = { lat: 37.4161493, lng: -122.0812166 };
8+
const mapElement = document.querySelector('gmp-map') as google.maps.MapElement;
9+
let innerMap;
10+
let infoWindow;
1011

1112
async function initMap() {
12-
const { Map } = await google.maps.importLibrary("maps") as google.maps.MapsLibrary;
13-
14-
map = new Map(document.getElementById('map') as HTMLElement, {
15-
center: centerCoordinates,
16-
zoom: 14,
17-
// [START_EXCLUDE]
18-
mapId: '4504f8b37365c3d0',
19-
// [END_EXCLUDE]
20-
});
13+
const { Map, InfoWindow } = await google.maps.importLibrary("maps") as google.maps.MapsLibrary;
2114

15+
innerMap = mapElement.innerMap;
16+
infoWindow = new InfoWindow();
2217
getPlaceDetails();
2318
}
2419

@@ -28,25 +23,43 @@ async function getPlaceDetails() {
2823
const { AdvancedMarkerElement } = await google.maps.importLibrary("marker") as google.maps.MarkerLibrary;
2924
// Use place ID to create a new Place instance.
3025
const place = new Place({
31-
id: 'ChIJN5Nz71W3j4ARhx5bwpTQEGg',
32-
requestedLanguage: 'en', // optional
26+
id: 'ChIJyYB_SZVU2YARR-I1Jjf08F0', // San Diego Zoo
3327
});
3428

3529
// 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);
30+
await place.fetchFields({ fields: ['displayName', 'formattedAddress', 'location', 'googleMapsURI'] });
4131

4232
// Add an Advanced Marker
4333
const marker = new AdvancedMarkerElement({
44-
map,
34+
map: innerMap,
4535
position: place.location,
4636
title: place.displayName,
4737
});
38+
39+
// Assemble the info window content.
40+
const content = document.createElement('div');
41+
const address = document.createElement('div');
42+
const placeId = document.createElement('div');
43+
address.textContent = place.formattedAddress || '';
44+
placeId.textContent = place.id;
45+
content.append(placeId, address);
46+
47+
if (place.googleMapsURI) {
48+
const link = document.createElement('a');
49+
link.href = place.googleMapsURI;
50+
link.target = '_blank';
51+
link.textContent = 'View Details on Google Maps';
52+
content.appendChild(link);
53+
}
54+
55+
// Display an info window.
56+
infoWindow.setHeaderContent(place.displayName);
57+
infoWindow.setContent(content);
58+
infoWindow.open({
59+
anchor: marker,
60+
});
4861
}
4962
// [END maps_place_class_fetchfields]
5063

5164
initMap();
52-
// [END maps_place_class]
65+
// [END maps_place_class]

0 commit comments

Comments
 (0)