为什么谷歌地图教程不能在我的电脑上运行?

时间:2012-08-15 14:39:32

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

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

  <!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>

2 个答案:

答案 0 :(得分:4)

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

在这种情况下,我建议您只需将其更改为以下内容,以便使用Google服务器中的样式表:

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

答案 1 :(得分:4)

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

<!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>