google 地图地点自动完成 addEventListener 不起作用
google maps places autocomplete addEventListener doesn't work
我一直在尝试添加 google maps places auto complete in ionic 2 project 以更新用户 location.However,addEventListener 似乎不起作用并且没有控制台错误任何人都可以告诉我哪里错了?
ngAfterViewInit() {
let input = < HTMLInputElement > document.getElementById("auto");
console.log('input', input);
let options = {
componentRestrictions: {
country: 'IN',
types: ['(regions)']
}
}
let autoComplete = new google.maps.places.Autocomplete(input, options);
console.log('auto', autoComplete);
google.maps.event.addListener(autoComplete, 'place_changed', function() {
this.location.loc = autoComplete.getPlace();
console.log('place_changed', this.location.loc);
});
}
<ion-label stacked>Search Location</ion-label>
<input type="text" id="auto" placeholder="Enter Search Location" [(ngModel)]="location.loc" />
index.html
<script src="https://maps.googleapis.com/maps/api/js?key=xxxxxxxxxxxxxx&libraries=places"></script>
尝试使用以下组件检查 autoComplete
上的 place_changed
事件:
import {Component, ViewChild, ChangeDetectorRef} from '@angular/core';
@Component({
selector: 'my-app',
template: `
<div>
<input #auto />
{{ location?.formatted_address | json}}
</div>
`,
})
export class App {
@ViewChild('auto') auto:any;
location: any;
constructor(private ref: ChangeDetectorRef) {
}
ngAfterViewInit(){
let options = {
componentRestrictions: {
country: 'IN'
}
};
let autoComplete = new google.maps.places.Autocomplete(this.auto.nativeElement, options);
console.log('auto', autoComplete);
autoComplete.addListener('place_changed', () => {
this.location = autoComplete.getPlace();
console.log('place_changed', this.location);
this.ref.detectChanges();
});
}
}
由于 place_changed
是在 angular js 之外触发的,我们需要手动触发 angular 使用 ChangeDetectorRef
的变化检测。
您可以使用箭头函数来保留 this
和 ChangeDetectionRef
来检测更改,因为 google 地图事件在 angular 区域外触发:
constructor(private cd: ChangeDetectorRef) { }
google.maps.event.addListener(autoComplete, 'place_changed', () => { // arrow function
this.location.loc = autoComplete.getPlace();
this.cd.detectChanges(); // detect changes
console.log('place_changed', this.location.loc);
});
autoComplete.getPlace();
returns对象,所以可以得到如下地址:
var place = autoComplete.getPlace();
this.location.loc = place.formatted_address;
我一直在尝试添加 google maps places auto complete in ionic 2 project 以更新用户 location.However,addEventListener 似乎不起作用并且没有控制台错误任何人都可以告诉我哪里错了?
ngAfterViewInit() {
let input = < HTMLInputElement > document.getElementById("auto");
console.log('input', input);
let options = {
componentRestrictions: {
country: 'IN',
types: ['(regions)']
}
}
let autoComplete = new google.maps.places.Autocomplete(input, options);
console.log('auto', autoComplete);
google.maps.event.addListener(autoComplete, 'place_changed', function() {
this.location.loc = autoComplete.getPlace();
console.log('place_changed', this.location.loc);
});
}
<ion-label stacked>Search Location</ion-label>
<input type="text" id="auto" placeholder="Enter Search Location" [(ngModel)]="location.loc" />
index.html
<script src="https://maps.googleapis.com/maps/api/js?key=xxxxxxxxxxxxxx&libraries=places"></script>
尝试使用以下组件检查 autoComplete
上的 place_changed
事件:
import {Component, ViewChild, ChangeDetectorRef} from '@angular/core';
@Component({
selector: 'my-app',
template: `
<div>
<input #auto />
{{ location?.formatted_address | json}}
</div>
`,
})
export class App {
@ViewChild('auto') auto:any;
location: any;
constructor(private ref: ChangeDetectorRef) {
}
ngAfterViewInit(){
let options = {
componentRestrictions: {
country: 'IN'
}
};
let autoComplete = new google.maps.places.Autocomplete(this.auto.nativeElement, options);
console.log('auto', autoComplete);
autoComplete.addListener('place_changed', () => {
this.location = autoComplete.getPlace();
console.log('place_changed', this.location);
this.ref.detectChanges();
});
}
}
由于 place_changed
是在 angular js 之外触发的,我们需要手动触发 angular 使用 ChangeDetectorRef
的变化检测。
您可以使用箭头函数来保留 this
和 ChangeDetectionRef
来检测更改,因为 google 地图事件在 angular 区域外触发:
constructor(private cd: ChangeDetectorRef) { }
google.maps.event.addListener(autoComplete, 'place_changed', () => { // arrow function
this.location.loc = autoComplete.getPlace();
this.cd.detectChanges(); // detect changes
console.log('place_changed', this.location.loc);
});
autoComplete.getPlace();
returns对象,所以可以得到如下地址:
var place = autoComplete.getPlace();
this.location.loc = place.formatted_address;