Google maps infowindow scrolling bug: How to solve all cases?

This is a known bug that the Google Maps API shows the scroll bar for the first (time) click on the info window,

The problem, the first time:

enter image description here

Other time periods:

enter image description here

So, I found out that adding maxWidth solves the problem,

This is not so for me; If I set maxWidth to 200px, the scrollbar will disappear, but its smaller than I need,

enter image description here

If I set 250px (the size I need), the scrollbar is saved

enter image description here

Any idea what I can try?

jsfiddle: http://jsfiddle.net/e0x20tvs/3/

 $sescam_ventana = { init: function() { this.mapa(); }, mapa: function() { var script = document.createElement('script'); script.type = 'text/javascript'; script.src = '//maps.googleapis.com/maps/api/js?v=3.exp&sensor=false&callback=$sescam_ventana.mapSetup'; document.body.appendChild(script); }, mapSetup: function() { var styles = [ /* { stylers: [ { saturation: -100 } ] },{ featureType: "road", elementType: "geometry", stylers: [ { visibility: "simplified" } ] },{ featureType: "poi.business", elementType: "labels", stylers: [ { visibility: "simplified" } ] } */ ]; var styledMap = new google.maps.StyledMapType(styles, { name: "SESCAM" }); // var mapOptions = { zoom: 16, scrollwheel: false, center: new google.maps.LatLng(39.6177074, 4.9725879), mapTypeControlOptions: { mapTypeIds: [google.maps.MapTypeId.ROADMAP, 'map_style'] } } var handleMarkerClick = function(marker, index) { var $navigationMenu = jQuery('<div>'); jQuery('.menu-item').each(function(i) { var $button = jQuery('<button>'); $button.text(jQuery(this).text()); $button.addClass(jQuery(this).data('class')); $navigationMenu.append($button); }); if (typeof infowindow === 'undefined') { infowindow = new google.maps.InfoWindow({ height: 380 }); } var centro = infoCentros[index] //helpful data infowindow.setContent( '<div class="sescam-info-window">' + '<h3>' + centro.nombre + '</h3>' + '<p>' + centro.lugar + '</h3>' + '<p class="titulo">Coordinador</p>' + '<p><strong>' + centro.coordinador.nombre + '</strong></p>' + '<p><a href="mailto:' + centro.coordinador.email + '">' + centro.coordinador.email + '</a></p>' + '<p class="titulo">Responsable</p>' + '<p><strong>' + centro.responsable.nombre + '</strong></p>' + '<p><a href="mailto:' + centro.responsable.email + '">' + centro.responsable.email + '</a></p>' + '<div class="menu">' + $navigationMenu.html() + '</div>' + '</div>' ); infowindow.open(marker.getMap(), marker); } var handleMenu = function() { jQuery('body').on('click', '.menu button', function() { var itemClass = jQuery(this).attr('class'); jQuery('.listado.' + itemClass).stop(true, true).slideDown().siblings('.listado').stop(true, true).slideUp(); }); } this.gmap = new google.maps.Map(document.getElementById('mapa'), mapOptions); this.gmap.mapTypes.set('map_style', styledMap); this.gmap.setMapTypeId('map_style'); this.mapMarkers = []; this.mapInfoWindow = new google.maps.InfoWindow({ height: 380 }); jQuery(window).resize(function() { $sescam_ventana.gmap.fitBounds($sescam_ventana.mapBounds); }); for (var i = 0; i < this.mapMarkers.length; i++) { // primero eliminamos todos los markers que pudiera haber de una visualización anterior this.mapMarkers[i].setMap(null); } this.mapMarkers.length = 0; // reseteamos el array this.mapBounds = new google.maps.LatLngBounds(); var c = 0; // de inicio no sabemos cuantos elementos tienen realmente latitud y longitud, este contador nos lo chivará var latlon; // lo guardamos fuera del each para poder verlo después, si resulta que es el único elemeto a mostrar for (var i = 0; i < infoCentros.length; i++) { var centro = infoCentros[i]; var lat = centro.cordenadas.lat; var lon = centro.cordenadas.long; if (lat && lon) { c++; latlon = new google.maps.LatLng(lat, lon); var moptions = { position: latlon, map: $sescam_ventana.gmap } moptions.icon = 'http://icons.iconarchive.com/icons/icons-land/vista-map-markers/256/Map-Marker-Marker-Outside-Pink-icon.png'; var marker = new google.maps.Marker(moptions); $sescam_ventana.mapMarkers.push(marker); //google.maps.event.addListener(marker, 'click', handleMarkerClick); google.maps.event.addListener(marker, 'click', handleMarkerClick.bind(undefined, marker, i)); $sescam_ventana.mapBounds.extend(latlon); } } if (c > 1) { $sescam_ventana.gmap.fitBounds(this.mapBounds); } else { // si solo hay uno el fitbounds no hace un zoom ni un centrado correctos $sescam_ventana.gmap.setCenter(latlon); } handleMenu(); } } jQuery(function() { $sescam_ventana.init(); }); 
 .gmapa { position: relative; padding-bottom: 50%; } #mapa { position: absolute; left: 0; top: 0; width: 100%; height: 100%; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="gmapa"> <script> var infoCentros = [{ "nombre": "El nombre 0", "texto": "Lorem ipsum sit met hamet amid0", "lugar": "Toledo", "coordinador": { "nombre": "Ana M\u00aa del Mar Alonso Fern\u00e1ndez", "email": " anapilar@gmail.com " }, "responsable": { "nombre": "Ana M\u00aa del Mar Alonso Fern\u00e1ndez", "email": " anapilar@gmail.com " }, "cordenadas": { "lat": 39.150544196, "long": -4.8019412125 } }, { "nombre": "El nombre 1", "texto": "Lorem ipsum sit met hamet amid1", "lugar": "Toledo", "coordinador": { "nombre": "Ana M\u00aa del Mar Alonso Fern\u00e1ndez", "email": " anapilar@gmail.com " }, "responsable": { "nombre": "Ana M\u00aa del Mar Alonso Fern\u00e1ndez", "email": " anapilar@gmail.com " }, "cordenadas": { "lat": 39.150544196, "long": -4.7019412125 } }, { "nombre": "El nombre 2", "texto": "Lorem ipsum sit met hamet amid2", "lugar": "Toledo", "coordinador": { "nombre": "Ana M\u00aa del Mar Alonso Fern\u00e1ndez", "email": " anapilar@gmail.com " }, "responsable": { "nombre": "Ana M\u00aa del Mar Alonso Fern\u00e1ndez", "email": " anapilar@gmail.com " }, "cordenadas": { "lat": 39.150544196, "long": -4.6019412125 } }, { "nombre": "El nombre 3", "texto": "Lorem ipsum sit met hamet amid3", "lugar": "Toledo", "coordinador": { "nombre": "Ana M\u00aa del Mar Alonso Fern\u00e1ndez", "email": " anapilar@gmail.com " }, "responsable": { "nombre": "Ana M\u00aa del Mar Alonso Fern\u00e1ndez", "email": " anapilar@gmail.com " }, "cordenadas": { "lat": 39.150544196, "long": -4.5019412125 } }, { "nombre": "El nombre 4", "texto": "Lorem ipsum sit met hamet amid4", "lugar": "Toledo", "coordinador": { "nombre": "Ana M\u00aa del Mar Alonso Fern\u00e1ndez", "email": " anapilar@gmail.com " }, "responsable": { "nombre": "Ana M\u00aa del Mar Alonso Fern\u00e1ndez", "email": " anapilar@gmail.com " }, "cordenadas": { "lat": 39.150544196, "long": -4.4019412125 } }]; </script> <div id="mapa"></div> </div> 
+7
javascript html css google-maps
source share
3 answers

EDIT

In your specific case, this is related to the Roboto web font. It is included in the Google Maps API, but nothing uses it at that time on your page until InfoWindow opens, so the browser has no reason to download it, and no.

However, when you open the info window, the browser at this point understands that it needs a font and starts to download it, but google maps measured the infoWindow size before loading the font (see the original answer on how the Google Maps api measures the infoWindow size) . When the font has finished loading, the content in infoWindow will be redrawn in the Roboto font, which actually makes infoWindow different (larger) than the size that Google measured so that it was before the font was loaded, and you will see scroll bars on it .

This also explains why you see scrollbars for the first time - when Google maps measured the info window before the font was loaded, but you won’t see them because the font is already loaded, any dimensions that will be displayed on the maps Google will now be correct.

So the solution is

a) Change something in the Roboto font on your page (forcing the browser to download this font) before the first OR info window opens

b) Use a different font (one that is used elsewhere on your page and thus loads when the page loads) for your infoWindow content.

