Google 地图 - 当地图不可见时,缩放以适合标记不起作用
我正在使用 Google 地图 API v2。我向地图添加标记,然后缩放以适合这些标记。如果地图可见,则效果很好,我这样做。但如果不是 - 例如,如果我有一个选项卡条,并且在页面加载时未选择地图的选项卡 - 那么当我显示地图时,缩放级别和中心是错误的。
这是一个简单的测试用例(使用 jQuery):
<script type="text/javascript">
var scale = Math.random() * 20;
$(document).ready(function() {
var $container = $('#container');
// $container.hide();
var map = new GMap2($('#map')[0]);
$container.show();
var markerBounds = new GLatLngBounds();
for (var i = 0; i < 10; i++) {
var randomPoint = new GLatLng(38.935394 + (Math.random() - 0.5) * scale, -77.061382 + (Math.random() - 0.5) * scale);
map.addOverlay(new GMarker(randomPoint));
markerBounds.extend(randomPoint);
}
map.setCenter(markerBounds.getCenter(), map.getBoundsZoomLevel(markerBounds));
});
</script>
<div id="container">
<div id="map" style="margin: 100px; width: 450px; height: 300px;"></div>
</div>
这可以正常工作,但是如果您取消注释 $container.hide()
,它就会崩溃。
有没有办法让 Google Maps API 在不可见的 div 上正常工作?
I'm using the Google Maps API v2. I add markers to the map and then zoom to fit those markers. This works fine if the map is visible I do this. But if not - for example, if I have a tabstrip, and the map's tab isn't selected when the page loads - then when I do show the map, the zoom level and center are wrong.
Here's a simple test case (uses jQuery):
<script type="text/javascript">
var scale = Math.random() * 20;
$(document).ready(function() {
var $container = $('#container');
// $container.hide();
var map = new GMap2($('#map')[0]);
$container.show();
var markerBounds = new GLatLngBounds();
for (var i = 0; i < 10; i++) {
var randomPoint = new GLatLng(38.935394 + (Math.random() - 0.5) * scale, -77.061382 + (Math.random() - 0.5) * scale);
map.addOverlay(new GMarker(randomPoint));
markerBounds.extend(randomPoint);
}
map.setCenter(markerBounds.getCenter(), map.getBoundsZoomLevel(markerBounds));
});
</script>
<div id="container">
<div id="map" style="margin: 100px; width: 450px; height: 300px;"></div>
</div>
This works fine as is, but if you uncomment $container.hide()
it's all whacked out.
Is there a way to get the Google Maps API to work properly on a div that's not visible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这就是我最终所做的事情,因为它是有价值的。
这使用 Rick Strahl 的 jQuery 监控插件 来监视选项卡面板可见性发生变化,然后重新应用缩放逻辑。
为了完整起见,这是我的
zoomToFitMarkers
扩展:这依赖于几个约定:
GMap2 对象存储在
map.gmap
中,其中map
是目标 DOM 元素:每次添加标记时地图,它存储在一个数组中以供将来使用:
Here's what I've ended up doing, for what it's worth.
This uses Rick Strahl's monitoring plugin for jQuery to watch the tab panel for visibility changes, and then reapplies the zoom logic.
For completeness here's my
zoomToFitMarkers
extension:This relies on a couple of conventions:
The GMap2 object is stored in
map.gmap
, wheremap
is the target DOM element:Each time a marker is added to the map, it's stored in an array for future use:
您需要做的就是先创建
GMaps2()
。然后,您可以hide()
容器,添加点,获取getBoundsZoomLevel()
,再次show()
它,它应该可以工作美好的。请尝试以下操作:
All you would need to do is to create the
GMaps2()
before anything else. You can thenhide()
the container, add the points, get thegetBoundsZoomLevel()
,show()
it again, and it should work fine.Try the following: