【问题标题】:Why doesn't the google maps tutorial work on my pc?为什么谷歌地图教程不能在我的电脑上运行?
【发布时间】:2012-08-15 14:39:32
【问题描述】:

所以我从谷歌开发者网站下载了一段代码示例来玩玩。下面找到的代码与here 找到的代码完全相同。当我下载 html 并尝试在 chrome 中本地打开它时,它没有显示任何内容,它返回一个空白屏幕,没有任何错误(firebug lite)。我正在使用 Mac,而且我对所有这些网络开发人员都是新手。我应该设置一个本地主机的东西还是我完全错过了什么?任何帮助是极大的赞赏。

  <!DOCTYPE html>
    <html>
      <head>
        <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
        <meta charset="utf-8">
        <title>Google Maps JavaScript API v3 Example: Circle Simple</title>
        <link href="/maps/documentation/javascript/examples/default.css" rel="stylesheet">
        <script src="https://maps.googleapis.com/maps/api/js?sensor=false"></script>
        <script>

         var citymap = {};
         citymap['chicago'] = {
           center: new google.maps.LatLng(41.878113, -87.629798),
           population: 2842518
         };
         citymap['newyork'] = {
           center: new google.maps.LatLng(40.714352, -74.005973),
           population: 8143197
         };
         citymap['losangeles'] = {
           center: new google.maps.LatLng(34.052234, -118.243684),
           population: 3844829
         }
         var cityCircle;

         function initialize() {
           var mapOptions = {
             zoom: 4,
             center: new google.maps.LatLng(37.09024, -95.712891),
             mapTypeId: google.maps.MapTypeId.TERRAIN
           };

           var map = new google.maps.Map(document.getElementById("map_canvas"),
               mapOptions);

           for (var city in citymap) {
             // Construct the circle for each value in citymap. We scale population by 20.
             var populationOptions = {
               strokeColor: "#FF0000",
               strokeOpacity: 0.8,
               strokeWeight: 2,
               fillColor: "#FF0000",
               fillOpacity: 0.35,
               map: map,
               center: citymap[city].center,
               radius: citymap[city].population / 20
             };
             cityCircle = new google.maps.Circle(populationOptions);
           }
         }
        </script>
      </head>
      <body onload="initialize()">
        <div id="map_canvas"></div>
      </body>
    </html>

【问题讨论】:

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


    【解决方案1】:

    由于您直接从演示站点保存了文件,因此您没有保存与其关联的 CSS 文件 (/maps/documentation/javascript/examples/default.css)。所以你的地图没有宽度/高度。如果您保存此文件并正确引用它,它应该可以工作。或者,您可以在 html 文件中添加一点 css:

    <!DOCTYPE html>
    <html>
      <head>
        <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
        <meta charset="utf-8">
        <title>Google Maps JavaScript API v3 Example: Circle Simple</title>
        <style type="text/css">
        html, body {
          height: 100%;
          margin: 0;
          padding: 0;
        }
    
        #map_canvas {
          height: 100%;
        }
    
        @media print {
          html, body {
            height: auto;
          }
    
          #map_canvas {
            height: 650px;
          }
        }
        </style>
        <script src="https://maps.googleapis.com/maps/api/js?sensor=false"></script>
        <script>
    
          // Create an object containing LatLng, population.
          var citymap = {};
          citymap['chicago'] = {
            center: new google.maps.LatLng(41.878113, -87.629798),
            population: 2842518
          };
          citymap['newyork'] = {
            center: new google.maps.LatLng(40.714352, -74.005973),
            population: 8143197
          };
          citymap['losangeles'] = {
            center: new google.maps.LatLng(34.052234, -118.243684),
            population: 3844829
          }
          var cityCircle;
    
          function initialize() {
            var mapOptions = {
              zoom: 4,
              center: new google.maps.LatLng(37.09024, -95.712891),
              mapTypeId: google.maps.MapTypeId.TERRAIN
            };
    
            var map = new google.maps.Map(document.getElementById('map_canvas'),
                mapOptions);
    
            for (var city in citymap) {
              // Construct the circle for each value in citymap. We scale population by 20.
              var populationOptions = {
                strokeColor: '#FF0000',
                strokeOpacity: 0.8,
                strokeWeight: 2,
                fillColor: '#FF0000',
                fillOpacity: 0.35,
                map: map,
                center: citymap[city].center,
                radius: citymap[city].population / 20
              };
              cityCircle = new google.maps.Circle(populationOptions);
            }
          }
        </script>
      </head>
      <body onload="initialize()">
        <div id="map_canvas"></div>
      </body>
    </html>
    

    【讨论】:

      【解决方案2】:

      问题在于文档的head 中的link 元素的href 属性使用指向本地计算机上样式表的相对URL。除非您已下载该样式表并将其放在 link 元素的 href 属性中引用的位置,否则它不会找到它。

      不过,在这种情况下,我建议您将其更改为以下内容,以便它使用来自 Google 服务器的样式表:

      <link href="https://google-developers.appspot.com/maps/documentation/javascript/examples/default.css" rel="stylesheet">
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-01-08
        • 2014-09-15
        • 1970-01-01
        • 1970-01-01
        • 2021-11-14
        • 1970-01-01
        • 1970-01-01
        • 2013-08-05
        相关资源
        最近更新 更多