forked from cornflourblue/angular-reverse-geocode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathangular-reverse-geocode.js
More file actions
47 lines (44 loc) · 1.71 KB
/
angular-reverse-geocode.js
File metadata and controls
47 lines (44 loc) · 1.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/**
* AngularJS reverse geocoding directive
* @author Jason Watmore <jason@pointblankdevelopment.com.au> (http://jasonwatmore.com)
* @version 1.0.0
*/
(function () {
angular.module('angularReverseGeocode', [])
.directive('reverseGeocode', function () {
return {
restrict: 'E',
template: '<div></div>',
scope: {
lat: '=',
lng: '=',
mapApiPromise: '=?'
},
link: function (scope, element) {
if (!scope.mapApiPromise) return watchLatLng();
scope.mapApiPromise.then(function (maps) {
watchLatLng(maps);
});
function watchLatLng(mapApi) {
var mapsApi = mapApi || google.maps;
scope.$watchGroup(['lat', 'lng'], function () {
var geocoder = new mapsApi.Geocoder();
var latlng = new mapsApi.LatLng(scope.lat, scope.lng);
geocoder.geocode({ 'latLng': latlng }, function (results, status) {
if (status == google.maps.GeocoderStatus.OK) {
if (results[1]) {
element.text(results[1].formatted_address);
} else {
element.text('Location not found');
}
} else {
element.text('Geocoder failed due to: ' + status);
}
});
});
}
},
replace: true
}
});
})();