获取 Google 地图 v3 来调整 InfoWindow 的高度

发布于 2024-10-22 20:40:13 字数 2694 浏览 2 评论 0原文

当我单击标记并出现 InfoWindow 时,如果内容的长度长于 InfoWindow 默认高度 (90px),则高度不会调整。

  • 我只使用文本,没有图像。
  • 我试过最大宽度。
  • 我已经检查了继承的CSS。
  • 我已将内容包装在 div 中 并将我的 CSS 应用到其中,包括 一个高度。
  • 我什至尝试过强迫 InfoWindow 使用 jQuery 调整大小 使用 domready 事件 信息窗口。

我只剩下几根头发了。这是我的 JS:

var geocoder;
var map;
var marker;

function initialize() {
  geocoder   = new google.maps.Geocoder();
  var latlng = new google.maps.LatLng(41.8801,-87.6272); 
  var myOptions = {
    zoom: 13,
    center: latlng,
    mapTypeId: google.maps.MapTypeId.ROADMAP
  };
  map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
}

function codeAddress(infotext,address) {
  geocoder.geocode({ 'address': address }, function (results, status) {
    if (status == google.maps.GeocoderStatus.OK) {
      map.setCenter(results[0].geometry.location);
      var image  = '/path-to/mapMarker.png';
      var infowindow = new google.maps.InfoWindow({ content: infotext, maxWidth: 200 });
      var marker = new google.maps.Marker({
        map: map,
        position: results[0].geometry.location,
        icon: image
      });
      google.maps.event.addListener(marker, 'click', function () { 
        infowindow.open(map, marker); 
      });
    }
  });

}

function checkZipcode(reqZip) {

  if ( /[0-9]{5}/.test(reqZip) ) {

    $.ajax({
      url: 'data.aspx?zip=' + reqZip,
      dataType: 'json',
      success: function(results) {

        $.each(results.products.product, function() {

          var display = "<span id='bubble-marker'><strong>"+this.name+"</strong><br>"+
                        this.address+"<br>"+
                        this.city+", "+this.state+" "+this.zip+"<br>"+
                        this.phone+"</span>";

          var address = this.address+","+
                        this.city+","+
                        this.state+","+
                        this.zip;

          codeAddress(display,address);

        });

      },
      error: function() { $('#information-bar').text('fail'); }
    });

  } else { $('#information-bar').text('Zip codes are five digit numbers.'); }

}

$('#check-zip').click(function() { $('#information-bar').text(''); checkZipcode($('#requested-zipcode').val()); });

initialize();

InfoText 和 Address 来自 XML 文件的 AJAX 查询。数据不是问题,因为它总是正确地传输。 codeAddress() 在数据被检索和格式化后被调用。

文件中的 HTML:

<div id="google_map"> <div id="map_canvas" style="width:279px; height:178px"></div> </div>

我的 InfoWindow 内容的 CSS(没有其他 CSS 应用于地图):

#bubble-marker{ font-size:11px; line-height:15px; }

When I click a marker and the InfoWindow appears, the height does not adjust if the length of the content is longer that the InfoWindow default height (90px).

  • I am using text-only, no images.
  • I have tried maxWidth.
  • I have checked for inherited CSS.
  • I have wrapped my content in a div
    and applied my CSS to that, including
    a height.
  • I have even tried forcing the
    InfoWindow to resize with jQuery
    using the domready event on the
    InfoWindow.

I only have a few hairs left. Here is my JS:

var geocoder;
var map;
var marker;

function initialize() {
  geocoder   = new google.maps.Geocoder();
  var latlng = new google.maps.LatLng(41.8801,-87.6272); 
  var myOptions = {
    zoom: 13,
    center: latlng,
    mapTypeId: google.maps.MapTypeId.ROADMAP
  };
  map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
}

function codeAddress(infotext,address) {
  geocoder.geocode({ 'address': address }, function (results, status) {
    if (status == google.maps.GeocoderStatus.OK) {
      map.setCenter(results[0].geometry.location);
      var image  = '/path-to/mapMarker.png';
      var infowindow = new google.maps.InfoWindow({ content: infotext, maxWidth: 200 });
      var marker = new google.maps.Marker({
        map: map,
        position: results[0].geometry.location,
        icon: image
      });
      google.maps.event.addListener(marker, 'click', function () { 
        infowindow.open(map, marker); 
      });
    }
  });

}

