Skip to content

Commit df753dc

Browse files
authored
fix: Updates Place Search text demo. (#449)
* fix: Updates Place Search text demo. * Update index.html Removes size attribute from gmp-place-details element, as it is not needed.
1 parent 00ab94e commit df753dc

File tree

2 files changed

+24
-13
lines changed

2 files changed

+24
-13
lines changed

samples/ui-kit-place-search-text/index.html

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,14 @@
2424
<gmp-place-list selectable style="display: none;"></gmp-place-list>
2525
</div>
2626
</div>
27-
<gmp-place-details size="large" style="display: none;"></gmp-place-details>
27+
<gmp-place-details style="display: none;">
28+
<gmp-place-details-place-request></gmp-place-details-place-request>
29+
<gmp-place-all-content></gmp-place-all-content>
30+
</gmp-place-details>
2831
</gmp-map>
2932
<!--[END maps_ui_kit_place_search_text_map] -->
3033
<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))})
3134
({key: "AIzaSyA6myHzS10YXdcazAFalmXvDkrYCp5cLc8", v: "alpha"});</script>
3235
</body>
3336
</html>
34-
<!--[END maps_ui_kit_place_search_text] -->
37+
<!--[END maps_ui_kit_place_search_text] -->

samples/ui-kit-place-search-text/index.ts

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,13 @@ const placeDetails = document.querySelector("gmp-place-details") as any;
1111
let marker = document.querySelector('gmp-advanced-marker') as any;
1212
const textSearchInput = document.getElementById('textSearchInput') as any;
1313
const textSearchButton = document.getElementById('textSearchButton') as HTMLButtonElement;
14+
const placeDetailsRequest = document.querySelector('gmp-place-details-place-request') as any;
1415

1516
/* [END maps_ui_kit_place_search_text_query_selectors] */
1617
/* [START maps_ui_kit_place_search_text_init_map] */
1718
let markers = {};
1819
let infoWindow;
19-
let center = { lat: 37.395641, lng: -122.077627 };
20+
let center = { lat: 37.395641, lng: -122.077627 }; // Mountain View, CA.
2021
let bounds;
2122

2223
async function initMap(): Promise<void> {
@@ -32,21 +33,31 @@ async function initMap(): Promise<void> {
3233
infoWindow = new google.maps.InfoWindow;
3334

3435
// Center the map
35-
let adjustedCenter = offsetLatLngRight(center, -0.005);
36-
map.innerMap.panTo(adjustedCenter);
36+
map.innerMap.panTo(center);
3737
map.innerMap.setZoom(14);
3838

3939
map.innerMap.setOptions({
4040
mapTypeControl: false,
4141
clickableIcons: false,
4242
});
4343

44+
/* [START maps_ui_kit_place_search_tex_event_handlers] */
45+
// Fire when the Place Details Element is loaded.
46+
placeDetails.addEventListener('gmp-load', (event) => {
47+
// Center the info window on the map.
48+
map.innerMap.fitBounds(placeDetails.place.viewport, { top: 500, left: 400 });
49+
});
50+
51+
// Handle clicks on the search button.
4452
textSearchButton.addEventListener('click', searchByTextRequest);
53+
54+
// Handle enter key on text input.
4555
textSearchInput.addEventListener('keydown', (event) => {
4656
if (event.key === 'Enter') {
4757
searchByTextRequest();
4858
}
4959
});
60+
/* [END maps_ui_kit_place_search_tex_event_handlers] */
5061
}
5162
/* [END maps_ui_kit_place_search_text_init_map] */
5263

@@ -83,12 +94,14 @@ async function addMarkers(){
8394
markers[place.id] = marker;
8495
bounds.extend(place.location);
8596
marker.collisionBehavior = google.maps.CollisionBehavior.REQUIRED_AND_HIDES_OPTIONAL;
86-
97+
98+
/* [START maps_ui_kit_place_search_text_click_event] */
8799
marker.addListener('gmp-click',(event) => {
88100
if(infoWindow.isOpen){
89101
infoWindow.close();
90102
}
91-
placeDetails.configureFromPlace(place);
103+
// Set the Place Details request to the selected place.
104+
placeDetailsRequest.place = place.id;
92105
placeDetails.style.display = "block";
93106
placeDetails.style.width = "350px";
94107
infoWindow.setOptions({
@@ -103,18 +116,13 @@ async function addMarkers(){
103116
});
104117

105118
});
119+
/* [END maps_ui_kit_place_search_text_click_event] */
106120
map.innerMap.setCenter(bounds.getCenter());
107121
map.innerMap.fitBounds(bounds);
108122
});
109123
}
110124
}
111125
/* [END maps_ui_kit_place_search_text_add_markers] */
112126

113-
// Helper function to offset the map center.
114-
function offsetLatLngRight(latLng, longitudeOffset) {
115-
const newLng = latLng.lng + longitudeOffset;
116-
return new google.maps.LatLng(latLng.lat, newLng);
117-
}
118-
119127
initMap();
120128
/* [END maps_ui_kit_place_search_text] */

0 commit comments

Comments
 (0)