地图不会出现在谷歌地图javascript api v3上

时间:2015-03-10 11:44:22

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

我是谷歌地图api的新手,我正在尝试创建我的第一张地图,通过复制谷歌给你的例子,但不知何故,它不起作用。

<!DOCTYPE html>
<html>
  <head>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
    <style type="text/css">
      html { height: 100% }
      body { height: 100%; margin: 0; padding: 0 }
      #map_canvas { height: 100% }
    </style>
    <script type="text/javascript"
      src="https://maps.googleapis.com/maps/api/js?key=<MYKEY>&sensor=true">
    </script>
    <script type="text/javascript">
      function initialize() {
        var mapOptions = {
          center: new google.maps.LatLng(-34.397, 150.644),
          zoom: 8,
          mapTypeId: google.maps.MapTypeId.ROADMAP
        };
        var map = new google.maps.Map(document.getElementById("map_canvas"),
            mapOptions);
      }

    </script>
  </head>
  <body onload="initialize()">
    <div id="map_canvas" style="width:100%; height:100%"></div>
  </body>
</html>

我正在使用博客来测试它,但是当我发布地图时却没有出现。我可以发布文字,但不能发布地图。想法?

2 个答案:

答案 0 :(得分:1)

首先谷歌地图api不理解css 100%的任何div。

  1. 将100%替换为静态宽度和高度
  2. 如果你确定的话 initialize()函数在onload上运行然后它的好处,如果没有 尝试在文档就绪函数上使用jquery调用它
  3. 如果您使用的是jquery。别忘了包括 你的最终代码将是
  4. javascript

    $( document ).ready(function() {
        function initialize() {
                var mapOptions = {
                  center: new google.maps.LatLng(-34.397, 150.644),
                  zoom: 8,
                  mapTypeId: google.maps.MapTypeId.ROADMAP
                };
                var map = new google.maps.Map(document.getElementById("map_canvas"),
                    mapOptions);
              } initialize();
        });
    

    和HTML

    <div id="map_canvas" style="width:300px; height:300px"></div>
    

    的jsfiddle googleMap

答案 1 :(得分:0)

更改

<script type="text/javascript"
  src="https://maps.googleapis.com/maps/api/js?key=<MYKEY>&sensor=true">
</script>

<script type="text/javascript"
  src="https://maps.googleapis.com/maps/api/js?sensor=true">
</script>

或只是提供关键参数,如果你有