diff --git a/angular-reverse-geocode.js b/angular-reverse-geocode.js index d8656f9..bc133f8 100644 --- a/angular-reverse-geocode.js +++ b/angular-reverse-geocode.js @@ -9,20 +9,37 @@ return { restrict: 'E', template: '
', - link: function (scope, element, attrs) { - var geocoder = new google.maps.Geocoder(); - var latlng = new google.maps.LatLng(attrs.lat, attrs.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); - } + 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 } diff --git a/demo/demo.html b/demo/demo.html index 462891a..776d06b 100644 --- a/demo/demo.html +++ b/demo/demo.html @@ -15,7 +15,7 @@