Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/396.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 未使用angular2 google地图和google places搜索栏定义google对象_Javascript_Google Maps_Angular_Google Places - Fatal编程技术网

Javascript 未使用angular2 google地图和google places搜索栏定义google对象

Javascript 未使用angular2 google地图和google places搜索栏定义google对象,javascript,google-maps,angular,google-places,Javascript,Google Maps,Angular,Google Places,我目前正在开发一个使用谷歌地图的angular2应用程序 我使用优秀的sebm angular2谷歌地图模块将谷歌地图的实例嵌入到应用程序中 我现在遇到的问题是,我想使用google place自动完成搜索栏 我已将模块导入我的地图模块,如下所示: import { NgModule } from '@angular/core'; import { CommonModule } from '@angular/common'; import { AgmCoreModule } from 'angu

我目前正在开发一个使用谷歌地图的angular2应用程序

我使用优秀的sebm angular2谷歌地图模块将谷歌地图的实例嵌入到应用程序中

我现在遇到的问题是,我想使用google place自动完成搜索栏

我已将模块导入我的地图模块,如下所示:

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { AgmCoreModule } from 'angular2-google-maps/core';
import { MapComponent } from './map.component';

@NgModule({
  imports: [
    CommonModule,
    AgmCoreModule.forRoot({
      apiKey: '************************',
      libraries: ['places']
    })
  ],
  declarations: [MapComponent]
})
export class MapModule { }
这对地图本身很有效。然而,我能找到的所有文档(例如本文)都说,“google”对象现在应该在全球范围内存在,我应该能够通过在我的组件中声明它来获得它

declare var google: any;
当我尝试这个,然后尝试使用谷歌对象,例如

console.log(google);

。。。它给了我一个错误,google是未定义的。

Angular在
index.html
加载后运行。如果您在
ngOnInit
中放入了某些内容(最好是
ngAfterViewInit
),那么
document.ready
应该已经启动了

唯一可能出错的方法是在获取google库的脚本标记上有
async
defer
标记


更好的解决方案是使用
node
安装谷歌地图,并在需要时设置用于获取谷歌库的任何加载程序(使用键
googlemaps
)。然后,您可以在
PlacesComponent
中放置
import“googlemaps”
,以确保在加载
index.html
后加载库。如果您在
ngOnInit
中放入了某些内容(最好是
ngAfterViewInit
),那么
document.ready
应该已经启动了

唯一可能出错的方法是在获取google库的脚本标记上有
async
defer
标记


更好的解决方案是使用
node
安装谷歌地图,并在需要时设置用于获取谷歌库的任何加载程序(使用键
googlemaps
)。然后,您可以在
PlacesComponent
中放置
import“googlemaps”
,以确保库已加载您可以在ngAfterViewInit事件处理程序中使用该对象。它在加载视图后被激发

您可以使用ngAfterViewInit事件处理程序中的对象。它在加载视图后被激发

您需要将MapsAPILoader注入到类中,然后调用load方法

在加载程序完成之前,将无法访问google对象

例如:

构造函数(私有_mapsAPILoader:mapsAPILoader){}


这个答案是通过github对amiller29au的简略描述-为了更广泛的访问而复制。

您需要将MapsAPILoader注入到类中,然后调用load方法

在加载程序完成之前,将无法访问google对象

例如:

构造函数(私有_mapsAPILoader:mapsAPILoader){}


这个答案是amiller29au通过github的简略回答-为了更广泛的访问而复制。

我的问题很差,我重新编辑了它,因此它更清楚地概括了我的问题,+1关于提及ngAfterViewInit()我的问题很差,我重新编辑了它,因此它更清楚地概括了我的问题,+1关于提及ngAfterViewInit()
ngAfterViewInit() {
  this._mapsAPILoader.load().then(() => {
    // Place your code in here...
  });
}