【问题标题】:Google Maps API showing blank map显示空白地图的 Google Maps API
【发布时间】:2013-08-29 11:39:52
【问题描述】:

我确定这是一个基本问题,但我现在已经多次将头撞到墙上,所以希望有人会同情我!

我有以下示例,但它所做的只是显示一个灰色的框,根本没有地图。谁能告诉我为什么?

我已经检查过我实际上正在返回一个结果,而且它似乎工作正常。

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <title></title>
        <meta name="description" content="">
        <meta name="viewport" content="width=device-width, initial-scale=1">

        <style>
            html, body, #map-canvas {margin: 0;padding: 0;height: 100%;}
        </style>
    <script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script>
    <script>
      var geocoder;
      var map;

      function initialize() 
      {
        geocoder = new google.maps.Geocoder();        
        geocoder.geocode( { 'address': "England"}, function(results, status) 
        {
          if (status == google.maps.GeocoderStatus.OK) 
          {
            var mapOptions = {
              zoom: 8,
              center: new google.maps.LatLng(results[0].geometry.location),
              mapTypeId: google.maps.MapTypeId.ROADMAP
            }

            // Let's draw the map
            map = new google.maps.Map(document.getElementById("map-canvas"), mapOptions);

          } 
          else 
          {
            alert("Geocode was not successful for the following reason: " + status);
          }
        });
      }

    initialize();
    </script>
  </head>

  <body onload="">
 <div id="map-canvas" style="width: 320px; height: 480px;"></div>
</body>
</html>    

【问题讨论】:

    标签: javascript google-maps-api-3


    【解决方案1】:

    如果地图的高度/宽度为 0,也会发生这种情况。

    【讨论】:

      【解决方案2】:

      results[0].geometry.location 已经是一个 latLng 对象,所以你可以说:

      center: results[0].geometry.location
      

      在这里找到工作小提琴:http://jsfiddle.net/87z9K/

      【讨论】:

      • 不错的收获,希望你不介意。在您的答案中添加小提琴以供 OP 验证。
      • @putvande 这行得通!仅供参考,它在哪里生成 latLng 对象?
      【解决方案3】:

      这是因为提供了磨损的“google.maps.LatLng”。 提供测试坐标,它将起作用。 换行

      center: new google.maps.LatLng(results[0].geometry.location),
      

      center: new google.maps.LatLng(-34.397, 150.644)
      

      获取英格兰坐标

      【讨论】:

        【解决方案4】:

        这不完全是您的问题,但密切相关。

        我发现我必须使用有效的centre 设置 mapOptions,如下所示:

        new google.maps.Map(mapCanvas, {
            center: new google.maps.LatLng(-34.397, 150.644)
        });
        

        如果我没有输入地图选项,或者如果我输入了但没有设置有效的center,我会得到一张没有加载图块的空白地图。

        【讨论】:

          【解决方案5】:

          尝试调整浏览器窗口的大小,摇一摇浏览器/用光标从浏览器选项卡中拖动它,您将看到地图出现。

          由于 MVC 部分视图中的一些奇怪原因,谷歌地图显示为空白,您的地图正在工作,只需要调整大小。

          用光标摇动浏览器窗口听起来很有趣,但它确实有效,我不知道如何最好地描述它。

          谢谢, 阿努拉格

          ================================================ =========================

          我的最终工作代码如下:

          `

          <script type="text/javascript">
          
              $(document).ready(function () {
                  (function () {
                      var options = {
                          zoom: 6,
                          center: new google.maps.LatLng(-2.633333, 37.233334),
                          mapTypeId: google.maps.MapTypeId.TERRAIN,
                          mapTypeControl: false
                      };
          
                      // init map
                      var map = new google.maps.Map(document.getElementById('map_canvas'), options);
          
          
                      var arrLocation = [];
                      $("#markerDiv").find("div").each(function () {
                          var Lat = $(this).find("input[id='Latitude']").val();
                          var Lon = $(this).find("input[id='Longitude']").val();
                          var Id = $(this).find("input[id='Id']").val();
                          var AssessmentDet = $(this).find("input[id='AssessmentDateTime']").val();
                          var LocAcc = $(this).find("input[id='LocationAccuracy']").val();
                          var assessorName = $(this).find("input[id='AssessorName']").val();
                          var partnerName = $(this).find("input[id='PartnerName']").val();
                          arrLocation.push({
                              Id: Id,
                              Latitude: Lat,
                              Longitude: Lon,
                              AssessmentDate: AssessmentDet,
                              LocationAccuracy: LocAcc,
                              AssessorDetail: assessorName,
                              PartnerName: partnerName
                              });
                      });
          
                      var allMarkers = [];
          
                      for (var i = 0; i < arrLocation.length; i++) {
          
                          //final position for marker, could be updated if another marker already exists in same position
                          var latlng = new google.maps.LatLng(arrLocation[i].Latitude, arrLocation[i].Longitude);
                          var finalLatLng = latlng;
                          var comparelatlng = "(" + arrLocation[i].Latitude + "," + arrLocation[i].Longitude + ")";
                          var copyMarker = arrLocation[i];
          
                          var marker = new google.maps.Marker({
                              position: new google.maps.LatLng(arrLocation[i].Latitude, arrLocation[i].Longitude),
                              map: map,
                              title: 'Equine # ' + arrLocation[i].Id,
                              icon:"abc.png"
                          });
          
          
                          var markerInfo = "Reference # : <b>" + arrLocation[i].Id + "</b><br/>";
                          markerInfo = markerInfo + "Assessor : <b>" + arrLocation[i].AssessorDetail + "</b><br/>";
                          markerInfo = markerInfo + "Date :  <b>" + arrLocation[i].AssessmentDate + "</b><br/>";
                          markerInfo = markerInfo + "Partner :  <b>" + arrLocation[i].PartnerName + "</b>";(function (marker, i) {
          
            bindInfoWindow(marker, map, new google.maps.InfoWindow(), markerInfo);
                          })(marker, i);
                      }
                  })();
              });
          
              function bindInfoWindow(marker, map, infowindow, html) {
                  google.maps.event.addListener(marker, 'click', function () {
                      infowindow.setContent(html);
                      infowindow.open(map, marker);
                  });
              }
          
          
          </script>
          

          `

          【讨论】:

          • 我看到了同样的情况,是的,调整浏览器窗口的大小确实会使其完成加载。不知道为什么或该怎么做。
          • 大声笑,它可以工作,但@Anurag 背后的主要原因是什么
          • 我目前正在与这种行为作斗争,并尝试了各种方法,例如触发调整大小。我认为我的问题是由于 DOM 对象在 Angular ui 路由器中未正确加载
          • 我也面临同样的问题!有什么办法可以解决吗?
          • @Anand 我通过创建一个
            然后通过 ajax 将 div 内容替换为 _partial 视图文本,同时确保在您的局部视图中指定了中心点地图的缩放级别如下,code var options = { zoom: 6, center: new google.maps.LatLng(-2.633333, 37.233334), mapTypeId: google.maps.MapTypeId.TERRAIN, mapTypeControl: false }; // 初始化地图 var map = new google.maps.Map(document.getElementById('map_canvas'), options); code
          【解决方案6】:

          我尝试设置地图的 MapTypeId 并按照 Anurag 的建议提供了帮助:

          map.setMapTypeId(google.maps.MapTypeId.TERRAIN);
          

          【讨论】:

            【解决方案7】:

            我可以看到您的代码存在一般 javascript 问题。

            您的脚本可能会在加载 HTML 之前尝试将地图嵌入页面中。

            这样调用函数(还有其他方法)。

            <body onload="initialize()">
            

            【讨论】:

            • 这不是实际问题,请参阅下面的答案。这些突出了代码存在的问题。
            • @Padrig,我实际上已经尝试过了,但是当我这样做时,地图框甚至无法加载。
            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2020-06-12
            • 2015-09-30
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多