-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathangular-reverse-geocode.js
More file actions
39 lines (37 loc) · 1.53 KB
/
angular-reverse-geocode.js
File metadata and controls
39 lines (37 loc) · 1.53 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
/**
* AngularJS reverse geocoding directive
* @author Jason Watmore <jason@pointblankdevelopment.com.au> (http://jasonwatmore.com)
* @author Larry Stone <larry@symbolshift.com>
* @version 2.0.0
*/
(function () {
angular.module('angularReverseGeocode', []).component('reverseGeocode', {
bindings: {
lat: '@',
lng: '@',
geocodeResultsIndex: '@?',
locationNotFoundText: '@?',
geocodeFailureText: '@?'
},
controller: function ($scope) {
var vm = this;
var defaultResultsIndex = 0;
var defaultLocationNotFoundText = 'Location not found';
var defaultGeocodeFailureText = 'Geocoder failed due to: ';
var geocoder = new google.maps.Geocoder();
var latlng = new google.maps.LatLng(vm.lat, vm.lng);
var resultsIndex = vm.geocodeResultsIndex || defaultResultsIndex;
geocoder.geocode({ 'latLng': latlng }, function (results, status) {
if (status === 'OK') {
vm.address = results[resultsIndex].formatted_address;
} else if (status === 'ZERO_RESULTS') {
vm.address = vm.locationNotFoundText || defaultLocationNotFoundText
} else {
vm.address = vm.geocodeFailureText || (defaultGeocodeFailureText + status);
}
$scope.$apply();
});
},
template: '<div class="reverse-geocoded-address">{{$ctrl.address}}</div>'
});
})();