我正在使用 agm-map,下面是 AutoComplete 的代码:
autocomplete.addListener('place_changed', () => {
this.mapSearched = true;
this.mapDragged = false;
this.ngZone.run(() => {
let place: PlaceResult = autocomplete.getPlace();
console.log(place);
if (place.geometry === undefined || place.geometry === null) {
return;
}
this.latitude = place.geometry.location.lat();
this.longitude = place.geometry.location.lng();
this.zoom = 12;
this.getAddress(place);
});
});
您可以看到 .getPlace 返回确切的PlaceResult 类型的单个对象作为地址。geoCoding 返回地址、组件的列表。
所以我想在没有 geoCoder 的情况下使用纬度和经度获取地址(因为它返回列表),结果是准确的单个对象,就像PlaceResult一样。下面是 GeoCoder 和 console.log(results) -> 列表地址组件。
this.geoCoder.geocode(
{ location: { lat: this.latitude, lng: this.longitude }, },
(results: any, status: string) => {
if (status === 'OK') {
// this is the address response which is address list
console.log(results)
} else {
window.alert('Geocoder failed due to: ' + status);
}
}
);