我正在使用角度2-谷歌地图。
如何从带有typescript的angular2谷歌地图中的纬度,经度获取国家和地区等地址?
小智 7
getCurrentLocation() {
this.mapsAPILoader.load().then(() => {
let geocoder = new google.maps.Geocoder;
let latlng = {lat: this.currentBusiness.latitude, lng: this.currentBusiness.longitude};
let that = this;
geocoder.geocode({'location': latlng}, function(results) {
if (results[0]) {
that.zoom = 11;
that.currentLocation = results[0].formatted_address;
//console.log(that.currentLocation);
} else {
console.log('No results found');
}
});
});
Run Code Online (Sandbox Code Playgroud)
}
您必须重新分配“ this”,因为它丢失了参考
我希望这可以帮助你
使用Angular Google Maps,您可以执行以下操作:
getGeoLocation(lat: number, lng: number) {
if (navigator.geolocation) {
let geocoder = new google.maps.Geocoder();
let latlng = new google.maps.LatLng(lat, lng);
let request = {
latLng: latlng
};
geocoder.geocode(request, (results, status) = > {
if (status == google.maps.GeocoderStatus.OK) {
let result = results[0];
let rsltAdrComponent = result.address_components;
let resultLength = rsltAdrComponent.length;
if (result != null) {
this.marker.buildingNum = rsltAdrComponent.find(x = > x.types == 'street_number').long_name;
this.marker.streetName = rsltAdrComponent.find(x = > x.types == 'route').long_name;
} else {
alert("No address available!");
}
}
});
}
}
Run Code Online (Sandbox Code Playgroud)
*确保您全局公开google
对象:declare var google: any;
进而:
mapClicked($event: MouseEvent) {
this.marker.lat = $event.coords.lat;
this.marker.lng = $event.coords.lng;
this.getGeoLocation(this.marker.lat, this.marker.lng);
console.log("Lat: " + this.marker.lat + " Long: " + this.marker.lng)
}
Run Code Online (Sandbox Code Playgroud)
组件/HTML:
<agm-map [latitude]="marker.lat || centerLat" [longitude]="marker.lng || centerLng" [zoom]="8" (mapClick)="mapClicked($event)">
<agm-marker *ngIf="marker.lat" (markerClick)="clickedMarker(marker.label)" [latitude]="marker.lat" [longitude]="marker.lng" [markerDraggable]="marker.draggable" (dragEnd)="markerDragEnd(marker, $event)">
<agm-info-window [disableAutoPan]="true">
{{marker.label}}
</agm-info-window>
</agm-marker>
</agm-map>
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
13116 次 |
最近记录: |