function checkZipcode(reqZip) {

  if ( /[0-9]{5}/.test(reqZip) ) {

    $.ajax({
      url: 'data.aspx?zip=' + reqZip,
      dataType: 'json',
      success: function(results) {

        $.each(results.products.product, function() {

          var display = "<span id='bubble-marker'><strong>"+this.name+"</strong><br>"+
                        this.address+"<br>"+
                        this.city+", "+this.state+" "+this.zip+"<br>"+
                        this.phone+"</span>";

          var address = this.address+","+
                        this.city+","+
                        this.state+","+
                        this.zip;

          codeAddress(display,address);

        });

      },
      error: function() { $('#information-bar').text('fail'); }
    });

  } else { $('#information-bar').text('Zip codes are five digit numbers.'); }

}

$('#check-zip').click(function() { $('#information-bar').text(''); checkZipcode($('#requested-zipcode').val()); });

initialize();

InfoText and Address come from an AJAX query of an XML file. Data is not the issue, as it always comes through correctly. codeAddress() is called after the data has been retrieved and formatted.

HTML in the file:

<div id="google_map"> <div id="map_canvas" style="width:279px; height:178px"></div> </div>

CSS for my InfoWindow content (no other CSS applies to the map):

#bubble-marker{ font-size:11px; line-height:15px; }

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

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

发布评论

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