I will leave the rest of my answer because it is a common misunderstanding of how infoWindows work, and as you pointed out in your Google search, a lot of people have hung it.

TL; DR:

Never use a parent selector (like # map-canvas) to style the HTML content of your info window.

It's not a mistake. Here's how google map info window styles work:

When you tell the Google Maps API you want to open infoWindow with HTML content, Google dynamically creates a div element, adds it to its body tag, captures the dimensions of the dynamically created div, and then adds the div with these dimensions to the map, which is your information.

This is where the trick comes into play.

Let's say this is your HTML:

 <div id="map-canvas"> </div> 

and this is your infoWindow content:

 <h1>I'm an infoWindow</h1> <p>Hi there!</p> 

and here is your CSS:

 h1 { font-size: 18px; } #map-canvas h1 { font-size: 24px; } 

Google almost always incorrectly draws this info-indicator, because when it dynamically creates a info-inversion of a div and adds it to the body to receive dimensions, the div will have h1 font size 18px. So now Google has measurements and places this div on the map, after which the font size h1 increases to 24px , so now Google uses the wrong measurements, and your information will be completed with scrollbars.

I always found the easiest way to work with infoWindow CSS is to use a div wrapper and always customize it, so you will have the HTML content of infoWindow:

 <div class="info-window-content"> <h1>I'm an infoWindow</h1> <p>Hi there!</p> </div> 

And then your CSS might look like this:

 /* global styles */ h1 { font-size: 18px; } p { line-height: 1.6; } /* info window styles */ .info-window-content h1 { font-size: 24px; } .info-window-content p { line-height: 1.2; } 

and you won’t fall into the scroll bar in your info window.

+4
source share

I have such an erorra as you have I used this solution

.gm-style div div div div {max-width: 250px! important;}

.sescam-info-window {position: absolute; overflow: hidden}

this is my mp view check more use check item good luck

http://www.lankabird.com/index.php/virtualtravelagent/map

0
source share

Edit: ok what was needed was the parent of .info-window-content.

So the only thing I did was add

 .gm-style-iw>div { overflow: hidden !important; } 

I made a fork of your message. http://jsfiddle.net/mygt82wp/ . It works?

I think this may be a universal solution for this error in Google Maps, I think this should work for every infoWindow.


Old post:

I think the simplest and most effective thing you can do is just 1 line of css:

 .info-window-content {overflow: hidden} 

Perhaps do this {overflow: hidden! important} if necessary

-2
source share

All Articles