私は自分のプロジェクトにangular2-google-mapsを使ってオートコンプリートを追加しようとしています。私はAppModuleにAgmCoreModule.forRoot(ライブラリ: 'places')を追加して、コンポーネントにオートコンプリートコードを追加しました。それでも私は "未定義のAutocomplete 'プロパティを読み取ることができません"エラーが発生します。グローバル変数googleの値をチェックしようとしましたが、google.mapには 'places'フィールドが含まれていません。私はangular2には比較的新しいので、私が紛失しているものを理解するには何らかの助けが必要です。私のコードはAppModuleに です
'{AngmCoreModule} from' angular2-google-maps/core '; AppComponentでangular2-google-maps autocomplete not working
@NgModule({
bootstrap: [ App ],
declarations: [
App,
ErrorComponent,
],
imports: [ // import Angular's modules
AgmCoreModule.forRoot({
apiKey: '[API_KEY_REDACTED]',
libraries: ["places"]
}),
BrowserModule,
FormsModule,
ReactiveFormsModule,
HttpModule,
TranslateModule.forRoot(),
RouterModule.forRoot(ROUTES, { useHash: true })
],
providers: [ // expose our Services and Providers into Angular's dependency injection
ENV_PROVIDERS,
APP_PROVIDERS,
]
})
export class AppModule {
:だから
import {MapsAPILoader} from 'angular2-google-maps/core';
import { NgZone } from '@angular/core';
declare var google: any;
export class EventInfoTab {
@ViewChild('gmap') gmap:any;
constructor(
private _loader: MapsAPILoader,
private zone : NgZone,
)
ngAfterViewInit(): void {
this._loader.load().then(() => {
let address = document.getElementById("location");
console.log("google", google);
let autocomplete = new google.maps.places.Autocomplete(address, {});
console.log ("autocomplete",autocomplete);
google.maps.event.addListener(autocomplete, 'place_changed',() => {
this.zone.run(() => {
console.log ("autocomplete place_changed",autocomplete);
var place = autocomplete.getPlace();
this.lat = place.geometry.location.lat();
this.lng = place.geometry.location.lng();
//alert(JSON.stringify(place));
this.markers[0] ={
lat: this.lat,
lng: this.lng,
label: 'x',
draggable: false
};
});
});
});....
プロジェクトに '@ types/googlemaps'をインストールしましたか? –
はい。 @ types/googlemapsをインストールしました。まだ同じエラーが発生しています。 –