Created
February 18, 2015 23:25
-
-
Save NicholasMurray/a522f0fae5eb654c9a5a to your computer and use it in GitHub Desktop.
AngularJS Geolocation Directive
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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