Skip to content

Commit 41346d0

Browse files
committed
Update Basic Autocomplete to use gmp-map
1 parent e13a10a commit 41346d0

File tree

3 files changed

+36
-40
lines changed

3 files changed

+36
-40
lines changed

samples/place-autocomplete-basic-map/index.html

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,33 @@
1111

1212
<link rel="stylesheet" type="text/css" href="./style.css" />
1313
<script type="module" src="./index.js"></script>
14+
<!-- prettier-ignore -->
15+
<script>
16+
(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))})
17+
({key: "AIzaSyA6myHzS10YXdcazAFalmXvDkrYCp5cLc8", v: "weekly"});
18+
</script>
1419
</head>
1520
<body>
1621
<!-- [START maps_place_autocomplete_basic_map_add] -->
17-
<div id="map-container"></div>
18-
<gmp-basic-place-autocomplete
19-
slot="control-inline-start-block-start"
20-
></gmp-basic-place-autocomplete>
22+
<gmp-map zoom="12" center="37.4220656,-122.0840897" map-id="DEMO_MAP_ID">
23+
<gmp-basic-place-autocomplete
24+
slot="control-inline-start-block-start"
25+
></gmp-basic-place-autocomplete>
26+
</gmp-map>
2127
<!-- [END maps_place_autocomplete_basic_map_add] -->
22-
<gmp-place-details-compact orientation="horizontal">
28+
<gmp-place-details-compact
29+
orientation="horizontal"
30+
style="width: 400px;
31+
display: none;
32+
border: none;
33+
padding: 0;
34+
margin: 0;
35+
background-color: #ffffff;
36+
color-scheme: light;"
37+
>
2338
<gmp-place-details-place-request></gmp-place-details-place-request>
2439
<gmp-place-standard-content></gmp-place-standard-content>
2540
</gmp-place-details-compact>
26-
27-
<!-- prettier-ignore -->
28-
<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))})
29-
({key: "AIzaSyA6myHzS10YXdcazAFalmXvDkrYCp5cLc8", v: "weekly"});</script>
3041
</body>
3142
</html>
3243
<!-- [END maps_place_autocomplete_basic_map] -->

samples/place-autocomplete-basic-map/index.ts

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,50 +12,47 @@ const placeDetailsElement = document.querySelector(
1212
'gmp-place-details-compact',
1313
) as any;
1414
const placeDetailsParent = placeDetailsElement.parentElement as HTMLElement;
15-
const mapDiv = document.getElementById('map-container') as HTMLElement;
15+
const gmpMapElement = document.querySelector('gmp-map') as any;
1616
const center: google.maps.LatLngLiteral = { lat: 40.749933, lng: -73.98633 }; // New York City
1717

18-
1918
async function initMap(): Promise<void> {
2019
// Asynchronously load required libraries from the Google Maps JS API.
2120
await google.maps.importLibrary('places');
22-
const { AdvancedMarkerElement } = await google.maps.importLibrary('marker') as typeof google.maps.marker;
23-
const { Map, InfoWindow } = await google.maps.importLibrary('maps') as any;
21+
const { AdvancedMarkerElement } = await google.maps.importLibrary('marker') as google.maps.MarkerLibrary;
22+
const { InfoWindow } = await google.maps.importLibrary('maps') as google.maps.MapsLibrary;
2423

2524
// Set the initial location bias for the autocomplete element.
2625
placeAutocompleteElement.locationBias = center;
2726

2827
// Create the map object with specified options.
29-
const map: google.maps.Map = new Map(mapDiv, {
30-
zoom: 12,
31-
center: center,
32-
mapId: 'DEMO_MAP_ID',
33-
clickableIcons: false,
34-
mapTypeControl: false,
35-
streetViewControl: false,
36-
});
28+
const map = gmpMapElement.innerMap;
29+
map.setOptions({
30+
clickableIcons: false,
31+
mapTypeControl: false,
32+
streetViewControl: false
33+
});
3734

3835
// Create an advanced marker to show the location of a selected place.
3936
const advancedMarkerElement: google.maps.marker.AdvancedMarkerElement = new AdvancedMarkerElement({
4037
map: map,
38+
collisionBehavior: google.maps.CollisionBehavior.REQUIRED_AND_HIDES_OPTIONAL,
4139
});
4240

4341
// Create an InfoWindow to hold the place details component.
4442
const infoWindow: google.maps.InfoWindow = new InfoWindow({
4543
minWidth: 360,
4644
disableAutoPan: true,
47-
closeButton: false,
4845
headerDisabled: true,
4946
pixelOffset: new google.maps.Size(0, -10),
5047
});
5148

52-
5349
// [START maps_place_autocomplete_basic_map_listener]
5450
// Event listener for when a place is selected from the autocomplete list.
5551
placeAutocompleteElement.addEventListener('gmp-select', (event: any) => {
5652

5753
// Reset marker and InfoWindow, and prepare the details element.
5854
placeDetailsParent.appendChild(placeDetailsElement);
55+
placeDetailsElement.style.display = "block";
5956
advancedMarkerElement.position = null;
6057
infoWindow.close();
6158

@@ -70,6 +67,7 @@ async function initMap(): Promise<void> {
7067
// Event listener for when the place details have finished loading.
7168
placeDetailsElement.addEventListener('gmp-load', () => {
7269
const location = placeDetailsElement.place.location as google.maps.LatLng;
70+
7371

7472
// Position the marker and open the InfoWindow at the place's location.
7573
advancedMarkerElement.position = location;
@@ -79,7 +77,6 @@ async function initMap(): Promise<void> {
7977
anchor: advancedMarkerElement,
8078
});
8179
map.setCenter(location);
82-
placeDetailsElement.focus();
8380
});
8481

8582
// Event listener to close the InfoWindow when the map is clicked.

samples/place-autocomplete-basic-map/style.css

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,38 +3,26 @@
33
* Copyright 2025 Google LLC. All Rights Reserved.
44
* SPDX-License-Identifier: Apache-2.0
55
*/
6-
/* [START maps_place_autocomplete_basic_map] */
6+
/* [START maps_place_autocomplete_basic_map] */
77
html,
88
body {
99
height: 100%;
1010
margin: 0;
1111
padding: 0;
1212
}
1313

14-
#map-container {
14+
gmp-map {
1515
height: 100%;
1616
}
1717

1818
gmp-basic-place-autocomplete {
1919
position: absolute;
20-
height: 50px;
20+
height: 30px;
2121
width: 500px;
2222
top: 10px;
2323
left: 10px;
24-
z-index: 1;
25-
box-shadow: 2px 2px 5px 0px rgba(0, 0, 0, 0. 2);
24+
box-shadow: 4px 4px 5px 0px rgba(0, 0, 0, 0.2);
2625
color-scheme: light;
2726
border-radius: 10px;
2827
}
29-
30-
gmp-place-details-compact {
31-
width: 360px;
32-
min-width: 300px;
33-
max-height: 300px;
34-
border: none;
35-
background-color: #ffffff;
36-
color-scheme: light;
37-
}
38-
39-
4028
/* [END maps_place_autocomplete_basic_map] */

0 commit comments

Comments
 (0)