无法读取未定义的属性“地图”:AGM-MAP

时间:2018-07-16 05:00:06

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

我正在使用Google Map使用我的应用程序来确定设备的位置。我正在使用Agm-Map库使用angular 6和集成的Google 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();

有人可以帮助我解决这个问题吗?谢谢

1 个答案:

答案 0 :(得分:0)

似乎Google尚未初始化,请尝试

declare const google: any;

或安装Google地图输入

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