我是角度和开发的新手。谁能帮我解决这个任务?我搞砸了从当前位置获取用户地址的任务。当我用标记放大或缩小地图时,我可以得到该位置的正确地址。但是当我在地图上拖放标记时无法获取地址,任何人都可以解决这个问题。
public mapReady(map) {
debugger
map.addListener("dragend", () => {
console.log(this.centerLatitude, this.centerLongitude);
this.apiloader.load().then(() => {
let geocoder = new google.maps.Geocoder;
let latlng = {
lat: this.centerLatitude,
lng: this.centerLongitude
};
geocoder.geocode({
'location': latlng
}, function(results) {
if (results[0]) {
this.currentLocation = results[0].formatted_address;
console.log(this.currentLocation);
} else {
console.log('Not found');
}
});
});
});
}
public centerChanged(coords: google.maps.LatLngLiteral) {
this.centerLatitude = coords.lat;
this.centerLongitude = coords.lng;
this.apiloader.load().then(() => {
let geocoder = new google.maps.Geocoder;
let latlng = {
lat: this.centerLatitude,
lng: this.centerLongitude
};
geocoder.geocode({
'location': latlng
}, function(results) {
if (results[0]) {
this.currentLocation = results[0].formatted_address;
console.log(this.currentLocation);
} else {
console.log('Not found');
}
});
});
}
private setCurrentLocation() {
if ('geolocation' in navigator) {
navigator.geolocation.getCurrentPosition((position) => {
this.lat = position.coords.latitude;
this.lng = position.coords.longitude;
this.marlatitude = position.coords.latitude;
this.marongitude = position.coords.longitude;
this.zoom = 8;
this.centerLatitude = this.marlatitude;
this.centerLongitude = this.marongitude;
this.apiloader.load().then(() => {
let geocoder = new google.maps.Geocoder;
let latlng = {
lat: this.centerLatitude,
lng: this.centerLongitude
};
geocoder.geocode({
'location': latlng
}, function(results) {
if (results[0]) {
this.currentLocation = results[0].formatted_address;
console.log(this.currentLocation);
} else {
console.log('Not found');
}
});
});
console.log(position)
});
}
}
<div class="map">
<agm-map
#agmMap
[latitude]="centerLatitude"
[longitude]="centerLongitude"
[zoom]="zoom"
(mapReady)="mapReady($event)"
(centerChange)="centerChanged($event)">
<agm-marker
[(latitude)]="centerLatitude"
[(longitude)]="centerLongitude"
[markerDraggable]="draggable"
(dragEnd)="mapReady($event)"
(centerChange)="centerChanged($event)">
</agm-marker>