The width of the Google Maps InfoWindow map is overwritten even if it is installed correctly.

I am using the Google Maps API and I have some problems about InfoWindow .

Here is a summary:

  • I load InfoWindow content when a user clicks on a marker
  • Content is a partial view loaded thanks to an Ajax call
  • In the .done callback, I call an asynchronous method that will insert data into the contents of InfoWindow. I need to do this because I want the main InfoWindow content to be displayed immediately, while this โ€œbonusโ€ information can be displayed in a few tenths of a second.

This works great; but I have a white bar to the right of my InfoWindow that I canโ€™t remove (see image below)

However, content loading is included in the <div> with a fixed width :

 <div id="div-main-infoWindow"> <!-- All my content --> </div> 

And in my CSS I wrote:

 #div-main-infoWindow { width:342px !important; } 


Downloading InfoWindow with more details is as follows:

 $.ajax({ url : "/my-url", async : false, contentType : "application/json; charset=utf-8", dataType : "json", type : "POST", data : JSON.stringify(myModel) }).done(function(response) { MarkerContent = response.Content; //The HTML content to display MyAsyncMethod().then(function(response) { //do some formatting with response return result; //result is just a small HTML string }).done(function(result1, result2) { //do some formatting with result1 and result2 $("#myNode").html(/*formatted result*/); }) //info is a global variable defined as new google.maps.InfoWindow() info.setOptions({ content: MarkerContent, maxWidth: 342 //To be sure }); info.open(map, marker); }); }); 

The content is completely in order, the problem is in this white bar.

Looking at my browser console (I played it in all browsers), I see this:

Iw

As you can see, my <div> containing all my data downloaded from my ajax call is in good size order (green rectangle corresponding to the green zone in the screenshot), BUT the above divs (from the Google API itself, into red rectangles) have a larger size from which the problem.

The only way I found is to run this jQuery script modifying the internal structure of InfoWindow:

 $(".gm-style-iw").next().remove(); $(".gm-style-iw").prev().children().last().width(342); $(".gm-style-iw").prev().children(":nth-child(2)").width(342); $(".gm-style-iw").width(342); $(".gm-style-iw").children().first().css("overflow", "hidden"); $(".gm-style-iw").children().first().children().first().css("overflow", "hidden"); $(".gm-style-iw").parent().width(342); 

Note : gm-style-iw is the class name given to Google by a div containing all the InfoWindow content that was drawn in the screenshot above. I also add this rule to my CSS:

 .gm-style-iw { width: 342px !important; //also tried with 100% !important, not better } 

It works in the console, however, it has no effect if it is written in the code itself, in the .done or in the domready Google Maps event ...
However, in this late case, if I encapsulate the above jQuery script in setTimeout() , it works !! I commented on the asynchronous method, so it is not to blame, but it seems that domready is domready executed, while 100% InfoWindow is not yet displayed - contrary to the document .

I also tried moving my info.setOptions outside the .done and putting it after it, with no effect.

So, how can I display a โ€œnormalโ€ InfoWindow without this white bar on the right?
I do not want to implement InfoBubble or another custom InfoWindow library. This is a personal project, and I want to understand why and where the problem is. And of course, find a solution.

Thank you for your help!

+5
source share
2 answers

This is a little harder than you think.

Just a few things:

  • Did you notice that there is a close button? Even when you delete the button, the space will be there because the API calculates the size of other nodes based on that space.
  • the tip should be in the center
  • there are additional containers for rounded borders and shadows
  • the size of infoWindow will be calculated so that it fits into the viewport
  • contents need to be scrolled if necessary
  • the position of the infoindust should be established (therefore, it needs to calculate the exact height of the information window)

Basically: all these things require calculating the exact sizes and positions, most nodes in the info installation are absolute positions, but rather a method like this would be used in DTP than you would use it in an HTML document.

Extras: InfoWindows will change very often to fix errors, a solution that works today may be broken tomorrow.

However, the approach that currently works for me:

  • set the maxWidth value in the infoindustrator to the desired width - 51 (in this case, there will be 291)
  • It is not possible to apply !important -rules through $.css , so you need to do this directly using the stylesheet. To be able to access all the elements, set the new class for the root node in infowindow (note that there may be info windows that you cannot control, for example, for POIs):

     google.maps.event.addListener(infowindow,'domready',function(){ $('#div-main-infoWindow')//the root of the content .closest('.gm-style-iw') .parent().addClass('custom-iw'); }); 
  • CSS:

  .custom-iw .gm-style-iw { top:15px !important; left:0 !important; border-radius:2px; } .custom-iw>div:first-child>div:nth-child(2) { display:none; } /** the shadow **/ .custom-iw>div:first-child>div:last-child { left:0 !important; top:0px; box-shadow: rgba(0, 0, 0, 0.6) 0px 1px 6px; z-index:-1 !important; } .custom-iw .gm-style-iw, .custom-iw .gm-style-iw>div, .custom-iw .gm-style-iw>div>div { width:100% !important; max-width:100% !important; } /** set here the width **/ .custom-iw, .custom-iw>div:first-child>div:last-child { width:342px !important; } /** set here the desired background-color **/ #div-main-infoWindow, .custom-iw>div:first-child>div:nth-child(n-1)>div>div, .custom-iw>div>div:last-child, .custom-iw .gm-style-iw, .custom-iw .gm-style-iw>div, .custom-iw .gm-style-iw>div>div { background-color:orange !important; } /** close-button(note that there may be a scrollbar) **/ .custom-iw>div:last-child { top:1px !important; right:0 !important; } /** padding of the content **/ #div-main-infoWindow { padding:6px; } 

Demo (as I said, may be broken tomorrow): http://jsfiddle.net/doktormolle/k57qojq7/

+10
source

A little late for the party here, but after searching for ages to get the same thing as the OP, an idea occurred to me. It looks like the width was set by the parent .gm-style-iw element, so I just set the width of the parent elements to auto using jQuery and hey presto! So here is my code in the hope that it can help someone in the future.

Js

 $('.gm-style-iw').parent().css('width', 'auto'); 

CSS

 .gm-style-iw { width: auto !important; top: 0 !important; left: 0 !important; } 
0
source

Source: https://habr.com/ru/post/1213632/


All Articles