谷歌地图v3更改信息窗口的大小

发布于 2024-11-25 07:24:35 字数 317 浏览 0 评论 0原文

我想设置信息窗口的大小以适应其中的内容。标准信息窗口太宽我尝试使用 maxWidth 但它似乎不起作用。调整信息窗口大小的最佳方法是什么?

请参阅代码:

    window.setContent( inspStates[i].name+ "<br/>"+"total: "+inspStates[i].totalInsp+ "<br/>"+ info);

气泡中将显示的信息如下所示( \n 表示下一行)

NY \n总计 60 \n2009: 10 \n2010: 20 \n2011: 30

I would like to set the size of an infowindow to fit the content inside. The standard infowindow is too wide I tried to use maxWidth but it doesn't seem to work. What is the best way to resize an infowindow?

see code:

    window.setContent( inspStates[i].name+ "<br/>"+"total: "+inspStates[i].totalInsp+ "<br/>"+ info);

the information that will be displayed in the bubble looks like this( \n means next line)

NY \ntotal 60 \n2009: 10 \n2010: 20 \n2011: 30

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

放肆 2024-12-02 07:24:35

您想要做的是将“标准”Googlemaps infoWindow 替换为您自己的信息窗口,并将您的内容放入其中。一旦您更换了标准 GM 信息窗口,您就有很大的工作自由度。我所做的是:

添加一个名为 #infoWindow 的新样式 ID,并设置其宽度。

#infoWindow {
    width: 150px;
}

在 Javascript 中的任何函数之外,我创建一个新的 infoWindow:

var infoWindow = new google.maps.InfoWindow();

在函数中创建一个新的 var,用于设置标记显示的信息,并将其值设置为内容:

var html = '<div id="infoWindow">';
html += 'NY<br />total 60<br />2009: 10<br />2010: 20<br />2011: 30';
html +='</div>';

使用您的位置信息调用 createMarker 函数会在其他地方创建,并包含 html var 作为参数之一:

var marker = createMarker(point,name,html);

您将在其他地方声明的 createMarker 函数,它将把所有信息放在一起,在地图上显示标记,并在单击时显示上面的信息:

function createMarker(latlng,name,html) {
    var contentString = html;
    var marker = new google.maps.Marker({
        position: latlng,
        map: map,
        title: name
        });

    google.maps.event.addListener(marker, 'click', function() {
        infoWindow.setContent(contentString); 
        infoWindow.open(map,marker);
        });
}

What you want to do is replace the "standard" Googlemaps infoWindow with your own, and put your content into it. Once you replace the standard GM infoWindow, you have a lot of latitude to work with. What I did is this:

Add a new style ID called #infoWindow, and set its width.

#infoWindow {
    width: 150px;
}

Outside of any functions within my Javascript, I create a new infoWindow:

var infoWindow = new google.maps.InfoWindow();

Create a new var within the function that sets the information displayed by the marker(s), and set its value to the content:

var html = '<div id="infoWindow">';
html += 'NY<br />total 60<br />2009: 10<br />2010: 20<br />2011: 30';
html +='</div>';

Call the createMarker function using the location information you would have created elsewhere, and include the html var as one of the arguments:

var marker = createMarker(point,name,html);

The createMarker function you would declare elsewhere, which would bring all of the information together, display the marker on your map, and display the information above when it's clicked on:

function createMarker(latlng,name,html) {
    var contentString = html;
    var marker = new google.maps.Marker({
        position: latlng,
        map: map,
        title: name
        });

    google.maps.event.addListener(marker, 'click', function() {
        infoWindow.setContent(contentString); 
        infoWindow.open(map,marker);
        });
}
魔法唧唧 2024-12-02 07:24:35

这样每个人都清楚,如果他们偶然发现了这条线索。

您所需要做的就是在信息窗口内容中设置容器的高度和宽度。

您不需要覆盖谷歌类,也不需要对此变得过于复杂。

var contentString = '<div class="map-info-window">'+
  '<h1>My Info Window</h1>'+
  '<p>Hello World</p>'+
  '</div>';

var infowindow = new google.maps.InfoWindow({
      content: contentString,   });

在我的 css 中我有这个:

.map-info-window {
    width:200px;
    height:200px;
}

就是这样。这就是设置 Google 地图信息窗口宽度所需要做的全部工作。

Just so everyone is clear, if they stumble on this thread.

All you need to do is set the height and width of a container in the your info window content.

You don't need to override google classes and you don't need to get overly complicated about this.

var contentString = '<div class="map-info-window">'+
  '<h1>My Info Window</h1>'+
  '<p>Hello World</p>'+
  '</div>';

var infowindow = new google.maps.InfoWindow({
      content: contentString,   });

In my css I have this:

.map-info-window {
    width:200px;
    height:200px;
}

That's it. That's all you need to do to set the width of a Google Maps InfoWindow.

简单 2024-12-02 07:24:35

对我来说这个 CSS 工作得很好:

.gm-style-iw {
  width: 324px !important;
  height: 120px !important;
}

For me this CSS works fine:

.gm-style-iw {
  width: 324px !important;
  height: 120px !important;
}
江城子 2024-12-02 07:24:35

要设置 google 地图内气泡信息窗口的大小,只需在 CSS 文件中添加以下 css 即可:

.gmap-popup {
  max-width:204px;
  max-height:110px;
}

改编自 这个讨论

To set the size of the bubble info-window inside a google map it's enough to add the following css in your CSS file:

.gmap-popup {
  max-width:204px;
  max-height:110px;
}

Adapted from this discussion.

苏辞 2024-12-02 07:24:35

JFrancis 的答案是正确的,只是在设置宽度时不要忘记在 CSS 中添加“!important”!这看起来可能是一件小事,但如果你不这样做,你的 CSS 就会被覆盖!

#infoWindow {
    width: 150px !important;
}

JFrancis answers is the right one, just don't forget to add "!important" in your CSS when you set the width! This might look like a minor thing, but if you don't do it, your CSS will simply get overwritten!

#infoWindow {
    width: 150px !important;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文