Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 30 additions & 13 deletions angular-reverse-geocode.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,37 @@
return {
restrict: 'E',
template: '<div></div>',
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
}
Expand Down
2 changes: 1 addition & 1 deletion demo/demo.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ <h1>
</div>

<script src="https://maps.googleapis.com/maps/api/js?v=3.exp"></script>
<script src="https://code.angularjs.org/1.2.16/angular.js"></script>
<script src="https://code.angularjs.org/1.3.15/angular.js"></script>
<script src="../angular-reverse-geocode.js"></script>
<script src="demo.js"></script>
</body>
Expand Down