Skip to content

Commit 469e679

Browse files
authored
fix: Adds logic for recentering map; changes PDE compact to weekly. (#445)
1 parent 79d8a0b commit 469e679

File tree

2 files changed

+22
-11
lines changed

2 files changed

+22
-11
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.ts

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,21 @@ async function initMap(): Promise<void> {
2323
// Hide the map type control.
2424
map.innerMap.setOptions({mapTypeControl: false});
2525

26+
// Function to update map and marker based on place details
27+
const updateMapAndMarker = () => {
28+
if (placeDetails.place && placeDetails.place.location) {
29+
let adjustedCenter = offsetLatLngRight(placeDetails.place.location, -0.005);
30+
map.innerMap.panTo(adjustedCenter);
31+
map.innerMap.setZoom(16); // Set zoom after panning if needed
32+
marker.position = placeDetails.place.location;
33+
marker.collisionBehavior = google.maps.CollisionBehavior.REQUIRED_AND_HIDES_OPTIONAL;
34+
marker.style.display = 'block';
35+
}
36+
};
37+
2638
// Set up map once widget is loaded.
2739
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;
40+
updateMapAndMarker();
3341
});
3442

3543
/* [START maps_ui_kit_place_details_event] */
@@ -40,18 +48,21 @@ async function initMap(): Promise<void> {
4048
if (event.placeId) {
4149
// Fire when the user clicks a POI.
4250
placeDetailsRequest.place = event.placeId;
51+
updateMapAndMarker();
4352
} else {
4453
// Fire when the user clicks the map (not on a POI).
4554
console.log('No place was selected.');
55+
marker.style.display = 'none';
4656
}
47-
48-
// Show the marker at the selected place.
49-
marker.position = placeDetails.place.location;
50-
marker.style.display = 'block';
51-
marker.collisionBehavior = google.maps.CollisionBehavior.REQUIRED_AND_HIDES_OPTIONAL;
5257
});
5358
}
5459
/* [END maps_ui_kit_place_details_event] */
5560

61+
// Helper function to offset marker placement for better visual appearance.
62+
function offsetLatLngRight(latLng, longitudeOffset) {
63+
const newLng = latLng.lng() + longitudeOffset;
64+
return new google.maps.LatLng(latLng.lat(), newLng);
65+
}
66+
5667
initMap();
5768
/* [END maps_ui_kit_place_details] */

0 commit comments

Comments
 (0)