我正在尝试使用angular2-google-maps向我的项目添加自动填充功能。我在我的AppModule中添加了AgmCoreModule.forRoot(带库:'places'),然后在我的组件中添加了自动完成代码。我仍然得到“无法读取属性'自动完成'未定义”错误。我尝试检查全局var google的值,而google.map不包含'places'字段。我对angular2比较新,所以需要一些帮助来理解我所缺少的东西。我的代码是 在AppModule中 从'angular2-google-maps / core'导入{AgmCoreModule};
@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 {
在AppComponent中:
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
};
});
});
});....
答案 0 :(得分:7)
所以,我能够让它发挥作用。在从主要组件进行路由之后,我试图在功能组件中包含具有自动完成功能的地图。我删除了代码
'AgmCoreModule.forRoot({
apiKey: '[API_KEY_REDACTED]',
libraries: ["places"]
}),'
来自app module.ts的并将其添加到功能的module.ts导入中并且工作正常。
答案 1 :(得分:3)
由于Pooja让她工作,我已经为她完成了Angular2 + angular2-google-maps + Autocomplete的工作示例,我想我会在这里添加代码以帮助未来的开发人员寻找类似的东西。
import {
Component,
NgModule,
OnInit,
NgZone
} from '@angular/core';
import {
BrowserModule
} from '@angular/platform-browser';
import {
AgmCoreModule,
MapsAPILoader
} from 'angular2-google-maps/core';
declare var google: any;
@Component({
selector: 'my-app',
styles: [`
.sebm-google-map-container {
height: 300px;
}
`],
template: `
<sebm-google-map
[latitude]="lat"
[longitude]="lng"
[zoom]="zoom"
[disableDefaultUI]="false"
[zoomControl]="true">
<sebm-google-map-marker
*ngFor="let m of markers; let i = index"
(markerClick)="clickedMarker(m.label, i)"
[latitude]="m.lat"
[longitude]="m.lng"
[label]="m.label"
[markerDraggable]="m.draggable"
(dragEnd)="markerDragEnd(m, $event)">
<sebm-google-map-info-window>
<strong>InfoWindow content</strong>
</sebm-google-map-info-window>
</sebm-google-map-marker>
</sebm-google-map>
<input type="text" id="autocompleteInput">
`})
export class App implements OnInit {
constructor(
private _loader: MapsAPILoader,
private _zone: NgZone) {
}
ngOnInit(): void {
this.autocomplete();
}
autocomplete() {
this._loader.load().then(() => {
var autocomplete = new google.maps.places.Autocomplete(document.getElementById("autocompleteInput"), {});
google.maps.event.addListener(autocomplete, 'place_changed', () => {
this._zone.run(() => {
var place = autocomplete.getPlace();
this.markers.push({
lat: place.geometry.location.lat(),
lng: place.geometry.location.lng(),
label: place.name,
});
this.lat = place.geometry.location.lat();
this.lng = place.geometry.location.lng();
console.log(place);
});
});
});
}
// google maps zoom level
zoom: number = 8;
// initial center position for the map
lat: number = 51.673858;
lng: number = 7.815982;
clickedMarker(label: string, index: number) {
console.log(`clicked the marker: ${label || index}`)
}
mapClicked($event: MouseEvent) {
this.markers.push({
lat: $event.coords.lat,
lng: $event.coords.lng
});
}
markerDragEnd(m: marker, $event: MouseEvent) {
console.log('dragEnd', m, $event);
}
markers: marker[] = [];
}
// just an interface for type safety.
interface marker {
lat: number;
lng: number;
label?: string;
draggable: boolean;
}
@NgModule({
imports: [
BrowserModule,
AgmCoreModule.forRoot({
libraries: ['places']
})
],
declarations: [ App ],
bootstrap: [ App ]
})
export class AppModule {}
4/17/2017更新
在版本 1.0.0-beta.0 - green-zebra 中,AGM团队发布了一个重大更改,其组件命名需要更新模板文件,如下所示: / p>
<agm-map
[latitude]="lat"
[longitude]="lng"
[zoom]="zoom"
[disableDefaultUI]="false"
[zoomControl]="true">
<agm-marker
*ngFor="let m of markers; let i = index"
(markerClick)="clickedMarker(m.label, i)"
[latitude]="m.lat"
[longitude]="m.lng"
[label]="m.label"
[markerDraggable]="m.draggable"
(dragEnd)="markerDragEnd(m, $event)">
<agm-info-window>
<strong>InfoWindow content</strong>
</agm-info-window>
</agm-marker>
</agm-map>
<input type="text" id="autocompleteInput">
以上GitHub repo可以找到上述plnkr /代码的更新版本。
答案 2 :(得分:0)
我也有相同的问题,但原因不同。我在模式(弹出式窗口)中使用地图
let address = document.getElementById("location");
let autocomplete = new google.maps.places.Autocomplete(address, {});
就我而言,以上代码在ngOnInit内部运行,问题是文本输入元素仍未创建。 您需要将自动完成代码放入一个函数中,该函数在每次创建元素时都会运行。我尝试了ngAfterViewInit,但没有成功