Skip to content

Commit c24c38c

Browse files
authored
fix: Fixes centering the map on the selected marker. (#281)
1 parent ee39d26 commit c24c38c

File tree

3 files changed

+15
-12
lines changed

3 files changed

+15
-12
lines changed

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
<script type="module" src="./index.js"></script>
1414
</head>
1515
<body>
16-
<h1>Place List Nearby Search with Google Maps</h1>
1716
<!--[START maps_ui_kit_place_search_map] -->
1817
<gmp-map center="-37.813,144.963" zoom="10" map-id="DEMO_MAP_ID">
1918
<div class="overlay" slot="control-inline-start-block-start">

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

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,24 @@ const placeDetails = document.querySelector("gmp-place-details") as any;
1212
let marker = document.querySelector('gmp-advanced-marker') as any;
1313
/* [END maps_ui_kit_place_search_query_selectors] */
1414
let markers = {};
15-
let infowindow;
15+
let infoWindow;
1616
let mapCenter;
1717

1818
async function initMap(): Promise<void> {
1919
await google.maps.importLibrary("places");
2020
const { InfoWindow } = await google.maps.importLibrary("maps") as google.maps.MapsLibrary;
2121
const { spherical } = await google.maps.importLibrary("geometry") as google.maps.GeometryLibrary;
2222

23-
infowindow = new google.maps.InfoWindow;
23+
infoWindow = new google.maps.InfoWindow;
2424

2525
function getContainingCircle(bounds) {
2626
const diameter = spherical.computeDistanceBetween(
2727
bounds.getNorthEast(),
2828
bounds.getSouthWest()
2929
);
30-
return { center: bounds.getCenter(), radius: diameter / 2 };
30+
const calculatedRadius = diameter / 2;
31+
const cappedRadius = Math.min(calculatedRadius, 50000); // Cap the radius to avoid an error.
32+
return { center: bounds.getCenter(), radius: cappedRadius };
3133
}
3234

3335
findCurrentLocation();
@@ -41,7 +43,6 @@ async function initMap(): Promise<void> {
4143
typeSelect.addEventListener("change", (event) => {
4244
// First remove all existing markers.
4345
for(marker in markers){
44-
console.log(marker);
4546
markers[marker].map = null;
4647
}
4748
markers = {};
@@ -53,7 +54,7 @@ async function initMap(): Promise<void> {
5354
),
5455
includedPrimaryTypes: [typeSelect.value],
5556
}).then(addMarkers);
56-
57+
// Handle user selection in Place Details.
5758
placeList.addEventListener("gmp-placeselect", ({ place }) => {
5859
markers[place.id].click();
5960
});
@@ -79,20 +80,20 @@ async function addMarkers(){
7980
bounds.extend(place.location);
8081

8182
marker.addListener('gmp-click',(event) => {
82-
if(infowindow.isOpen){
83-
infowindow.close();
83+
if(infoWindow.isOpen){
84+
infoWindow.close();
8485
}
8586
placeDetails.configureFromPlace(place);
8687
placeDetails.style.width = "350px";
87-
infowindow.setOptions({
88+
infoWindow.setOptions({
8889
content: placeDetails
8990
});
90-
infowindow.open({
91+
infoWindow.open({
9192
anchor: marker,
9293
map: map.innerMap
9394
});
9495
placeDetails.addEventListener('gmp-load',() => {
95-
map.innerMap.fitBounds(place.viewport, {top: placeDetails.offsetHeight || 206, left: 200});
96+
map.innerMap.fitBounds(place.viewport, { top: 400, left: 200 });
9697
});
9798

9899
});

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,12 @@ html,
2424
gmp-map {
2525
box-sizing: border-box;
2626
padding: 0 20px 20px;
27+
height: 600px;
2728
}
2829

2930
.overlay {
31+
position: relative;
32+
top: 40px;
3033
margin: 20px;
3134
width: 400px;
3235
}
@@ -55,7 +58,7 @@ html,
5558
}
5659

5760
.list-container {
58-
height: 600px;
61+
height: 400px;
5962
overflow: auto;
6063
border-radius: 10px;
6164
}

0 commit comments

Comments
 (0)