Skip to content

Commit 8873a68

Browse files
authored
feat: Adds text search box to UI Kit text search demo. (#441)
* feat: Adds text search box to UI Kit text search demo. * Update index.html
1 parent 9103278 commit 8873a68

File tree

8 files changed

+88
-76
lines changed

8 files changed

+88
-76
lines changed

samples/ui-kit-place-details-compact/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
</gmp-map>
3636
<!--[END maps_ui_kit_place_details_compact_container]-->
3737
<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))})
38-
({key: "AIzaSyA6myHzS10YXdcazAFalmXvDkrYCp5cLc8", v: "beta"});</script>
38+
({key: "AIzaSyA6myHzS10YXdcazAFalmXvDkrYCp5cLc8", v: "weekly"});</script>
3939
</body>
4040
</html>
4141
<!--[END maps_ui_kit_place_details_compact] -->

samples/ui-kit-place-details/index.html

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,17 @@
1616
<!--[START maps_ui_kit_place_details_map] -->
1717
<gmp-map center="47.759737, -122.250632" zoom="16" map-id="DEMO_MAP_ID">
1818
<div class="widget-container" slot="control-inline-start-block-start">
19-
<gmp-place-details size="x-large"></gmp-place-details>
19+
<gmp-place-details>
20+
<gmp-place-details-place-request place="ChIJC8HakaIRkFQRiOgkgdHmqkk"></gmp-place-details-place-request>
21+
<gmp-place-all-content></gmp-place-all-content>
22+
</gmp-place-details>
23+
2024
</div>
2125
<gmp-advanced-marker></gmp-advanced-marker>
2226
</gmp-map>
2327
<!--[END maps_ui_kit_place_details_map] -->
2428
<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))})
25-
({key: "AIzaSyA6myHzS10YXdcazAFalmXvDkrYCp5cLc8", v: "alpha"});</script>
29+
({key: "AIzaSyA6myHzS10YXdcazAFalmXvDkrYCp5cLc8", v: "weekly"});</script>
2630
</body>
2731
</html>
28-
<!--[END maps_ui_kit_place_details] -->
32+
<!--[END maps_ui_kit_place_details] -->

samples/ui-kit-place-details/index.ts

Lines changed: 15 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8,61 +8,50 @@
88
/* [START maps_ui_kit_place_details_query_selector] */
99
const map = document.querySelector('gmp-map') as any;
1010
const placeDetails = document.querySelector('gmp-place-details') as any;
11+
const placeDetailsRequest = document.querySelector('gmp-place-details-place-request') as any;
1112
const marker = document.querySelector('gmp-advanced-marker') as any;
1213
/* [END maps_ui_kit_place_details_query_selector] */
1314

1415
let center = { lat: 47.759737, lng: -122.250632 };
1516

