|
| 1 | +/* |
| 2 | + * @license |
| 3 | + * Copyright 2025 Google LLC. All Rights Reserved. |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | +/* [START maps_ui_kit_place_search_text_compact] */ |
| 7 | +const mapContainer = document.getElementById("map-container") as any; |
| 8 | +const placeSearch = document.querySelector("gmp-place-search") as any; |
| 9 | +const placeSearchQuery = document.querySelector("gmp-place-text-search-request") as any; |
| 10 | +const queryInput = document.querySelector(".query-input") as any; |
| 11 | +const searchButton = document.querySelector(".search-button") as any; |
| 12 | +const detailsContainer = document.getElementById("details-container") as any; |
| 13 | +const placeDetails = document.querySelector("gmp-place-details-compact") as any; |
| 14 | +const placeRequest = document.querySelector("gmp-place-details-place-request") as any; |
| 15 | +const warningMsg = document.querySelector(".warning") as any; |
| 16 | +let markers = {}; |
| 17 | +let previousSearchQuery = ''; |
| 18 | +let gMap; |
| 19 | +let placeDetailsPopup; |
| 20 | + |
| 21 | +let AdvancedMarkerElement; |
| 22 | +let LatLngBounds; |
| 23 | +let LatLng; |
| 24 | + |
| 25 | +async function init() { |
| 26 | + //@ts-ignore |
| 27 | + const {Map} = await google.maps.importLibrary("maps"); |
| 28 | + await google.maps.importLibrary("places"); |
| 29 | + //@ts-ignore |
| 30 | + ({AdvancedMarkerElement} = await google.maps.importLibrary("marker")); |
| 31 | + //@ts-ignore |
| 32 | + ({LatLngBounds, LatLng} = await google.maps.importLibrary("core")); |
| 33 | + |
| 34 | + let mapOptions = { |
| 35 | + center: {lat: 37.422, lng: -122.085}, |
| 36 | + zoom: 2, |
| 37 | + mapTypeControl: false, |
| 38 | + clickableIcons: false, |
| 39 | + mapId: '2439f449ff38ce55' |
| 40 | + }; |
| 41 | + |
| 42 | + gMap = new Map(mapContainer, mapOptions); |
| 43 | + |
| 44 | + placeDetailsPopup = new AdvancedMarkerElement({ |
| 45 | + map: null, |
| 46 | + content: placeDetails, |
| 47 | + zIndex: 100 |
| 48 | + }); |
| 49 | + |
| 50 | + findCurrentLocation(); |
| 51 | + |
| 52 | + gMap.addListener('click', (e) => { |
| 53 | + hidePlaceDetailsPopup(); |
| 54 | + }); |
| 55 | + |
| 56 | + searchButton.addEventListener("click", searchPlaces); |
| 57 | + queryInput.addEventListener("keydown", (event) => { |
| 58 | + if (event.key == 'Enter') { |
| 59 | + event.preventDefault(); |
| 60 | + searchPlaces(); |
| 61 | + } |
| 62 | + }); |
| 63 | + |
| 64 | + placeSearch.addEventListener("gmp-select", ({ place }) => { |
| 65 | + if (markers[place.id]) { |
| 66 | + markers[place.id].click(); |
| 67 | + } |
| 68 | + }); |
| 69 | +} |
| 70 | + |
| 71 | +function searchPlaces(){ |
| 72 | + if (queryInput.value.trim() === previousSearchQuery) { |
| 73 | + return; |
| 74 | + } |
| 75 | + previousSearchQuery = queryInput.value.trim(); |
| 76 | + warningMsg.style.display = 'none'; |
| 77 | + placeDetailsPopup.map = null; |
| 78 | + |
| 79 | + for(const markerId in markers){ |
| 80 | + if (Object.prototype.hasOwnProperty.call(markers, markerId)) { |
| 81 | + markers[markerId].map = null; |
| 82 | + } |
| 83 | + } |
| 84 | + markers = {}; |
| 85 | + if (queryInput.value) { |
| 86 | + mapContainer.style.height = '75vh'; |
| 87 | + placeSearch.style.display = 'block'; |
| 88 | + placeSearchQuery.textQuery = queryInput.value; |
| 89 | + placeSearchQuery.locationBias = gMap.getBounds(); |
| 90 | + placeSearch.addEventListener('gmp-load', addMarkers, { once: true }); |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +async function addMarkers(){ |
| 95 | + const bounds = new LatLngBounds(); |
| 96 | + |
| 97 | + if(placeSearch.places.length > 0){ |
| 98 | + placeSearch.places.forEach((place) => { |
| 99 | + let marker = new AdvancedMarkerElement({ |
| 100 | + map: gMap, |
| 101 | + position: place.location |
| 102 | + }); |
| 103 | + |
| 104 | + marker.metadata = {id: place.id}; |
| 105 | + markers[place.id] = marker; |
| 106 | + bounds.extend(place.location); |
| 107 | + |
| 108 | + marker.addListener('click',(event) => { |
| 109 | + placeRequest.place = place; |
| 110 | + placeDetails.style.display = 'block'; |
| 111 | + |
| 112 | + placeDetailsPopup.position = place.location; |
| 113 | + placeDetailsPopup.map = gMap; |
| 114 | + |
| 115 | + gMap.fitBounds(place.viewport, {top: 200, left: 100}); |
| 116 | + |
| 117 | + }); |
| 118 | + gMap.setCenter(bounds.getCenter()); |
| 119 | + gMap.fitBounds(bounds); |
| 120 | + }); |
| 121 | + } else { |
| 122 | + warningMsg.style.display = "block"; |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +async function findCurrentLocation(){ |
| 127 | + //@ts-ignore |
| 128 | + const { LatLng } = await google.maps.importLibrary("core"); |
| 129 | + if (navigator.geolocation) { |
| 130 | + navigator.geolocation.getCurrentPosition( |
| 131 | + (position) => { |
| 132 | + const pos = new LatLng(position.coords.latitude,position.coords.longitude); |
| 133 | + gMap.panTo(pos); |
| 134 | + gMap.setZoom(16); |
| 135 | + }, |
| 136 | + () => { |
| 137 | + console.log('The Geolocation service failed.'); |
| 138 | + gMap.setZoom(16); |
| 139 | + //@ts-ignore |
| 140 | + }, |
| 141 | + ); |
| 142 | + } else { |
| 143 | + console.log("Your browser doesn't support geolocation"); |
| 144 | + gMap.setZoom(16); |
| 145 | + } |
| 146 | + |
| 147 | +} |
| 148 | + |
| 149 | +function hidePlaceDetailsPopup() { |
| 150 | + if (placeDetailsPopup.map) { |
| 151 | + placeDetailsPopup.map = null; |
| 152 | + placeDetails.style.display = 'none'; |
| 153 | + } |
| 154 | +} |
| 155 | + |
| 156 | +init(); |
| 157 | +/* [END maps_ui_kit_place_search_text_compact] */ |
0 commit comments