获取Google Maps v3以调整InfoWindow的高度

EdL*_*ner 16 infowindow google-maps-api-3

当我单击标记并显示InfoWindow时,如果内容的长度超过InfoWindow默认高度(90px),则不会调整高度.

  • 我使用纯文字,没有图像.
  • 我试过maxWidth.
  • 我检查了继承的CSS.
  • 我已经将我的内容包装在一个div中并将我的CSS应用到包含高度的CSS中.
  • 我甚至尝试使用InfoWindow上的domready事件强制InfoWindow使用jQuery调整大小.

我只剩下几根头发.这是我的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();
Run Code Online (Sandbox Code Playgroud)

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

文件中的HTML:

<div id="google_map"> <div id="map_canvas" style="width:279px; height:178px"></div> </div>
Run Code Online (Sandbox Code Playgroud)

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

#bubble-marker{ font-size:11px; line-height:15px; }
Run Code Online (Sandbox Code Playgroud)

dav*_*ode 9

我终于找到了解决问题的有效方法.不像我希望的那样灵活,但它非常好.从根本上说,关键点是:不要使用字符串作为窗口内容,而是使用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);
Run Code Online (Sandbox Code Playgroud)

以下是CSS声明:

div.map-popup {
    overflow: auto;
    overflow-x: hidden;
    overflow-y: auto;
}
Run Code Online (Sandbox Code Playgroud)

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


Geo*_*oth 1

您的地图画布太小。增加元素的宽度/高度<div id="map_canvas">,您应该会自动看到更大的 InfoWindows。

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

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); 
            });
        }
    });
}
Run Code Online (Sandbox Code Playgroud)