【问题标题】:initMap is not a function: ErrorinitMap 不是函数:错误
【发布时间】:2017-03-28 01:07:44
【问题描述】:

GoogleMap 在我的 Angular 项目 (WebStorm IDE) 中不可见!!

我尝试过使用回调函数!!谷歌地图仍然不可见! I get this Error in Console


index.html

<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyAANu_sjcxDH04GZHg187EGg6csTeiX-jw&callback=initMap"
         defer></script>

MapCtrl.js

 .controller('MapCtrl',function ($scope, $window) {

$window.initMap = function () {

    var map=new google.maps.Map(document.getElementById('map'), {
      center: {lat: -34.397, lng: 150.644},
      zoom: 8
    });

  }

});

Mymap.html

<div id="map" ng-controller="MapCtrl"></div>

【问题讨论】:

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


【解决方案1】:

我曾经遇到过同样的问题。我通过从 src url 的末尾删除 '&callback=initMap' 来解决它。在你的情况下;

<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyAANu_sjcxDH04GZHg187EGg6csTeiX-jw"
     defer></script>

而不是

<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyAANu_sjcxDH04GZHg187EGg6csTeiX-jw&callback=initMap"
     defer></script>

【讨论】:

    【解决方案2】:

    尝试在地图 id 中添加一些 css 样式,这种情况发生在我身上很多次。

    html, body { height: 100%; margin: 0; padding: 0; }
    #map { height: 100%; }
    

    如果您需要更多信息,请阅读here

    【讨论】:

    猜你喜欢
    • 2018-01-12
    • 1970-01-01
    • 1970-01-01
    • 2018-04-21
    • 1970-01-01
    • 1970-01-01
    • 2019-02-09
    • 1970-01-01
    相关资源
    最近更新 更多