Skip to content

Commit 9221fe5

Browse files
authored
feat: Updates Place Autocomplete examples for GA launch. (#273)
1 parent e67e78b commit 9221fe5

File tree

4 files changed

+9
-7
lines changed

4 files changed

+9
-7
lines changed

samples/place-autocomplete-element/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
<!-- prettier-ignore -->
1919
<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))})
20-
({key: "AIzaSyA6myHzS10YXdcazAFalmXvDkrYCp5cLc8", v: "beta"});</script>
20+
({key: "AIzaSyA6myHzS10YXdcazAFalmXvDkrYCp5cLc8", v: "weekly"});</script>
2121
</body>
2222
</html>
2323
<!-- [END maps_place_autocomplete_element] -->

samples/place-autocomplete-element/index.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
async function initMap(): Promise<void> {
1919
// [START maps_place_autocomplete_element_add]
2020
// Request needed libraries.
21-
//@ts-ignore
2221
await google.maps.importLibrary("places") as google.maps.PlacesLibrary;
2322
// Create the input HTML element, and append it.
2423
//@ts-ignore
@@ -39,9 +38,9 @@ async function initMap(): Promise<void> {
3938
// [START maps_place_autocomplete_element_listener]
4039
// Add the gmp-placeselect listener, and display the results.
4140
//@ts-ignore
42-
placeAutocomplete.addEventListener('gmp-placeselect', async ({ place }) => { // TODO: gmp-placeselect -> gmp-select; place -> placePrediction.
41+
placeAutocomplete.addEventListener('gmp-select', async ({ placePrediction }) => {
42+
const place = placePrediction.toPlace();
4343
await place.fetchFields({ fields: ['displayName', 'formattedAddress', 'location'] });
44-
4544
selectedPlaceTitle.textContent = 'Selected Place:';
4645
selectedPlaceInfo.textContent = JSON.stringify(
4746
place.toJSON(), /* replacer */ null, /* space */ 2);

samples/place-autocomplete-map/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
<!-- prettier-ignore -->
2222
<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))})
23-
({key: "AIzaSyA6myHzS10YXdcazAFalmXvDkrYCp5cLc8", v: "beta"});</script>
23+
({key: "AIzaSyA6myHzS10YXdcazAFalmXvDkrYCp5cLc8", v: "weekly"});</script>
2424
</body>
2525
</html>
2626
<!-- [END maps_place_autocomplete_map] -->

samples/place-autocomplete-map/index.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
let map: google.maps.Map;
1919
let marker: google.maps.marker.AdvancedMarkerElement;
2020
let infoWindow: google.maps.InfoWindow;
21+
let center = { lat: 40.749933, lng: -73.98633 }; // New York City
2122
async function initMap(): Promise<void> {
2223
// Request needed libraries.
2324
//@ts-ignore
@@ -28,7 +29,7 @@ async function initMap(): Promise<void> {
2829

2930
// Initialize the map.
3031
map = new google.maps.Map(document.getElementById('map') as HTMLElement, {
31-
center: { lat: 40.749933, lng: -73.98633 },
32+
center,
3233
zoom: 13,
3334
mapId: '4504f8b37365c3d0',
3435
mapTypeControl: false,
@@ -38,6 +39,7 @@ async function initMap(): Promise<void> {
3839
const placeAutocomplete = new google.maps.places.PlaceAutocompleteElement();
3940
//@ts-ignore
4041
placeAutocomplete.id = 'place-autocomplete-input';
42+
placeAutocomplete.locationBias = center;
4143

4244
const card = document.getElementById('place-autocomplete-card') as HTMLElement;
4345
//@ts-ignore
@@ -55,7 +57,8 @@ async function initMap(): Promise<void> {
5557
// [START maps_place_autocomplete_map_listener]
5658
// Add the gmp-placeselect listener, and display the results on the map.
5759
//@ts-ignore
58-
placeAutocomplete.addEventListener('gmp-placeselect', async ({ place }) => { // TODO: gmp-placeselect -> gmp-select; place -> placePrediction.
60+
placeAutocomplete.addEventListener('gmp-select', async ({ placePrediction }) => {
61+
const place = placePrediction.toPlace();
5962
await place.fetchFields({ fields: ['displayName', 'formattedAddress', 'location'] });
6063

6164
// If the place has a geometry, then present it on a map.

0 commit comments

Comments
 (0)