【问题标题】:Google Maps InfoWindow width is overwritten even when set correctly即使设置正确,Google Maps InfoWindow 宽度也会被覆盖
【发布时间】:2015-04-19 19:02:29
【问题描述】:

我正在使用 Google Maps API,但我对InfoWindow 有一些问题。

这是一个总结:

  • 当用户单击标记时,我正在加载 InfoWindow 的内容
  • 内容是部分视图,通过 Ajax 调用加载
  • 在 .done 回调中,我调用了一个异步方法,它将数据插入到 InfoWindow 内容中。我需要这样做是因为我希望立即显示 InfoWindow 的主要内容,而这个“奖励”信息可能会在十分之一秒后显示。

这非常有效;但我的信息窗口右侧有一条白色条带,无法删除(见下图)

但是,我加载的内容包含在带有固定width<div> 中:

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

在我的 CSS 中,我写道:

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


更多细节的 InfoWindow 的加载如下所示:

$.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);
        });
});

内容完全没问题,就是这个白条的问题。

查看我的浏览器控制台(我在所有浏览器中都复制了它),我可以看到:

如您所见,我的 &lt;div&gt; 包含从我的 ajax 调用加载的所有数据,大小合适(绿色矩形,对应于屏幕截图中的灰色区域),但上述 div(来自 Google API本身,进入红色矩形)具有更大的尺寸,问题出在于此。

我发现的唯一方法是运行这个修改 InfoWindow 内部结构的 jQuery 脚本:

$(".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);

注意gm-style-iw是Google给出的包含InfoWindow所有内容的div的类名,悬停在上面的截图上。我还在我的 CSS 中添加了这条规则:

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

它在控制台中有效,但是,在代码本身、.done 回调或domready Google 地图事件中编写时无效...
但是,在这种后期情况下,如果我将上面的 jQuery 脚本封装在 setTimeout() 中,它就可以工作!我评论了异步方法,所以不是这个有罪,但似乎 domready 被执行,而 InfoWindow 的 100% 仍未显示 - 这是 contrary to the doc

我还尝试将我的info.setOptions 移到.done 回调之外并将其放在它之后,没有效果。

那么,如果没有右侧的白色条带,如何显示“正常”信息窗口?
我不想实现 InfoBubble 或其他自定义 InfoWindow 库。这是一个个人项目,我想了解问题的原因和位置。当然,还要找到解决方案。

感谢您的帮助!

【问题讨论】:

  • 它只适用于我一次,第二次点击相同或另一个标记,它使用默认值,第二次点击后'custom-iw'被删除

标签: google-maps google-maps-api-3 width infowindow


【解决方案1】:

它比你想象的要复杂一点。

只是一些事情:

  • 您是否注意到有一个关闭按钮?即使您移除按钮,空间也会存在,因为 API 会根据此空间计算其他节点的大小
  • 笔尖必须在中心
  • 还有用于圆形边框和阴影的其他容器
  • 将计算 infoWindow 的大小以使其适合视口
  • 内容必须在需要时可滚动
  • 信息窗口的位置必须设置(因此需要计算信息窗口的准确高度)

基本上:所有这些都需要计算精确的大小和位置,信息窗口中的大多数节点都是绝对定位的,它更像是一种在 DTP 中使用的技术,而不是在 HTML 文档中使用它。

另外:InfoWindows 会经常修改以修复错误,今天有效的解决方案明天可能会被破坏。

但是,目前对我有用的一种方法:

  1. 将信息窗口的maxWidth 设置为所需的宽度 - 51(在本例中为 291)
  2. 无法通过$.css 应用!important-rules,因此必须直接通过样式表完成。为了能够访问所有元素,为信息窗口的根节点设置一个新类(请注意,可能存在您无法控制的信息窗口,例如 POI):

    google.maps.event.addListener(infowindow,'domready',function(){ 
      $('#div-main-infoWindow')//the root of the content
       .closest('.gm-style-iw')
        .parent().addClass('custom-iw');
    });
    
  3. 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;
  }

演示(如我所说,明天可能会坏):http://jsfiddle.net/doktormolle/k57qojq7/

【讨论】:

  • 伙计,你太棒了!感谢您的解释和代码:-) 只是一件事,我不明白你为什么说将 maxwidth 设置为 342 - 51 ;而您在代码中将其设置为 342 ?现在,我的 .gm-style-iw 在我的 CSS 中有一条规则 width: 100% 并且没关系。将max-width 设置为 291px 会更好吗?
  • 我的意思是InfoWindow的maxWidth-option(不是CSS),你可以在fiddle的代码中看到它。
  • 342 或 291 as max-width 总是产生相同(好)的结果,这很奇怪......但是,非常感谢您的帮助
  • 效果好与否,取决于内容量(打印内容所需的空间)
  • 注意小费不是放在342的中间
【解决方案2】:

在这里聚会有点晚了,但是在寻找了与 OP 相同的目标之后,我想到了一个想法。宽度似乎是由.gm-style-iw 的父元素设置的,所以我只是使用jQuery 将父元素的宽度设置为自动,嘿presto!所以这是我的代码,希望它可以在将来对某人有所帮助。

JS

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

CSS

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-05-28
    • 2011-08-17
    • 2017-02-15
    • 2011-02-11
    • 2021-09-26
    • 1970-01-01
    • 2010-12-06
    • 1970-01-01
    相关资源
    最近更新 更多