javascript - 谷歌地图不显示

标签 javascript google-maps google-maps-api-3

我正在尝试将 Google map 插入模式。模态显示了 map 的形状,但全是灰色的。我搜索并找到了一些建议,例如调用 map 的调整大小事件,或将 map 图像的最大宽度设置为无,但到目前为止这些建议都没有帮助。请帮助我。

JavaScript

var map;        
            var myCenter=new google.maps.LatLng(53, -1.33);
var marker=new google.maps.Marker({
    position:myCenter
});

function initialize() {
  var mapProp = {
      center:myCenter,
      zoom: 14,
      draggable: false,
      scrollwheel: false,
      mapTypeId:google.maps.MapTypeId.ROADMAP
  };

  map=new google.maps.Map(document.getElementById("map-canvas"),mapProp);
  marker.setMap(map);

  google.maps.event.addListener(marker, 'click', function() {

    infowindow.setContent(contentString);
    infowindow.open(map, marker);

  }); 
};
google.maps.event.addDomListener(window, 'load', initialize);

google.maps.event.addDomListener(window, "resize", resizingMap());

$('#myMapModal').on('show.bs.modal', function() {
   //Must wait until the render of the modal appear, thats why we use the resizeMap and NOT resizingMap!! ;-)
   resizeMap();
})

function resizeMap() {
   if(typeof map =="undefined") return;
   setTimeout( function(){resizingMap();} , 400);
}

function resizingMap() {
   if(typeof map =="undefined") return;
   var center = map.getCenter();
   google.maps.event.trigger(map, "resize");
   map.setCenter(center); 
}

html

<div class="modal fade" id="myMapModal">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                 <h4 class="modal-title">Modal title</h4>

            </div>
            <div class="modal-body">
                <div class="container">
                    <div class="row">
                        <div id="map-canvas" class=""></div>
                    </div>
                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>

            </div>

CSS

#map-canvas  {
  margin: 0;
  padding: 0;
  height: 100%;
}

#map-canvas {
  width:500px;
  height:480px;
}

最佳答案

做模型时 map div 在启动时不可见并且创建不正确..我建议你在 resizeMap 函数上记忆一下 resizeMap 中的初始化函数

function resizeMap() {
  initialize()
  if(typeof map =="undefined") return;
  setTimeout( function(){resizingMap();} , 400);
}

function resizingMap() {
  initialize()
  if(typeof map =="undefined") return;
  var center = map.getCenter();
  google.maps.event.trigger(map, "resize");
  map.setCenter(center); 
}

无论如何,当您打开模式时,您必须“初始化” map 。创建 map 时,包含 map 的 div 必须可见。

关于javascript - 谷歌地图不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36118979/

相关文章:

javascript - 谷歌地图 API google.maps.event 未定义

javascript - Google Maps API v3 - 初始化后添加标记

javascript - Google Maps API 仅出现一个标记图标

javascript - 在angularjs中使用各种字体

Javascript:错误:找不到模块 '../data/icons/table.png'

javascript - 循环确定数字的数字是否满足条件(Javascript)

javascript - Google map 'heatmap' 可以处理密度和数据值吗?

javascript - Canvas 内容作为高分辨率图像。

java - Android 地理编码功能不如 iOS

android - Google map SDK 地理编码