【问题标题】:Ng-map get address after dragging markerNg-map 拖动标记后获取地址
【发布时间】:2016-03-13 04:05:39
【问题描述】:

我正在使用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());
}

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

【问题讨论】:

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


    【解决方案1】:

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

    您的代码将是:

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

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

        【解决方案3】:

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

        请看一下这个也有指令代码的tutorial

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2017-09-16
          • 1970-01-01
          • 1970-01-01
          • 2018-06-01
          • 2012-10-01
          • 2015-12-29
          • 2011-03-16
          • 1970-01-01
          相关资源
          最近更新 更多