获取 Google 地图 v3 来调整 InfoWindow 的高度
当我单击标记并出现 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我终于找到了解决该问题的有效方法。虽然没有我希望的那么灵活,但已经相当不错了。从根本上来说,关键点是:不要使用字符串作为窗口内容,而是使用 DOM 节点。
这是我的代码:
以下是 CSS 声明:
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:
the following is the CSS declaration:
ps: "instance" refers to the current custom subclass of google.maps.OverlayView (which I'm extending)
只需用 div 包裹您的内容并指定其高度:
,例如
- 就我而言,这已经足够了。
Just wrap your content with a div and specify it's height:
<div style="height:60px">...</div>
, e.g.- In my case it was fairly enough.
您的地图画布太小。增加
元素的宽度/高度,您应该会自动看到更大的 InfoWindows。
也就是说,我在正在构建的网站上遇到了同样的问题。我通过创建一个包含 InfoWindow 内容的克隆 div,测量该 div 的宽度和高度,然后将 InfoWindow 内容 div 设置为具有测量的宽度和高度来解决这个问题。这是我移植到 codeAddress 函数中间的代码(另请注意,我从 InfoWindow 声明中删除了
maxWidth: 200
):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):只需使用带有 padding-bottom: 30px 的 DIV 包裹 InfoBox 内容即可;
JS:
CSS:
Just wrap you InfoBox content with DIV with padding-bottom: 30px;
JS:
CSS:
这并不是真正的答案(daveoncode 创建 DOM 节点并将其用作内容的解决方案是正确的),但是如果您需要动态更改设置后的内容(例如使用 jQuery),那么您可以强制 gmail 使用以下命令调整 infoWindow 的大小:
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: