我想知道是否可以使用angular2-google-maps库从位置(逻辑和纬度)获取国家/地区代码或国家/地区。
private setCurrentPosition() {
if ("geolocation" in navigator) {
navigator.geolocation.getCurrentPosition((position) => {
this.latitude = position.coords.latitude;
this.longitude = position.coords.longitude;
this.zoom = 12;
});
}
}
答案 0 :(得分:1)
您必须反转地理编码以获取此类信息
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 = "";
}
可能有更好的方法,但这就是我的工作方式。