googlemaps / googlemaps/js-markerclusterer

Map zooms into incorrect place when clicking a marker cluster with map restrictions enabled

Open
#601 3 comments 7 reactions 1 assignee Claimed by @wangela View on GitHub
triage me type: bug
Dominant language
TypeScript
Stars
293
Forks
104
Avg merge
3m
Merged PRs (30d)
18

Description

I have a map with marker clustering and bounds restrictions enabled. However, when I click on the marker cluster, instead of zooming in on the cluster, it zooms in on the centre of the map. To test it I used the “official” examples for marker clustering (https://developers.google.com/maps/documentation/javascript/marker-clustering) and map restrictions (https://developers.google.com/maps/documentation/javascript/examples/control-bounds-restriction).

This uses locations in Australia and New Zealand, with a map centre of Auckland, NZ and bounds restrictions for NZ. With this example, I found the marker cluster zoom to zoom in on the wrong place initially, but it started working when I set strictBounds = true in the bounds restrictions setting.

However, when I try using locations and bounds restrictions for the US, marker cluster zoom doesn’t work correctly even when strictBounds is set to true. Below the script showing the US locations that exhibits the issue.

Interestingly, if I set MUCH wider east and west bounds in the restrictions (to the point that the bounds are pretty much pointless), then cluster zoom/centre works again. So it seems like the handling of zooming and centering on a marker cluster is inconsistent when map restrictions are enabled depending on where the cluster sits relative to the bounds restrictions.

`function initMap() {

const usa_bounds = {
north: parseFloat(49.382808),
south: parseFloat(24.521208),
west: parseFloat(-124.736342),
east: parseFloat(-55.945392),
};
const newYorkCoordinates = { lat: 40.7128, lng: 74.0060 };

const map = new google.maps.Map(document.getElementById("map"), {
zoom: 3,
center: newYorkCoordinates,
restriction: {
latLngBounds: usa_bounds,
strictBounds: true,
},
});

const infoWindow = new google.maps.InfoWindow({
content: "",
disableAutoPan: true,
});

// Create an array of alphabetical characters used to label the markers.
const labels = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
// Add some markers to the map.
const markers = locations.map((position, i) => {
const label = labels[i % labels.length];
const marker = new google.maps.Marker({
position,
label,
});

// markers can only be keyboard focusable when they have click listeners
// open info window when marker is clicked
marker.addListener("click", () => {
infoWindow.setContent(label);
infoWindow.open(map, marker);
});
return marker;
});

// Add a marker clusterer to manage the markers.
new markerClusterer.MarkerClusterer({ map, markers });

}

const locations = [
{ lat: 40.7484, lng: -73.9857 },
{ lat: 40.7594, lng: -73.9800 },
];

window.initMap = initMap;`

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.