Skip to content

Instantly share code, notes, and snippets.

@NicholasMurray
Created February 18, 2015 23:25
Show Gist options
  • Select an option

  • Save NicholasMurray/a522f0fae5eb654c9a5a to your computer and use it in GitHub Desktop.

Select an option

Save NicholasMurray/a522f0fae5eb654c9a5a to your computer and use it in GitHub Desktop.
AngularJS Geolocation Directive
angular
.module('directives', [])
.directive('geolocation', function($window) {
return {
restrict: "E",
template: '<div></div>',
link: function (scope, element, attrs) {
if ($window.navigator && $window.navigator.geolocation) {
$window.navigator.geolocation.getCurrentPosition(function(position) {
element.html('<div>Your geolocation is latitude:<span id="latitude"></span> and longitude:<span id="longitude"></span></div>');
element.find('#latitude').text(position.coords.latitude);
element.find('#longitude').text(position.coords.longitude);
}, function(error) {
element.text("Your geolocation is not available");
});
}
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment