谷歌地图调整大小并在标签系统中居中

Google Maps resize and centre in tabs system

本文关键字:标签 系统 中居 调整 谷歌地图      更新时间:2023-09-26

我目前正在隐藏标签系统中使用谷歌地图。一切正常,除了地图似乎偏离中心,我必须滚动才能找到我的标记。

我已经设法加载了完整的地图,因为以前由于隐藏属性,只有地图的一部分可见。

我使用了下面的脚本,但是在尝试使用.getCenter集中地图时收到Javascrip错误"对象#没有方法'getCenter'"。

<script type="text/javascript">
    $(document).ready(function(){
    $('#hotel-listing .option').hide();
    $('#hotel-listing .option:first').show();
    $('#hotel-listing nav ul li a:first').addClass('active');
    $('#hotel-listing nav ul li a').click(function(){ 
    $('#hotel-listing nav ul li a').removeClass('active');
    $(this).addClass('active');
    var currentTab = $(this).attr('href');
    $('#hotel-listing .option').hide();
    $(currentTab).show();
 var currCenter = map.getCenter();
 google.maps.event.trigger(map, 'resize');
 map.setCenter(currCenter);
    return false;
    }); 
    });
</script> 

任何帮助表示赞赏。

谢谢

您可能已将map定义为映射初始化函数中的var,但由于作用域而无法访问它。

如果将var map;定义为全局,则在映射初始化函数中删除var代码可能会起作用。没有看到 ho 你的地图构造函数代码被设置了,这仍然只是一个猜测,但基于大多数谷歌地图示例,一个很好的有根据的猜测