简体   繁体   English

谷歌地图:动态调整信息量

[英]Google Maps: Dynamically resize infowindow

How do I resize the info window once it's opened? 如何在信息窗口打开后重新调整大小? I have a hyperlink inside the window which generates an AJAX-request; 我在窗口内有一个超链接,它生成一个AJAX请求; on the callback I would like to resize the info window. 在回调中我想调整信息窗口的大小。

Check out the following. 请查看以下内容。 What happens is that i create an initial info window with minimal info (see getInfoWindowHtml). 会发生什么是我用最少的信息创建一个初始信息窗口(参见getInfoWindowHtml)。 The gmap.openInfoWindowHtml call provides for a callback which gets called after the infowindow opens. gmap.openInfoWindowHtml调用提供了一个在infowindow打开后调用的回调。 In that callback, make your ajax call and reset the window contents. 在该回调中,进行ajax调用并重置窗口内容。 There are two issues though: 但有两个问题:

i couldnt get the window to resize exactly to the returned content, so i just used a standard size (see the new GSize(300, 150) in the anonymous function returned by markerClickFn) 我无法让窗口完全重新调整返回的内容,所以我只使用标准大小(请参阅markerClickFn返回的匿名函数中的新GSize(300,150))

  1. Im my case, if a marker is near the extremes of the image bounds, parts of the info window would be clipped. 我的情况是,如果标记靠近图像边界的极端,信息窗口的某些部分将被剪裁。 IOW, the map does not recenter to include the infowindow. IOW,地图不会重新定位以包含infowindow。 I can probably fix this, but it has not been a pressing issue. 我可以解决这个问题,但这不是一个紧迫的问题。

     function markerClickFn(venue, latlng) { return function() { var title = venue.Name; var infoHtml = getInfoWindowHtml(venue); // need to zoom in gmap.setZoom(13); gmap.openInfoWindowHtml(latlng, infoHtml, { onOpenFn: function() { var iw = gmap.getInfoWindow(); iw.reset(iw.getPoint(), iw.getTabs(), new GSize(300, 150), null, null); $("#info-" + venue.Id.toString()).load("/Venue/MapInfoWindow/" + venue.Id); } } ); }; } function getSidebarHtml(venue) { var url = "/Venue/Details/" + venue.Id; // actually should bring up infowindow var html = "<li id='venue-" + venue.Id + "'>\\n"; html = html + venue.Name + "\\n"; //html = html + "<p class='description'>" + trunc(venue.Description, 200) + "</p>\\n"; html = html + "</li>"; return html; } function getInfoWindowHtml(venue) { var url = "/Venue/Details/" + venue.Id; // actually should bring up infowindow var html = "<div id='info-" + venue.Id + "'><a href='" + url + "' class='url summary'>" + venue.Name + "</a></div>\\n"; return html; } function addVenueMarker(venue) { var icon = new GIcon(G_DEFAULT_ICON); icon.image = "http://chart.apis.google.com/chart?cht=mm&amp;chs=24x32&amp;chco=FFFFFF,008CFF,000000&amp;ext=.png"; var latLng = new GLatLng(venue.Lat, venue.Lng); var marker = new GMarker(latLng, { icon: icon }); var fn = markerClickFn(venue, latLng); GEvent.addListener(marker, "click", fn); marker.event_ = venue; marker.click_ = fn; venue.marker_ = marker; markers.push(marker); return marker; } 

It looks like the only solution I found to resize the InfoWindow dynamically is to call the InfoWindow's setContent method. 看起来我发现动态调整InfoWindow的唯一解决方案是调用InfoWindow的setContent方法。 You'll need to pass in the HTML node or the HTML string. 您需要传入HTML节点或HTML字符串。 This basically redraws the InfoWindow. 这基本上重绘了InfoWindow。 What it essentially does is close the current one and opens a new one. 它本质上做的是关闭当前的并打开一个新的。 So, it'll see a flicker during the reloading of the content. 因此,在重新加载内容时会看到闪烁。 This kind of sucks. 这种糟透了。 It seems that Google deprecated their v2 API and made things really hard to do in the newer version. 谷歌似乎已经弃用了他们的v2 API,并且在新版本中做了很多事情。 I think Google has hired a bunch of Sun Java developers across the street in Silicon Valley and they guys don't know how to provide simple to use API's like the Microsoft camp. 我认为谷歌在硅谷的街道上雇佣了一大批Sun Java开发人员,他们不知道如何提供简单易用的API,就像微软的阵营一样。 Really frustrating! 真的很沮丧!

It isn't possible to animate the resize of the info window, but there is a reset function. 无法为信息窗口的大小调整设置动画,但有一个重置功能。 The documentation says that you can leave any parameter null, but if you leave the 'size' parameter null, you'll get an error. 文档说您可以将任何参数保留为null,但如果将'size'参数保留为null,则会出现错误。 This means that you have to specify the size of the InfoWindow. 这意味着您必须指定InfoWindow的大小。

However, since that ruins the original point of dynamically resizing the window, I would strongly recommend simply closing the original info window and creating a new one - which is what the reset function will appear to do anyhow. 但是,由于这会破坏动态调整窗口大小的原始点, 我强烈建议简单地关闭原始信息窗口并创建一个新窗口 - 这就是重置功能无论如何都会出现的情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM