javascript - Google Maps API v3 - 隐藏 Div - map 未以 div 为中心

标签 javascript google-maps

我已经查看了几个类似的问题,但仍然没有找到解决方案。

我有这个页面:http://www.citizensmemorial.com/Test2014/locations/cmh-infectious-disease-and-internal-medicine-clinic/index.html

我正在使用此代码:

<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyBs_4MZDYZTCtkoB-Hbo4qQyWy-qHQuEdA"></script>
<script>
var mapExists = document.getElementById('map-canvas'); if(mapExists) 
var map;
function initialize() {
var myLatLng = new google.maps.LatLng (37.625387, -93.424350)
  var mapOptions = {    
    zoom: 12,
    center: myLatLng
  };   
 map = new google.maps.Map(document.getElementById('map-canvas'),
      mapOptions);
       var marker = new google.maps.Marker({
      position: myLatLng,
      map: map,
      title: 'CMH Infectious Disease & Internal Medicine Clinic'
});
}
google.maps.event.addDomListener(window, 'load', initialize);
    </script>

这是适用的 CSS:

html, body, #map-canvas {
    width:100%;
    height:100%
}
#map_canvas {
position: relative;
}
.map-holder {
width:500px;
    height:270px;
    padding: 0px;
    display:table; 
    margin-top:3px; 
    float:right;
}

我假设它与 css 相关,导致 map 无法居中,因为我已经在这个 url 上测试了 map 代码并且它正在工作: http://www.citizensmemorial.com/Temp/mapTest.html

最佳答案

看起来您正在初始化 map ,然后 map Canvas div 可见。您需要触发调整大小,否则 map 将不知道其正确的大小。

初始化 map 后调用 map 调整大小

google.maps.event.trigger(map, "resize");

然后将 map 中心设置为您的标记。

map.setCenter(markerlatlng);

关于javascript - Google Maps API v3 - 隐藏 Div - map 未以 div 为中心,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28030786/

相关文章:

javascript - 如何调用 Nashorn CompiledScript 中的方法?

javascript - 无法打印 getDay() 方法的结果

javascript - 隐藏谷歌地图地形和标签选项

javascript - 获取给定中心点和半径的边界框的纬度/经度值

google-maps - 带有 VisualRefresh 的 Google map 图标

javascript - 如何访问 angularjs 中 ng-repeat 中调用的组件中的索引?

javascript - 如何只在移动设备上运行jquery代码?

php - 我的网站在整个文件中注入(inject)了 javascript

ios - 如何通过 google maps iOS API 对地址进行地理编码?

ios - 如何在函数初始化后从函数中获取值?