为什么Google Maps教程无法在我的PC上运行? [英] Why doesn't the google maps tutorial work on my pc?

查看:37
本文介绍了为什么Google Maps教程无法在我的PC上运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我已经从google开发人员网站上下载了一些示例代码来试用.下面找到的代码与在此处找到的代码完全相同.当我下载html并尝试在chrome中本地打开它时,它什么也没有显示,它返回了一个空白屏幕,没有任何错误(firebug lite).我使用的是Mac,所有这些Web开发人员都是新手.我应该设置本地主机的东西还是我完全丢失了东西?任何帮助是极大的赞赏.

So I've downloaded a sample bit of code from the google developer website to play around with. The bit of code found below is exactly the same as the code found here. When I download the html and try to open it locally in chrome it doesn't show anything, it returns me a blank screen without any errors (firebug lite). I'm using a mac and I'm new at all this web dev stuff. Should I set up a localhost thing or am I completely missing something? Any help is greatly appreciated.

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

推荐答案

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

Since you saved the file directly from the demo site, you did not save the CSS file associated with it (/maps/documentation/javascript/examples/default.css). So your map has no width/height to it. If you save this file and reference it properly it should work. Alternatively, you can just add the bit of css to your html file:

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

这篇关于为什么Google Maps教程无法在我的PC上运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