我使用ng-map在angularjs中使用谷歌地图。我想在拖到标记末尾后得到地址。我试图在他们的文件中找到,但没有找到。他们的文件很好,但没有找到我想要的。经过尝试,我得到了lat和log后拖动结束。但我需要地址。所以我要做的是从标记中获取地址,或者我可以转换lat&地址很长。但我没有找到任何代码的地理编码转换lat-long地址。这是代码,我可以从那里lat&长:-
<ng-map>
<marker centered="true" position="current-location" draggable="true" on-dragend="getCurrentLocation()"></marker>
</ng-map>
方法如下:-
$scope.getCurrentLocation = function(){
$scope.pos = this.getPosition();
console.log($scope.pos.lat(),$scope.pos.lng());
}
拖动标记后,请帮我找到地址。
在函数$scope.getCurrentLocation()中,它接收一个参数,您可以使用它来获取标记的位置(lat,lng)。
你的代码是:
$scope.getCurrentLocation = function(event){
console.log(event.latLng.lat());
console.log(event.latLng.lng());
}
有一个在线指令可以进行反向地理编码。这很好,您可以直接在代码中使用,以实现将标记拖动到有效地址后获得的lat/lng转换的目的。
请看一下这个教程,它也有指令代码。
您可以看看这个例子,并充分利用它。谷歌有自己的方法来实现反向地理编码
文件:https://developers.google.com/maps/documentation/javascript/examples/geocoding-reverse
示例:http://plnkr.co/edit/rSKZR8?p=preview
this.doSth = function() {
geocoder.geocode({'location': this.getPosition()}, function(results, status) {
if (status === google.maps.GeocoderStatus.OK) {
if (results[1]) {
console.log(results[1]);
} else {
window.alert('No results found');
}
} else {
window.alert('Geocoder failed due to: ' + status);
}
});
}