@@ -11,60 +11,58 @@ const placeDetails = document.querySelector('gmp-place-details') as any;
1111const marker = document . querySelector ( 'gmp-advanced-marker' ) as any ;
1212/* [END maps_ui_kit_place_details_query_selector] */
1313
14- async function initMap ( ) : Promise < void > {
15- // Request needed libraries.
16- const { Map } = await google . maps . importLibrary ( "maps" ) as google . maps . MapsLibrary ;
17- const { AdvancedMarkerElement } = await google . maps . importLibrary ( "marker" ) as google . maps . MarkerLibrary ;
18- const { Place } = await google . maps . importLibrary ( "places" ) as google . maps . PlacesLibrary ;
14+ let center = { lat : 47.759737 , lng : - 122.250632 } ;
1915
20- // Calls the geolocation helper function to center the map on the current
21- // device location.
22- findCurrentLocation ( ) ;
16+ async function initMap ( ) : Promise < void > {
17+ // 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 ;
2321
2422 // Hide the map type control.
2523 map . innerMap . setOptions ( { mapTypeControl : false } ) ;
2624
25+ // Set the default selection.
26+ const place = new Place ( {
27+ id : "ChIJC8HakaIRkFQRiOgkgdHmqkk" ,
28+ requestedLanguage : "en" , // optional
29+ } ) ;
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+
35+ marker . position = placeDetails . place . location ;
36+ marker . style . display = 'block' ;
37+
2738 /* [START maps_ui_kit_place_details_event] */
2839 // Add an event listener to handle map clicks.
2940 map . innerMap . addListener ( 'click' , async ( event ) => {
3041 marker . position = null ;
3142 event . stop ( ) ;
32- placeDetails . style . visibility = 'visible' ;
3343 if ( event . placeId ) {
3444 // Fire when the user clicks a POI.
3545 await placeDetails . configureFromPlace ( { id : event . placeId } ) ;
3646 } else {
3747 // Fire when the user clicks the map (not on a POI).
3848 await placeDetails . configureFromLocation ( event . latLng ) ;
3949 }
50+ // Get the offset center.
51+ let adjustedCenter = offsetLatLngRight ( placeDetails . place . location , - 0.005 ) ;
52+
4053 // Show the marker at the selected place.
4154 marker . position = placeDetails . place . location ;
4255 marker . style . display = 'block' ;
56+ map . innerMap . panTo ( adjustedCenter ) ;
4357 } ) ;
4458}
45- /* [END maps_ui_kit_place_details_event] */
4659
47- // Helper function for geolocation.
48- async function findCurrentLocation ( ) {
49- const { LatLng } = await google . maps . importLibrary ( 'core' ) as google . maps . CoreLibrary ;
50- if ( navigator . geolocation ) {
51- navigator . geolocation . getCurrentPosition (
52- ( position ) => {
53- const pos =
54- new LatLng ( position . coords . latitude , position . coords . longitude ) ;
55- map . innerMap . panTo ( pos ) ;
56- map . innerMap . setZoom ( 16 ) ;
57- } ,
58- ( ) => {
59- console . log ( 'The Geolocation service failed.' ) ;
60- map . innerMap . setZoom ( 16 ) ;
61- } ,
62- ) ;
63- } else {
64- console . log ( 'Your browser doesn\'t support geolocation' ) ;
65- map . innerMap . setZoom ( 16 ) ;
66- }
60+ // Helper function to offset the map center.
61+ function offsetLatLngRight ( latLng , longitudeOffset ) {
62+ const newLng = latLng . lng ( ) + longitudeOffset ;
63+ return new google . maps . LatLng ( latLng . lat ( ) , newLng ) ;
6764}
65+ /* [END maps_ui_kit_place_details_event] */
6866
6967declare global {
7068 interface Window {
0 commit comments