拖动标记后,Ng-map获取地址

时间:2015-12-08 09:39:22

标签: javascript angularjs google-maps-api-3 ng-map

我正在使用ng-map在angularjs中使用谷歌地图。我想在拖拽标记结束时获取地址。我试图在他们的文档中找到但没有找到。他们的文件很好,但没有找到我想要的东西。在尝试后我得到lat并在拖动结束后记录。但我需要地址。所以我要做的是从标记获取地址或我可以转换lat&很久没有解决。但我没有找到地理编码器的任何代码来转换lat long来解决。这是我可以在哪里使用lat和amp的代码。长: -

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

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

3 个答案:

答案 0 :(得分:4)

网上有指令进行反向地理编码。这很棒,您可以直接在代码中使用,以便在将标记拖动到有效地址后转换lat / lng。

请查看具有指令代码的tutorial

答案 1 :(得分:4)

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

您的代码将是:

$scope.getCurrentLocation = function(event){
        console.log(event.latLng.lat());
        console.log(event.latLng.lng());
}

答案 2 :(得分: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);
        }
      });

}
相关问题