1617
async function initMap(): Promise<void> {
1718
// Request needed libraries.
18-
const { Map } = await google.maps.importLibrary("maps") as google.maps.MapsLibrary;
19-
const { AdvancedMarkerElement, PinElement } = await google.maps.importLibrary("marker") as google.maps.MarkerLibrary;
20-
const { Place } = await google.maps.importLibrary("places") as google.maps.PlacesLibrary;
19+
await google.maps.importLibrary("maps") as google.maps.MapsLibrary;
20+
await google.maps.importLibrary("marker") as google.maps.MarkerLibrary;
21+
await google.maps.importLibrary("places") as google.maps.PlacesLibrary;
2122

2223
// Hide the map type control.
2324
map.innerMap.setOptions({mapTypeControl: false});
2425

25-
// Set the default selection.
26-
const place = new Place({
27-
id: "ChIJC8HakaIRkFQRiOgkgdHmqkk",
28-
requestedLanguage: "en", // optional
26+
// Set up map once widget is loaded.
27+
placeDetails.addEventListener('gmp-load', (event) => {
28+
map.innerMap.panTo(placeDetails.place.location);
29+
map.innerMap.setZoom(16); // Set zoom after panning if needed
30+
marker.position = placeDetails.place.location;
31+
marker.style.display = 'block';
32+
marker.collisionBehavior = google.maps.CollisionBehavior.REQUIRED_AND_HIDES_OPTIONAL;
2933
});
30-
await placeDetails.configureFromPlace(place);
31-
let adjustedCenter = offsetLatLngRight(placeDetails.place.location, -0.005);
32-
map.innerMap.panTo(adjustedCenter);
33-
map.innerMap.setZoom(16);
34-
marker.position = placeDetails.place.location;
35-
marker.style.display = 'block';
36-
marker.collisionBehavior = google.maps.CollisionBehavior.REQUIRED_AND_HIDES_OPTIONAL;
3734

3835
/* [START maps_ui_kit_place_details_event] */
39-
// Add an event listener to handle map clicks.
36+
// Add an event listener to handle clicks.
4037
map.innerMap.addListener('click', async (event) => {
4138
marker.position = null;
4239
event.stop();
4340
if (event.placeId) {
4441
// Fire when the user clicks a POI.
45-
await placeDetails.configureFromPlace({id: event.placeId});
42+
placeDetailsRequest.place = event.placeId;
4643
} else {
4744
// Fire when the user clicks the map (not on a POI).
48-
await placeDetails.configureFromLocation(event.latLng);
45+
console.log('No place was selected.');
4946
}
50-
// Get the offset center.
51-
let adjustedCenter = offsetLatLngRight(placeDetails.place.location, -0.005);
5247

5348
// Show the marker at the selected place.
5449
marker.position = placeDetails.place.location;
5550
marker.style.display = 'block';
56-
map.innerMap.panTo(adjustedCenter);
51+
marker.collisionBehavior = google.maps.CollisionBehavior.REQUIRED_AND_HIDES_OPTIONAL;
5752
});
5853
}
5954
/* [END maps_ui_kit_place_details_event] */
6055

61-
// Helper function to offset the map center.
62-
function offsetLatLngRight(latLng, longitudeOffset) {
63-
const newLng = latLng.lng() + longitudeOffset;
64-
return new google.maps.LatLng(latLng.lat(), newLng);
65-
}
66-
6756
initMap();
6857
/* [END maps_ui_kit_place_details] */

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
</select>
2828
</div>
2929
<div class="list-container">
30-
<gmp-place-list selectable></gmp-place-list>
30+
<gmp-place-list selectable style="display: none;"></gmp-place-list>
3131
</div>
3232
</div>
3333
</gmp-map>

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ async function initMap(): Promise<void> {
4848
markers = {};
4949

5050
if (typeSelect.value) {
51+
placeList.style.display = "block";
5152
placeList.configureFromSearchNearbyRequest({
5253
locationRestriction: getContainingCircle(
5354
map.innerMap.getBounds()

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,15 @@
1616
<!--[START maps_ui_kit_place_search_text_map] -->
1717
<gmp-map center="37.395641,-122.077627" zoom="10" map-id="DEMO_MAP_ID">
1818
<div class="overlay" slot="control-inline-start-block-start">
19+
<div class="text-search-container">
20+
<input type="text" id="textSearchInput" placeholder="Enter text search...">
21+
<button id="textSearchButton">Search</button>
22+
</div>
1923
<div class="list-container">
20-
<gmp-place-list selectable></gmp-place-list>
24+
<gmp-place-list selectable style="display: none;"></gmp-place-list>
2125
</div>
2226
</div>
23-
<gmp-place-details size="large"></gmp-place-details>
27+
<gmp-place-details size="large" style="display: none;"></gmp-place-details>
2428
</gmp-map>
2529
<!--[END maps_ui_kit_place_search_text_map] -->
2630
<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))})

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

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ const map = document.querySelector("gmp-map") as any;
99
const placeList = document.querySelector("gmp-place-list") as any;
1010
const placeDetails = document.querySelector("gmp-place-details") as any;
1111
let marker = document.querySelector('gmp-advanced-marker') as any;
12+
const textSearchInput = document.getElementById('textSearchInput') as any;
13+
const textSearchButton = document.getElementById('textSearchButton') as HTMLButtonElement;
14+
1215
/* [END maps_ui_kit_place_search_text_query_selectors] */
1316
/* [START maps_ui_kit_place_search_text_init_map] */
1417
let markers = {};
@@ -38,17 +41,22 @@ async function initMap(): Promise<void> {
3841
clickableIcons: false,
3942
});
4043

41-
searchByTextRequest('tacos in Mountain View');
44+
textSearchButton.addEventListener('click', searchByTextRequest);
45+
textSearchInput.addEventListener('keydown', (event) => {
46+
if (event.key === 'Enter') {
47+
searchByTextRequest();
48+
}
49+
});
4250
}
4351
/* [END maps_ui_kit_place_search_text_init_map] */
4452

4553
/* [START maps_ui_kit_place_search_text_query] */
46-
async function searchByTextRequest(textQuery) {
47-
if (textQuery) {
54+
async function searchByTextRequest() {
55+
if (textSearchInput.value !== "") {
56+
placeList.style.display = "block";
4857
placeList.configureFromSearchByTextRequest({
4958
locationRestriction: bounds,
50-
includedType: "restaurant",
51-
textQuery: textQuery,
59+
textQuery: textSearchInput.value,
5260
}).then(addMarkers);
5361
// Handle user selection in Place Details.
5462
placeList.addEventListener("gmp-placeselect", ({ place }) => {
@@ -57,6 +65,7 @@ async function searchByTextRequest(textQuery) {
5765
}
5866
}
5967
/* [END maps_ui_kit_place_search_text_query] */
68+
6069
/* [START maps_ui_kit_place_search_text_add_markers] */
6170
async function addMarkers(){
6271
const { AdvancedMarkerElement } = await google.maps.importLibrary("marker") as google.maps.MarkerLibrary;
@@ -80,6 +89,7 @@ async function addMarkers(){
8089
infoWindow.close();
8190
}
8291
placeDetails.configureFromPlace(place);
92+
placeDetails.style.display = "block";
8393
placeDetails.style.width = "350px";
8494
infoWindow.setOptions({
8595
content: placeDetails

samples/ui-kit-place-search-text/style.css

Lines changed: 42 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -10,59 +10,63 @@ html,
1010
margin: 0;
1111
}
1212

13-
body {
14-
display: flex;
15-
flex-direction: column;
16-
font-family: Arial, Helvetica, sans-serif;
17-
}
18-
19-
h1 {
20-
font-size: 16px;
21-
text-align: center;
22-
}
23-
2413
gmp-map {
2514
box-sizing: border-box;
2615
height: 500px;
2716
}
2817

18+
gmp-place-list {
19+
background-color: #fff;
20+
font-size: large
21+
}
22+
2923
.overlay {
3024
position: relative;
3125
top: 20px;
3226
margin: 20px;
3327
width: 400px;
3428
}
35-
36-
.controls {
37-
display: flex;
38-
gap: 10px;
39-
margin-bottom: 10px;
40-
height: 32px;
29+
30+
.list-container {
31+
height: 400px;
32+
overflow: auto;
33+
border-radius: 10px;
4134
}
4235

43-
.search-button {
44-
background-color: #5491f5;
45-
color: #fff;
46-
border: 1px solid #ccc;
47-
border-radius: 5px;
48-
width: 100px;
49-
cursor: pointer;
36+
/* Styles for the text search container and its elements */
37+
.text-search-container {
38+
display: flex; /* Arrange input and button side-by-side */
39+
gap: 8px; /* Space between input and button */
40+
padding-bottom: 10px; /* Space below the search bar */
41+
border-bottom: 1px solid #eee; /* Separator line */
5042
}
51-
52-
.type-select {
53-
border: 1px solid #ccc;
54-
border-radius: 5px;
55-
flex-grow: 1;
56-
padding: 0 10px;
43+
44+
#textSearchInput {
45+
flex-grow: 1; /* Allow input to take available space */
46+
padding: 8px 12px;
47+
border: 1px solid #ccc;
48+
border-radius: 4px;
49+
font-size: 1rem;
5750
}
58-
59-
.list-container {
60-
height: 400px;
61-
overflow: auto;
62-
border-radius: 10px;
51+
52+
#textSearchInput:focus {
53+
outline: none;
54+
border-color: #4a80e8; /* Highlight on focus */
55+
box-shadow: 0 0 0 2px rgba(74, 128, 232, 0.2);
6356
}
64-
65-
gmp-place-list {
66-
background-color: #fff;
57+
58+
#textSearchButton {
59+
padding: 8px 15px;
60+
background-color: #4a80e8; /* Example button color */
61+
color: white;
62+
border: none;
63+
border-radius: 4px;
64+
cursor: pointer;
65+
font-size: 1rem;
66+
transition: background-color 0.2s ease-in-out;
67+
}
68+
69+
#textSearchButton:hover {
70+
background-color: #356ac0; /* Darker shade on hover */
6771
}
6872
/* [END maps_ui_kit_place_search_text] */

0 commit comments

Comments
 (0)