4

我正在使用ng-map在 angularjs 中使用谷歌地图。我想在标记拖动结束后获取地址。我试图在他们的文档中找到,但没有找到。他们的文档很好,但没有找到我想要的。尝试后,我得到了纬度并在拖动结束后登录。但我需要地址。所以我要做的是从标记中获取地址,或者我可以将 lat & long 转换为地址。但我没有找到任何地理编码器将经纬度转换为地址的代码。这是我可以从中获取纬度和经度的代码:-

<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());
}

请帮我在拖动标记后找到地址。

4

3 回答 3

5

在函数 $scope.getCurrentLocation() 中,它接收一个参数,您可以使用它来获取标记的位置 (lat,lng)。

您的代码将是:

$scope.getCurrentLocation = function(event){
        console.log(event.latLng.lat());
        console.log(event.latLng.lng());
}
于 2016-07-12T07:52:45.443 回答
4

有一个在线指令可以进行反向地理编码。这很棒,您可以直接在代码中使用,以实现将标记拖动到有效地址后获得的纬度/经度转换的目的。

请查看包含指令代码的本教程。

于 2015-12-09T02:11:23.143 回答
1

你可以看看这个例子,并利用它。谷歌有自己的方式来实现反向地理编码

文档: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);
        }
      });

}
于 2015-12-09T15:29:06.297 回答