评论(5

南街女流氓 2024-10-29 20:40:13

我终于找到了解决该问题的有效方法。虽然没有我希望的那么灵活,但已经相当不错了。从根本上来说,关键点是:不要使用字符串作为窗口内容,而是使用 DOM 节点。
这是我的代码:

// this dom node will act as wrapper for our content
var wrapper = document.createElement("div");

// inject markup into the wrapper
wrapper.innerHTML = myMethodToGetMarkup();

// style containing overflow declarations
wrapper.className = "map-popup";

// fixed height only :P
wrapper.style.height = "60px";

// initialize the window using wrapper node     
var popup = new google.maps.InfoWindow({content: wrapper});

// open the window
popup.open(map, instance);

以下是 CSS 声明:

div.map-popup {
    overflow: auto;
    overflow-x: hidden;
    overflow-y: auto;
}

ps:“instance”指的是 google.maps.OverlayView (我正在扩展)的当前自定义子类

I finally found a working solution for the problem. Is not as flexible as I wished, but it's pretty good. Fundamentally the key point is: don't use a string as window content but instead a DOM node.
This is my code:

// this dom node will act as wrapper for our content
var wrapper = document.createElement("div");

// inject markup into the wrapper
wrapper.innerHTML = myMethodToGetMarkup();

// style containing overflow declarations
wrapper.className = "map-popup";

// fixed height only :P
wrapper.style.height = "60px";

// initialize the window using wrapper node     
var popup = new google.maps.InfoWindow({content: wrapper});

// open the window
popup.open(map, instance);

the following is the CSS declaration:

div.map-popup {
    overflow: auto;
    overflow-x: hidden;
    overflow-y: auto;
}

ps: "instance" refers to the current custom subclass of google.maps.OverlayView (which I'm extending)

香橙ぽ 2024-10-29 20:40:13

只需用 div 包裹您的内容并指定其高度:

...

,例如

 myMarker.setContent('<div style="height:60px">' + txt + '</div>');

- 就我而言,这已经足够了。

Just wrap your content with a div and specify it's height: <div style="height:60px">...</div>, e.g.

 myMarker.setContent('<div style="height:60px">' + txt + '</div>');

- In my case it was fairly enough.

双马尾 2024-10-29 20:40:13

您的地图画布太小。增加

元素的宽度/高度,您应该会自动看到更大的 InfoWindows。

也就是说,我在正在构建的网站上遇到了同样的问题。我通过创建一个包含 InfoWindow 内容的克隆 div,测量该 div 的宽度和高度,然后将 InfoWindow 内容 div 设置为具有测量的宽度和高度来解决这个问题。这是我移植到 codeAddress 函数中间的代码(另请注意,我从 InfoWindow 声明中删除了 maxWidth: 200):

function codeAddress(infotext,address) {
    geocoder.geocode({ 'address': address }, function (results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            map.setCenter(results[0].geometry.location);

            // Create temporary div off to the side, containing infotext:
            var $cloneInfotext = $('<div>' + infotext + '</div>')
                .css({marginLeft: '-9999px', position: 'absolute'})
                .appendTo($('body'));

            // Wrap infotext with a div that has an explicit width and height, 
            // found by measuring the temporary div:
            infotext = '<div style="width: ' + $cloneInfotext.width() + 'px; ' +
                'height: ' + $cloneInfotext.height() + 'px">' + infotext + 
                '</div>';

            // Delete the temporary div:
            $cloneInfotext.remove();

            // Note no maxWidth defined here:
            var infowindow = new google.maps.InfoWindow({ content: infotext }); 
            var marker = new google.maps.Marker({
                map: map,
                position: results[0].geometry.location
            });
            google.maps.event.addListener(marker, 'click', function () { 
                infowindow.open(map, marker); 
            });
        }
    });
}

Your map canvas is too small. Increase the width/height of your <div id="map_canvas"> element and you should see larger InfoWindows automatically.

That said, I had the same problem on a site I was building. I solved it by creating a cloned div containing the InfoWindow content, measuring that div's width and height, and then setting the InfoWindow content div to have that measured width and height. Here's my code ported into the middle of your codeAddress function (also note that I removed the maxWidth: 200 from your InfoWindow declaration):

function codeAddress(infotext,address) {
    geocoder.geocode({ 'address': address }, function (results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            map.setCenter(results[0].geometry.location);

            // Create temporary div off to the side, containing infotext:
            var $cloneInfotext = $('<div>' + infotext + '</div>')
                .css({marginLeft: '-9999px', position: 'absolute'})
                .appendTo($('body'));

            // Wrap infotext with a div that has an explicit width and height, 
            // found by measuring the temporary div:
            infotext = '<div style="width: ' + $cloneInfotext.width() + 'px; ' +
                'height: ' + $cloneInfotext.height() + 'px">' + infotext + 
                '</div>';

            // Delete the temporary div:
            $cloneInfotext.remove();

            // Note no maxWidth defined here:
            var infowindow = new google.maps.InfoWindow({ content: infotext }); 
            var marker = new google.maps.Marker({
                map: map,
                position: results[0].geometry.location
            });
            google.maps.event.addListener(marker, 'click', function () { 
                infowindow.open(map, marker); 
            });
        }
    });
}
沫雨熙 2024-10-29 20:40:13

只需使用带有 padding-bottom: 30px 的 DIV 包裹 InfoBox 内容即可;

JS:

map_info_window = new google.maps.InfoWindow();      
var $content = $('<div class="infobox">').html(content);
map_info_window.setContent($content.get(0));
map_info_window.open(map, marker);

CSS:

.infobox{
    padding-bottom: 30px;
}

Just wrap you InfoBox content with DIV with padding-bottom: 30px;

JS:

map_info_window = new google.maps.InfoWindow();      
var $content = $('<div class="infobox">').html(content);
map_info_window.setContent($content.get(0));
map_info_window.open(map, marker);

CSS:

.infobox{
    padding-bottom: 30px;
}
橪书 2024-10-29 20:40:13

这并不是真正的答案(daveoncode 创建 DOM 节点并将其用作内容的解决方案是正确的),但是如果您需要动态更改设置后的内容(例如使用 jQuery),那么您可以强制 gmail 使用以下命令调整 infoWindow 的大小:

infoWindowLinea.setContent(infoWindowLinea.getContent());

It is not really an answer (daveoncode's solution to create a DOM node and use it as content is right), but if you need to dynamically change the content once set (e.g. with jQuery) then you can force gmail to resize the infoWindow with:

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