3

所以我从谷歌开发者网站下载了一些示例代码来玩。下面找到的代码与此处找到的代码完全相同。当我下载 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>
4

2 回答 2

4

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

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

<link href="https://google-developers.appspot.com/maps/documentation/javascript/examples/default.css" rel="stylesheet">
于 2012-08-15T14:46:45.057 回答
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>
于 2012-08-15T14:47:14.380 回答