【问题标题】:Google MAP API Key in Angular AppAngular 应用程序中的 Google MAP API 密钥
【发布时间】:2015-06-26 12:23:58
【问题描述】:

我有一个 Angular 应用程序并尝试将一个简单的谷歌地图添加到页面。但我得到了这个:

> Google Maps API is required. Please register the following JavaScript
> library http://maps.google.com/maps/api/js?sensor=true. <div
> data-ui-view="" class="ng-scope">(anonymous function) @
> angular.js:11598$get @ angular.js:8548$ @ angular.js:8219v @
> angular.js:7726g @ angular.js:7075(anonymous function) @
> angular.js:6954k @ angular-ui-router.min.js:7k.compile @
> angular-ui-router.min.js:7$ @ angular.js:8217v @ angular.js:7726g @
> angular.js:7075g @ angular.js:7078(anonymous function) @
> angular.js:6954compile @ angular-ui-router.min.js:7$ @
> angular.js:8217v @ angular.js:7726g @ angular.js:7075(anonymous
> function) @ angular.js:6954k @ angular-ui-router.min.js:7k.compile @
> angular-ui-router.min.js:7$ @ angular.js:8217v @ angular.js:7726g @
> angular.js:7075(anonymous function) @ angular.js:6954compile @
> angular-ui-router.min.js:7$ @ angular.js:8217v @ angular.js:7726g @
> angular.js:7075(anonymous function) @ angular.js:6954k @
> angular-ui-router.min.js:7(anonymous function) @
> angular-ui-router.min.js:7$get.l.$broadcast @
> angular.js:14707x.transitionTo.x.transition.I.then.x.transition.x.transition
> @ angular-ui-router.min.js:7(anonymous function) @
> angular.js:13175$get.l.$eval @ angular.js:14388$get.l.$digest @
> angular.js:14204(anonymous function) @ angular.js:14427e @
> angular.js:4902(anonymous function) @ angular.js:5282
> js?sensor=true&_=1435321003344:8 Failed to execute 'write' on
> 'Document': It isn't possible to write into a document from an
> asynchronously-loaded external script unless it is explicitly opened.

下面的页面是我从https://codeload.github.com/hpneo/gmaps/下载的示例 奇怪的是,如果我通过 http://myapp/directroy/map.html 浏览它,它可以正常工作,但如果我想通过 http://myapp/ng-route-to-map-page

到达那里,它会给我上面的错误

我尝试通过 code.google.com 注册 javascript sdk,并将库作为

包含到页面中

我做错了吗?我把 API Key 放在那里

<script type="text/javascript" src="http://maps.google.com/maps/api/js?&key=APIKEY"></script>

但仍然得到同样的错误。

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>GMaps.js &mdash; Geometry overlays</title>
  <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
  <script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=true"></script>

  <script type="text/javascript" src="../gmaps.js"></script>
  <link rel="stylesheet" href="http://twitter.github.com/bootstrap/1.3.0/bootstrap.min.css" />
  <link rel="stylesheet" type="text/css" href="examples.css" />
  <script type="text/javascript">
    var map, rectangle, polygon, circle;
    $(document).ready(function(){
      map = new GMaps({
        el: '#map',
        lat: -12.043333,
        lng: -77.028333
      });
      var bounds = [[-12.030397656836609,-77.02373871559225],[-12.034804866577001,-77.01154422636042]];
      rectangle = map.drawRectangle({
        bounds: bounds,
        strokeColor: '#BBD8E9',
        strokeOpacity: 1,
        strokeWeight: 3,
        fillColor: '#BBD8E9',
        fillOpacity: 0.6
      });

      var paths = [[-12.040397656836609,-77.03373871559225],[-12.040248585302038,-77.03993927003302],[-12.050047116528843,-77.02448169303511],[-12.044804866577001,-77.02154422636042]];
      polygon = map.drawPolygon({
        paths: paths,
        strokeColor: '#25D359',
        strokeOpacity: 1,
        strokeWeight: 3,
        fillColor: '#25D359',
        fillOpacity: 0.6
      });
      var lat = -12.040504866577001;
      var lng = -77.02024422636042;
      circle = map.drawCircle({
        lat: lat,
        lng: lng,
        radius: 350,
        strokeColor: '#432070',
        strokeOpacity: 1,
        strokeWeight: 3,
        fillColor: '#432070',
        fillOpacity: 0.6
      });
      for(var i in paths){
        bounds.push(paths[i]);
      }
      var b = [];
      for(var i in bounds){
        latlng = new google.maps.LatLng(bounds[i][0], bounds[i][1]);
        b.push(latlng);
      }
      for(var i in paths){
        latlng = new google.maps.LatLng(paths[i][0], paths[i][1]);
        b.push(latlng);
      }
      map.fitLatLngBounds(b);
    });
  </script>
