我知道这个问题发生在其他人身上,但我一直坚持下去,找不到任何解决方案。
这是我的@NgModule :
import { NgModule } from '@angular/core';
import { AgmCoreModule } from 'angular2-google-maps/core/index';
// Global vars
import appGlobals = require('./../globals');
@NgModule({
imports: [ AgmCoreModule.forRoot({
apiKey: appGlobals.googleMapsApiKey
}) ],
})
export class JobSearchModule {}
这是我的 HTML:
<div class="col-md-12 google-maps-container">
<sebm-google-map [latitude]="mapLat"
[longitude]="mapLng"
[mapDraggable]="false"
[zoom]="zoom"
[zoomControl]="false"
[streetViewControl]="false"
(mapClick)="mapClicked($event)">
<sebm-google-map-marker [latitude]="mapLat"
[longitude]="mapLng">
</sebm-google-map-marker>
</sebm-google-map>
</div>
这是我在 HTML 头标签中的截图:
所以看起来它确实包含了两次 API,但我不知道为什么或如何。
@escarabin问题是您将模块导入应用程序的子模块中。 您必须在应用程序的根模块中导入它。 关闭它,因为这不是错误。 随意重新打开。 谢谢!
嘿@SebastianM! 感谢您的建议,但我仍然面临这个问题。
这是我的配置:
job-search-module.ts
的子模块中使用angular2-google-mapsshared.module.ts
,其中包含我在其他几个模块中使用的所有组件和模块。app.module.ts
现在,当我在 app.module.ts 中导入AgmCoreModule时,我看到一条错误消息,指出sebm-google-map
不是我的job-search-module.ts
的一部分,并且没有显示任何映射。
当我在shared.module.ts
或job-search.module.ts
中导入AgmCoreModule时,我看到 _“您已在此页面上多次包含 Google Maps API”_。
那我该怎么办?
PS:我没有能力重新打开问题
任何人? :(我现在很坚持
@escarabin我有类似的问题。 尝试这个。
您需要在根应用程序模块中导入地图,因此您可以执行以下操作:
@NgModule({
declarations: [
...
],
imports: [
...,
AgmCoreModule.forRoot({
apiKey: 'some key here'
})
],
但是,如果您想在不属于该模块的组件中使用地图,请转到该模块并执行以下操作:
@NgModule({
declarations: [
SomeComponentThatHasMapsInItsHtml,
.....
],
imports: [
AgmCoreModule,
...
],
要添加我认为这可行的原因,是因为您需要在应用程序的根目录中注册该事物(以便使用您的密钥初始化 google 内容)。
但是您在属于另一个模块的组件中使用<seb-maps-smth-smth>
,因此您需要将 AgmCoreModule 添加为导入,以便它可以在此模块中找到具有选择器seb-maps-smth-smth
的导出组件。
希望能解决问题
非常感谢@EddyP23的努力,但不幸的是我仍然看到错误......它真的在你身边使用[email protected]吗? 这很奇怪。
问题不在于错误本身,而是在页面上导致了一堆完全随机的错误......
另外,您在谈论<seb-maps-smth-smth>
。 我不使用这个标签,也没有在文档中看到它。 我正在使用<sebm-google-map>
@escarabin是的,我使用的是 0.15.0 版。
我的意思是任何seb-maps-{placeholder}
选择器。 我的 HTML 看起来像:
<sebm-google-map #sebmGoogleMap
[latitude]="myVar1"
[longitude]="myVar2"
(idle)='onIdle()'
[zoom]='mapZoomLevel'
[streetViewControl]='false'
>
<sebm-google-map-marker #marker
[latitude]="myVar3"
[longitude]="myVar4"
></sebm-google-map-marker>
</sebm-google-map>
你现在看到什么错误? 我相信这个设置应该可以工作。 你能分享更多的代码吗?
我仍然看到“您在此页面上多次包含 Google Maps API”错误。
我的 HTML 如下所示:
<sebm-google-map #sebmGoogleMap
[latitude]="mapLat"
[longitude]="mapLng"
[mapDraggable]="false"
[zoom]="zoom"
[zoomControl]="false"
[streetViewControl]="false"
(mapClick)="mapClicked($event)">
<sebm-google-map-marker [latitude]="mapLat"
[longitude]="mapLng">
</sebm-google-map-marker>
</sebm-google-map>
这是我的子模块:
import { NgModule } from '@angular/core';
import { jobSearchRouting } from './job-search.routes';
import { SharedModule } from './../shared/shared.module';
import { AgmCoreModule } from 'angular2-google-maps/core/index';
// Components
import { SearchComponent } from './components/search.component';
@NgModule({
declarations: [ SearchComponent ],
imports: [ jobSearchRouting,
AgmCoreModule,
SharedModule ]
})
export class JobSearchModule {}
和我的 app.module.ts @NgModule :
@NgModule({
declarations: [ AppComponent,
HomeComponent,
HeaderComponent,
FooterComponent,
PostComponent,
ClubComponent,
CandidateSignUpComponent,
PostPreviewComponent,
SignInComponent,
ProfileSubHeaderComponent,
RecruiterPromoComponent,
NotificationsComponent ],
imports: [ RouterModule,
HttpModule,
SharedModule,
BrowserModule,
AgmCoreModule.forRoot({
apiKey: appGlobals.googleMapsApiKey
}),
MetaModule.forRoot(metaConfig),
routing ],
providers: [ NotificationsService,
MetaService ],
bootstrap: [ AppComponent ],
})
嗯..这很奇怪。 您确定没有以旧方式在任何地方(例如在index.html
中)导入谷歌地图的脚本标签吗? 像这样的东西:
<script async defer
src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&callback=initMap">
</script>
我还会仔细检查AgmCoreModule
的所有用法,以检查您是否没有在其他地方意外地两次导入它。
我也有这个问题。 我在 app.module 中为我的组件添加了 AgmCoreModule.forRoot({...}),并且检查了多个 google api 调用。
仍然返回相同的错误。
我也有这个问题。 我还在 app.module 中为我的组件添加了 AgmCoreModule.forRoot({...}),并且我还检查了多个 google api 调用。
好吧,我的错误,如果它可以帮助这里的其他人——尝试删除你的旧脚本标签,而不是把它注释掉。 出于某种原因,在模板中评论它并没有删除它。
@EddyP23 ,如果我使用 skd 自动完成和 agm 映射,我该怎么办?
因为它导入索引
src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&callback=initMap">
不幸的是,我不能just delete your old script tag
因为我们的应用程序有一些遗留部分需要存在脚本标签。 我建议惰性加载器在获取之前先检查 api lib 是否存在。 我现在有点卡住了,可能不得不编辑源代码。
下面是我相当讨厌的 hack,它允许 a) 旧的依赖项假设 API 通过硬编码的<script src=""
标签静态包含,并且 b) 避免You have included the Google Maps API multiple times on this page
错误。
实现覆盖
import { ModuleWithProviders } from '@angular/core';
import { AgmCoreModule } from '@agm/core';
import { MapsAPILoader } from '@agm/core/services/maps-api-loader/maps-api-loader';
import { LazyMapsAPILoaderConfigLiteral } from '@agm/core/services/maps-api-loader/lazy-maps-api-loader';
export class AgmCoreOverrideModule {
static forRoot(lazyMapsAPILoaderConfig?: LazyMapsAPILoaderConfigLiteral): ModuleWithProviders {
var module = AgmCoreModule.forRoot(lazyMapsAPILoaderConfig);
// This is the magical path to what we need to override.
// Future releases of Agm Core may totally break this.
let provider: any = module.providers[2];
provider.useClass = IgnoreIncludingApiLoader;
return module;
}
}
export class IgnoreIncludingApiLoader extends MapsAPILoader {
load(): Promise<void> {
// Do absolutely nothing.
return new Promise<void>(resolve => resolve());
}
}
在 app.module 中连接它
@NgModule({
imports: [
AgmCoreOverrideModule.forRoot({
apiKey: '<key>' // Not really needed.
})
],
...
很多天以来,我一直在尝试解决这个问题,最后我通过比较项目中的所有模块文件得出了解决方案。
请检查您的软件包中的所有模块。
如果您在多个模块中发现以下代码,请删除它们并仅将其保留在一个模块中。
<br i="8"/>
platformBrowserDynamic().bootstrapModule(AppModule)<br i="9"/>
.catch(err => console.log(err));<br i="10"/>
最好只保留在 main.ts
希望这个解决方案对你们有帮助
我能够解决@escarabin描述的覆盖模块的场景中的问题。
import { ModuleWithProviders, Inject, Optional, NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { AgmCoreModule, LAZY_MAPS_API_CONFIG, MapsAPILoader } from '@agm/core';
import { LazyMapsAPILoader, LazyMapsAPILoaderConfigLiteral } from '@agm/core/services/maps-api-loader/lazy-maps-api-loader';
import { WindowRef, DocumentRef, BROWSER_GLOBALS_PROVIDERS } from '@agm/core/utils/browser-globals';
@NgModule({ imports: [CommonModule, AgmCoreModule], exports: [AgmCoreModule]})
export class AgmOverrideModule {
static forRoot(lazyMapsAPILoaderConfig?: LazyMapsAPILoaderConfigLiteral): ModuleWithProviders {
return {
ngModule: AgmOverrideModule, providers: [BROWSER_GLOBALS_PROVIDERS, { provide: MapsAPILoader, useClass: ApiLoader },
{ provide: LAZY_MAPS_API_CONFIG, useValue: lazyMapsAPILoaderConfig } ]}
}
}
export class ApiLoader extends LazyMapsAPILoader {
private static cargado: boolean;
constructor( @Optional() @Inject(LAZY_MAPS_API_CONFIG) config: any, @Inject(WindowRef) w: WindowRef, @Inject(DocumentRef) d: DocumentRef) {
super(config, w, d);
}
load(): Promise<void> {
if (ApiLoader.cargado) {
return new Promise<void>(resolve => resolve());
} else {
return new Promise<void>((resolve, reject) => {
super.load()
.then(() => {
ApiLoader.cargado = true;
resolve();
})
.catch(err => {
reject(err);
});
});
}
}
}
并在共享模块中导入模块
@NgModule({
imports: [
...
AgmOverrideModule.forRoot({
apiKey: environment.claveMaps
}),
...
export class ComunModule
并在延迟加载的模块中
```
@NgModule({
进口:[
通讯模块,
AgmOverrideModule,
...
]
太感谢了:)。
2018 年 3 月 19 日星期一晚上 7:03,Juliete notifications@github.com写道:
我能够在@escarabin 的场景中解决问题
https://github.com/escarabin描述了覆盖模块。从'@angular/core'导入{ ModuleWithProviders, Inject, Optional, NgModule };
从“@angular/common”导入 { CommonModule };
从'@agm/core'导入{ AgmCoreModule, LAZY_MAPS_API_CONFIG, MapsAPILoader };
从“@agm/core/services/maps-api-loader/lazy-maps-api-loader”导入 { LazyMapsAPILoader, LazyMapsAPILoaderConfigLiteral };
从“@agm/core/utils/browser-globals”导入 { WindowRef、DocumentRef、BROWSER_GLOBALS_PROVIDERS };
从'@agm/core/core.module'导入{coreDirectives};@NgModule({ 进口: [CommonModule, AgmCoreModule], 出口: [AgmCoreModule]})
导出类 AgmOverrideModule {
静态 forRoot(lazyMapsAPILoaderConfig?: LazyMapsAPILoaderConfigLiteral): ModuleWithProviders {
返回 {
ngModule:AgmOverrideModule,提供者:[BROWSER_GLOBALS_PROVIDERS,{提供:MapsAPILoader,useClass:ApiLoader},
{ 提供:LAZY_MAPS_API_CONFIG,使用值:lazyMapsAPILoaderConfig } ]}
}
}导出类 ApiLoader 扩展 LazyMapsAPILoader {
私人静态货物:布尔值;
构造函数(@Optional()@Inject(LAZY_MAPS_API_CONFIG)配置:任何,@Inject(WindowRef)w:WindowRef,@Inject(DocumentRef)d:DocumentRef){
超级(配置,w,d);}
负载():承诺{
如果(ApiLoader.cargado){
返回新的承诺(解决=>解决());
} 别的 {
返回新的承诺((解决,拒绝)=> {
超负荷()
.then(() => {
ApiLoader.cargado = true;
解决();
})
.catch(错误 => {
拒绝(错误);
});
});
}
}
}并在共享模块中导入模块
AgmOverrideModule.forRoot({ apiKey: environment.claveMaps }),
—
您收到此消息是因为您发表了评论。
直接回复此邮件,在 GitHub 上查看
https://github.com/SebastianM/angular-google-maps/issues/692#issuecomment-374212677 ,
或使线程静音
https://github.com/notifications/unsubscribe-auth/AiXUoVhigXAsP1d3tYQsPwK8UP_T-lfXks5tf7OkgaJpZM4KMHK6
.
我也有库的版本 1.0.0-beta.5 的问题。 我调查了这个问题,但并非在所有情况下都会发生。
这是上下文,我有使用AgmCoreModule.forRoot
的主模块:
@NgModule({
imports: [
(...)
AgmCoreModule.forRoot({
apiKey: environment.googleMaps.apiKey
}),
和一个使用AgmCoreModule
的惰性模块:
@NgModule({
declarations: [
(...)
],
imports: [
(...)
AgmCoreModule,
当首先转到链接到根模块的页面然后转到lay模块的页面时,我可以重现该问题。 如果我直接进入惰性模块中的页面,我没有问题......
我调查了一下这个问题,我发现在这种情况下,agm 的 LayLoader 的load
方法被并行调用了两次:
FitBoundsService
function FitBoundsService(loader) {
var _this = this;
this._boundsChangeSampleTime$ = new BehaviorSubject(200);
this._includeInBounds$ = new BehaviorSubject(new Map());
this.bounds$ = from(loader.load()).pipe(flatMap(function () { return _this._includeInBounds$; }), sample(this._boundsChangeSampleTime$.pipe(switchMap(function (time) { return timer(0, time); }))), map(function (includeInBounds) { return _this._generateBounds(includeInBounds); }), shareReplay(1));
}
GoogleMapsAPIWrapper
GoogleMapsAPIWrapper.prototype.createMap = function (el, mapOptions) {
var _this = this;
return this._zone.runOutsideAngular(function () {
return _this._loader.load().then(function () {
var map = new google.maps.Map(el, mapOptions);
_this._mapResolver(map);
return;
});
});
};
你知道如何解决这个问题吗?
谢谢!
最有用的评论
@escarabin我有类似的问题。 尝试这个。
您需要在根应用程序模块中导入地图,因此您可以执行以下操作:
但是,如果您想在不属于该模块的组件中使用地图,请转到该模块并执行以下操作: