【问题标题】:Why can't I load google maps in angular 2 component?为什么我不能在 Angular 2 组件中加载谷歌地图?
【发布时间】:2017-01-22 01:53:51
【问题描述】:

这是我的 ts 组件:

import {Component, OnInit,  Output, EventEmitter} from '@angular/core';

    declare var google: any;

    @Component({
      selector: 'app-root',
      templateUrl: './app.component.html',
      styleUrls: ['./app.component.css'],
    })


    export class AppComponent implements OnInit {
      title = 'Dashboard';
      private map: any;


        constructor() {
          let brussels = new google.maps.LatLng(50.82, 4.35);
          var mapOptions = {
            zoom: 9,
            center: brussels
          };
          this.map = new google.maps.Map(document.getElementById('googleMap'), mapOptions);

          var marker = new google.maps.Marker({
            position: brussels
          });
          //google.maps.event.addListener(marker, 'click', ( () => this.select.next("i was a map click")) )
          marker.setMap(this.map);

        }

      ngOnInit(){

      }

这是我的html:

<h1>
  {{title}}
</h1>
<div id="googleMap"></div>
<div id="legend-container"><h3>Legend</h3></div>
<div id="info-box" style="">?</div>

我在主 index.html 中声明了谷歌地图的 api 键。问题是我似乎无法将谷歌地图加载到组件中。控制台报错主要有以下几种:

error_handler.js:45 EXCEPTION: Error in ./AppComponent class AppComponent_Host - inline template:0:0 caused by: google is not definedErrorHandler.handleError @ error_handler.js:45(anonymous function) @ application_ref.js:209ZoneDelegate.invoke @ zone.js:192onInvoke @ ng_zone_impl.js:43ZoneDelegate.invoke @ zone.js:191Zone.run @ zone.js:85(anonymous function) @ zone.js:451ZoneDelegate.invokeTask @ zone.js:225onInvokeTask @ ng_zone_impl.js:34ZoneDelegate.invokeTask @ zone.js:224Zone.runTask @ zone.js:125drainMicroTaskQueue @ zone.js:357
error_handler.js:47 ORIGINAL EXCEPTION: google is not definedErrorHandler.handleError @ error_handler.js:47(anonymous function) @ application_ref.js:209ZoneDelegate.invoke @ zone.js:192onInvoke @ ng_zone_impl.js:43ZoneDelegate.invoke @ zone.js:191Zone.run @ zone.js:85(anonymous function) @ zone.js:451ZoneDelegate.invokeTask @ zone.js:225onInvokeTask @ ng_zone_impl.js:34ZoneDelegate.invokeTask @ zone.js:224Zone.runTask @ zone.js:125drainMicroTaskQueue @ zone.js:357
error_handler.js:50 ORIGINAL STACKTRACE:ErrorHandler.handleError @ error_handler.js:50(anonymous function) @ application_ref.js:209ZoneDelegate.invoke @ zone.js:192onInvoke @ ng_zone_impl.js:43ZoneDelegate.invoke @ zone.js:191Zone.run @ zone.js:85(anonymous function) @ zone.js:451ZoneDelegate.invokeTask @ zone.js:225onInvokeTask @ ng_zone_impl.js:34ZoneDelegate.invokeTask @ zone.js:224Zone.runTask @ zone.js:125drainMicroTaskQueue @ zone.js:357
error_handler.js:51 ReferenceError: google is not defined

任何建议都非常感谢:)

【问题讨论】:

  • 谷歌地图尚未加载,因此google 命名空间未定义...
  • @yurzui 我做到了,但没有任何运气

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


【解决方案1】:

谷歌地图 API 尚未加载,您应该异步加载它并在加载后初始化地图。看看this question关于异步加载谷歌地图。

正如@Günter Zöchbauer 所说,您应该在初始化视图后初始化地图。 (在ngAfterViewInit

类似这样的:

export class AppComponent implements AfterViewInit {
  title = 'Dashboard';
  private map: any;

  constructor(private el:ElementRef) {
  }
  onMapsReady(){
    let brussels = new google.maps.LatLng(50.82, 4.35);
    var mapOptions = {
      zoom: 9,
      center: brussels
    };
    this.map = new google.maps.Map(this.el.nativeElement, mapOptions);
    var marker = new google.maps.Marker({
      position: brussels
    });
    //google.maps.event.addListener(marker, 'click', ( () => this.select.next("i was a map click")) )
    marker.setMap(this.map);
  }
  ngAfterViewInit(){
    (<any>window).googleMapsReady=this.onMapsReady.bind(this);
     var script = document.createElement("script");
    script.type = "text/javascript";
    document.getElementsByTagName("head")[0].appendChild(script);
    script.src = "http://maps.googleapis.com/maps/api/js?v=3&sensor=false&callback=googleMapsReady";

  }

}

注意:通过服务加载 API 会(好得多)

【讨论】:

  • 你刚刚停止了我 2 天的痛苦。谢谢.. 好的,明白了。为什么 ionic 无法从 index.html 中的脚本标签加载远程脚本?为什么我们需要这样加载它?
【解决方案2】:

此时 DOM 中不存在 googleMap 元素。将代码移至ngAfterViewInit()

 export class AppComponent implements OnInit {
    title = 'Dashboard';
    private map: any;


    ngAfterViewInit() {
      let brussels = new google.maps.LatLng(50.82, 4.35);
      var mapOptions = {
        zoom: 9,
        center: brussels
      };
      this.map = new google.maps.Map(document.getElementById('googleMap'), mapOptions);

      var marker = new google.maps.Marker({
        position: brussels
      });
      //google.maps.event.addListener(marker, 'click', ( () => this.select.next("i was a map click")) )
      marker.setMap(this.map);

    }
 }

【讨论】:

  • 错误是关于google没有被定义,不是元素不存在。
  • 那么这是他将遇到的下一个问题;-)
【解决方案3】:

尝试从您的 index.html 加载这 2 个文件

<script src="https://www.gstatic.com/charts/loader.js"></script>
<script type="text/javascript" src="https://www.google.com/jsapi"></script>

更多详情,请参考这个-https://developers.google.com/chart/interactive/docs/basic_load_libs

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-12
    • 1970-01-01
    • 2012-11-10
    • 2018-06-16
    • 1970-01-01
    • 2018-05-01
    相关资源
    最近更新 更多