From dac6b749e18b551ea25e0fc645c04267f40a9dd5 Mon Sep 17 00:00:00 2001 From: Mico Date: Wed, 16 Sep 2015 23:30:20 +0800 Subject: [PATCH 1/2] Used scope instead of attributes to avoid curly-brace expression bindings in the DOM. Also used scope so you can watch the scope variables and update the DOM after changes (ex. Promise-based lat-lng coordinates). --- angular-reverse-geocode.js | 29 ++++++++++++++++++----------- demo/demo.html | 2 +- 2 files changed, 19 insertions(+), 12 deletions(-) diff --git a/angular-reverse-geocode.js b/angular-reverse-geocode.js index d8656f9..aba0942 100644 --- a/angular-reverse-geocode.js +++ b/angular-reverse-geocode.js @@ -9,20 +9,27 @@ 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); + scope: { + lat: '=', + lng: '=' + }, + link: function (scope, element) { + scope.$watchGroup(['lat', 'lng'], function () { + var geocoder = new google.maps.Geocoder(); + var latlng = new google.maps.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('Location not found'); + element.text('Geocoder failed due to: ' + status); } - } 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 @@

- + From 75f6b970670b023ed46c463123fc41ffeec27459 Mon Sep 17 00:00:00 2001 From: Mico Date: Mon, 7 Dec 2015 10:47:41 +0800 Subject: [PATCH 2/2] Added mapsAPI promise in cases that it's asynchronously loaded --- angular-reverse-geocode.js | 38 ++++++++++++++++++++++++-------------- 1 file changed, 24 insertions(+), 14 deletions(-) diff --git a/angular-reverse-geocode.js b/angular-reverse-geocode.js index aba0942..bc133f8 100644 --- a/angular-reverse-geocode.js +++ b/angular-reverse-geocode.js @@ -11,25 +11,35 @@ template: '
', scope: { lat: '=', - lng: '=' + lng: '=', + mapApiPromise: '=?' }, link: function (scope, element) { - scope.$watchGroup(['lat', 'lng'], function () { - var geocoder = new google.maps.Geocoder(); - var latlng = new google.maps.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); + 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('Location not found'); + element.text('Geocoder failed due to: ' + status); } - } else { - element.text('Geocoder failed due to: ' + status); - } + }); }); - }); - + } }, replace: true }