您必须反向地理编码才能获得此类信息
map.service.ts
getRevGeoLocation(lat: number, lng: number): Observable<google.maps.GeocoderResult> {
if (navigator.geolocation) {
let geocoder = new google.maps.Geocoder();
let latlng = new google.maps.LatLng(lat, lng);
let request = {
latLng: latlng
};
return Observable.create(observer => {
geocoder.geocode(request, (results, status) => {
if (status == google.maps.GeocoderStatus.OK) {
console.log(results[0]);
observer.next(results[0])
observer.complete();
} else {
console.log('Error: ', results, ' & Status: ', status);
observer.error();
}
});
});
}
}
map.component.ts
callRevGeoLocate(lat: number, lng: number) {
this._mapsService.getRevGeoLocation(lat, lng).subscribe(
results => {
this._zone.run(() => {
this.fillInputs(results.formatted_address);
});
}
);
fillInputs(formatted_address: string) {
let addrElements = formatted_address.split(",");
let provPostalCode = addrElements[2].split(" ");
this.marker.draggable = true;
this.marker.label = formatted_address;
this.marker.buildingNum = addrElements[0].substr(0, addrElements[0].indexOf(' '));
this.marker.streetName = addrElements[0].substr(addrElements[0].indexOf(' ') + 1);
this.marker.city = addrElements[1].trim();
this.marker.region = provPostalCode[1];
if (provPostalCode.length == 4)
this.marker.postalCode = provPostalCode[2] + provPostalCode[3];
else if (provPostalCode.length == 3)
this.marker.postalCode = provPostalCode[2];
else
this.marker.postalCode = "";
}
可能有更好的方法,但这就是我的工作方式。