GoogleWebComponents / google-map

Google Maps web components
https://elements.polymer-project.org/elements/google-map
Other
437 stars 257 forks source link

Multiple initially open InfoWindows are cut off with fitToMarkers #355

Open backbone87 opened 7 years ago

backbone87 commented 7 years ago

When I create a map with fitToMarkers and 2 or more markers that are initially open, the InfoWindows are cut off. This is partially related to #322

Normally if an InfoWindow is opened, the map pans to fit the InfoWindow into the map viewport. Ofc when having multiple InfoWindows opened, it is not guaranteed that each InfoWindow fits into the viewport: The panning of the map to fit the InfoWindow could cause a previous opened InfoWindow to be cut off. In practice, when dealing with very few Markers/InfoWindows (2-4 considering a big enough map), this is a non-issue.

However, when using fitToMarkers with initially open InfoWindows, the panning of the InfoWindows is overwritten, because map.fitBounds is called after the InfoWindows is opened (see _initMap). FitToMarkers only considers the markers and not the open InfoWindows attached to them. What also complicates this issue, is that the InfoWindow's size projected into LatLngBounds is dependant on the zoom ("the InfoWindow doesnt scale with the map zoom"). So just taking (top right bottom left) projected into LatLngBounds at the current zoom level and then including it into the bounds that are passed to fitBounds, doesnt work in all cases, because fitBounds may change the zoom level.

The workaround i currently use, is to attach the markers with open = false and open the InfoWindows once the map becomes idle each time after it is ready:

jQuery(function($) {
    var map = $("#myMapID");
    map.on("google-map-ready", function() {
        map.one("google-map-idle", function() {
            for(var k in this.markers) {
                this.markers[k].open = true;
            }
        });
    });
});

This works in most cases, because the map is panned a little bit upwards, when opening InfoWindows on markers near the top of the map.

frontendschlampe commented 7 years ago

Any updates or ideas for this problem?