【问题标题】:Cannot read property 'maps' of undefined : AGM-MAP无法读取未定义的属性“地图”:AGM-MAP
【发布时间】:2018-12-23 14:08:27
【问题描述】:

我正在使用谷歌地图来使用我的应用程序来查明设备的位置。我正在使用 angular 6 并使用 agm-map 库集成谷歌地图。一切正常,但是当我尝试为地图设置边界以便地图显示每个标记时,我得到了这个错误,

core.js:1598 ERROR TypeError: Cannot read property 'maps' of undefined
    at device-location.view.ts:45
    at SafeSubscriber.schedulerFn [as _next] (core.js:3724)
    at SafeSubscriber.push../node_modules/rxjs/_esm5/internal/Subscriber.js.SafeSubscriber.__tryOrUnsub (Subscriber.js:253)
    at SafeSubscriber.push../node_modules/rxjs/_esm5/internal/Subscriber.js.SafeSubscriber.next (Subscriber.js:191)
    at Subscriber.push../node_modules/rxjs/_esm5/internal/Subscriber.js.Subscriber._next (Subscriber.js:129)
    at Subscriber.push../node_modules/rxjs/_esm5/internal/Subscriber.js.Subscriber.next (Subscriber.js:93)
    at EventEmitter.push../node_modules/rxjs/_esm5/internal/Subject.js.Subject.next (Subject.js:53)
    at EventEmitter.push../node_modules/@angular/core/fesm5/core.js.EventEmitter.emit (core.js:3704)
    at map.js:220
    at ZoneDelegate.push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invoke (zone.js:388)

你可以在下面看到我的代码,

 ngOnInit() {
      this.devices$.subscribe(data => {
        if (data) {
            this.devices = data;
        }
      });
    }

  ngAfterViewInit() {

    this.agmMap.mapReady.subscribe(map => {
      const bounds: LatLngBounds = new google.maps.LatLngBounds(); // HERE GOOGLE UNDEFINED
      for (const dev of this.devices) {
        bounds.extend(new google.maps.LatLng(dev.latitude, dev.longitude));
      }
      map.fitBounds(bounds);
    });
  }

这是发生错误的代码行,

const bounds: LatLngBounds = new google.maps.LatLngBounds();

谁能帮我解决这个问题?谢谢

【问题讨论】:

    标签: angular typescript google-maps google-maps-api-3 google-maps-markers


    【解决方案1】:

    google好像没有初始化,试试

    declare const google: any;
    

    或者安装谷歌地图类型

    npm install @types/google-maps --save-dev
    

    【讨论】:

    • 我已经安装了@types/google-maps。我试过了,还是一样的错误
    • 您是否宣传了对 index.html 的引用
    猜你喜欢
    • 2021-08-28
    • 1970-01-01
    • 1970-01-01
    • 2022-07-11
    • 2022-07-27
    • 1970-01-01
    • 2020-12-31
    • 2021-11-11
    • 1970-01-01
    相关资源
    最近更新 更多