</head>
<body>
  <h1>GMaps.js &mdash; Geometry overlays</h1>
  <div class="row">
    <div class="span11">
      <div id="map"></div>
    </div>
    <div class="span6">
      <p>You can draw geometry overlays (which can be a polygon or a rectangle or a circle):</p>
      <pre>polygon = map.drawRectangle({
  bounds: bounds,
  strokeColor: '#BBD8E9',
  strokeOpacity: 1,
  strokeWeight: 3,
  fillColor: '#BBD8E9',
  fillOpacity: 0.6
});
polygon = map.drawPolygon({
  paths: paths,
  strokeColor: '#25D359',
  strokeOpacity: 1,
  strokeWeight: 3,
  fillColor: '#25D359',
  fillOpacity: 0.6
});
circle = map.drawCircle({
  lat: lat,
  lng: lng,
  radius: 350,  //350 meters
  strokeColor: '#432070',
  strokeOpacity: 1,
  strokeWeight: 3,
  fillColor: '#432070',
  fillOpacity: 0.6
});</pre>
      <p>Be careful with the settings as they are not the same for each overlay.</p>
    </div>
  </div>
</body>
</html>

【问题讨论】:

    标签: javascript angularjs google-maps iis mime-types


    【解决方案1】:

    如果您想在 Angular 应用程序中使用 Google 地图,您应该使用 Angular google maps directive

    使用 bower 简单安装

    bower install angular-google-maps
    

    在您的 HTML 页面中加载脚本

    请注意,Google 提供的 API 密钥可以在 maps.googleapis 脚本参考的尾部指定。

    <script src='/path/to/lodash[.min].js'></script>
    <script src='/path/to/angular-google-maps[.min].js?key=123...'></script>
    <script src='//maps.googleapis.com/maps/api/js?sensor=false'></script>
    

    在你的应用中注入依赖

    angular.module('myApplicationModule', ['uiGmapgoogle-maps']);
    

    将地图添加到您的范围

    $scope.map = { center: { latitude: 45, longitude: -73 }, zoom: 8 };
    

    然后将地图添加到您的模板中,完成!

    <ui-gmap-google-map center='map.center' zoom='map.zoom'></ui-gmap-google-map>
    

    不要忘记在 CSS 中指定高度

    .angular-google-map-container { height: 400px; }
    

    您不必指定 API 密钥,但如果您不这样做,预计会看到:

    NoApiKeys developers.google.com/maps/documentation/javascript/...

    【讨论】:

    • 我需要使用 GMaps 库
    • 这些第一行 if (!(typeof window.google === 'object' && window.google.maps)) { throw 'Google Maps API 是必需的。请注册以下 JavaScript 库maps.google.com/maps/api/js?sensor=true。' } 导致它。我认为需要某种异步加载,但想不通
    【解决方案2】:

    好的。我的问题是我两次引用了 maps api。一旦在我的父页面和地图页面 -child- 中,我将它包含在解析中。

    一旦我删除了父页面中的那个,我的问题就解决了。也许,它可以帮助其他有同样问题的人。只需检查参考。另一方面,我认为在我的情况下给出诸如Google Maps API is required. Please register the following JavaScript 之类的错误是不合适的。

    【讨论】:

      【解决方案3】:

      取自a similar question

      在api请求中指定&amp;callback=yourCallbackdocument.write不会被调用,在哪里

      window["yourCallback"] = function () {}; // compiler minified dot notation for me

      要么是这样,要么在您的 HTML 中包含 http://maps.googleapis.com/maps/api/js 脚本标签,因为它第一次被解析,就像在新构建的 iframe 或服务器响应中作为字符串一样。

      我相信它是网络请求“引荐来源”,它会根据您的 &amp;client=yourClientID&amp;signature=yourHMACSHA1EncodedSignature 进行检查,因此如果您要签名,请确保您的 window.location.href 与请求标头匹配。 iframe 中的document.referrer 将显示父框架的 url,除非他们阻止它(某些网站的安全问题)

      document.write 会在以后发生时清除您的文档,因此会被阻止

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-03-25
        • 2019-05-21
        • 2017-04-19
        • 1970-01-01
        • 1970-01-01
        • 2013-04-19
        • 1970-01-01
        • 2017-07-16
        相关资源
        最近更新 更多