【发布时间】:2021-04-26 13:25:02
【问题描述】:
我正在使用 Angular Material 谷歌地图组件将地图集成到离子/电容器应用程序中。
https://github.com/angular/components/tree/master/src/google-maps
当我开发应用程序时,地图功能可以正常工作。当我尝试为实现 Google 地图的组件运行测试文件时,我收到此错误:
Failed: Namespace google not found, cannot construct embedded google map. Please install the Google Maps JavaScript API: https://developers.google.com/maps/documentation/javascript/tutorial#Loading_the_Maps_API
Error: Namespace google not found, cannot construct embedded google map. Please install the Google Maps JavaScript API: https://developers.google.com/maps/documentation/javascript/tutorial#Loading_the_Maps_API
at new GoogleMap (http://localhost:9876/_karma_webpack_/node_modules/@angular/google-maps/__ivy_ngcc__/fesm2015/google-maps.js:202:1)
at NodeInjectorFactory.GoogleMap_Factory [as factory] (http://localhost:9876/_karma_webpack_/node_modules/@angular/google-maps/__ivy_ngcc__/fesm2015/google-maps.js:441:49)
at getNodeInjectable (http://localhost:9876/_karma_webpack_/node_modules/@angular/core/__ivy_ngcc__/fesm2015/core.js:4029:1)
at instantiateAllDirectives (http://localhost:9876/_karma_webpack_/node_modules/@angular/core/__ivy_ngcc__/fesm2015/core.js:7964:1)
at createDirectivesInstances (http://localhost:9876/_karma_webpack_/node_modules/@angular/core/__ivy_ngcc__/fesm2015/core.js:7330:1)
at ɵɵelementStart (http://localhost:9876/_karma_webpack_/node_modules/@angular/core/__ivy_ngcc__/fesm2015/core.js:13902:1)
at HomePage_Template (ng:///HomePage.js:79:9)
at executeTemplate (http://localhost:9876/_karma_webpack_/node_modules/@angular/core/__ivy_ngcc__/fesm2015/core.js:7303:1)
at renderView (http://localhost:9876/_karma_webpack_/node_modules/@angular/core/__ivy_ngcc__/fesm2015/core.js:7112:1)
at renderComponent (http://localhost:9876/_karma_webpack_/node_modules/@angular/core/__ivy_ngcc__/fesm2015/core.js:8373:1)
karma.conf.js(这是最后的努力,运气不好。):
config.set({
...
plugins: [
...
require('@angular/google-maps')
],
...
});
HTML:
<google-map>
<map-rectangle
*ngIf="showSelectionUI"
[bounds]="bounds"
[options]="options"
></map-rectangle>
<map-marker
#marker="mapMarker"
*ngFor="let position of markerPositions"
[position]="position"
></map-marker>
</google-map>
打字稿:
@ViewChild(GoogleMap) map: GoogleMap;
@ViewChild(MapRectangle) rectangle: MapRectangle;
这是整个规范文件:
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { IonicModule } from '@ionic/angular';
import { HomePage } from './home.page';
import { Storage } from '@ionic/storage';
import {
HttpClientTestingModule,
HttpTestingController
} from '@angular/common/http/testing';
import { GoogleMapsModule, GoogleMap } from '@angular/google-maps';
import { GooglePlaceModule } from 'ngx-google-places-autocomplete';
describe('HomePage', () => {
let component: HomePage;
let fixture: ComponentFixture<HomePage>;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [HomePage],
imports: [
IonicModule.forRoot(),
HttpClientTestingModule,
GoogleMapsModule,
GooglePlaceModule
],
providers: [
{
provide: Storage,
useValue: {
get: () => 'manual'
}
}
]
}).compileComponents();
fixture = TestBed.createComponent(HomePage);
component = fixture.componentInstance;
fixture.detectChanges();
}));
it('should create', () => {
expect(component).toBeTruthy();
});
});
【问题讨论】:
标签: angular google-maps ionic-framework capacitor