Ng-map get address after dragging marker

2020-07-18 03:28发布

I am using ng-map to use google map in angularjs. I want to get address safter drag end of marker. I have tried to find in their documentation but did not found for that. Their documentation is good but did not found what i wanted. After trying i got the lat and log after drag end. But i need the address. So what i have to do is to get address from marker or i can convert lat & long to address. But i did not find any code for geocoder to convert lat long to address. Here is the code from where i can the lat & long :-

<ng-map>
    <marker centered="true" position="current-location" draggable="true" on-dragend="getCurrentLocation()"></marker>
</ng-map>

here is the method :-

$scope.getCurrentLocation = function(){
     $scope.pos = this.getPosition();
     console.log($scope.pos.lat(),$scope.pos.lng());
}

Please help me to find the address after dragging the marker.

3条回答
地球回转人心会变
2楼-- · 2020-07-18 04:04

There is a directive online that does the reverse geocoding. It is great and you can directly use in your code to serve the purpose of converting the lat/lng obtained after dragging the marker to a valid address.

Please take a look at this tutorial that has the directive code too.

查看更多
一纸荒年 Trace。
3楼-- · 2020-07-18 04:14

In the function $scope.getCurrentLocation(), it receives a parameter, you can use it to get the location (lat,lng) for the marker.

Your code would be:

$scope.getCurrentLocation = function(event){
        console.log(event.latLng.lat());
        console.log(event.latLng.lng());
}
查看更多
走好不送
4楼-- · 2020-07-18 04:22

You can take a look that this example, and take advantage of it. Google has its own way to achieve reverse geocoding

Documentation: https://developers.google.com/maps/documentation/javascript/examples/geocoding-reverse

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

}
查看更多
登录 后发表回答