Created
July 6, 2013 08:11
-
-
Save joshmcarthur/5939202 to your computer and use it in GitHub Desktop.
An AngularJS service for finding a user's current location and street address (Using navigator.geolocation and OSM)
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
ExampleController = ($scope, LocationService) -> | |
LocationService.locate().then (position) -> | |
$scope.position = position | |
ExampleController.$inject = ["$scope", "LocationService"] | |
YourAngularApp.controller("ExampleController", ExampleController) |
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
<div ng-controller="ExampleController"> | |
<div class="well" ng-show="position"> | |
<h1>{{position.address}}</h1> | |
<span>Within {{position.coords.accuracy}}m</span> | |
</div> | |
<div ng-hide="position"> | |
<h1>Finding your position...</h1> | |
</div> | |
</div> |
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
LocationService = ($q, $rootScope, ReverseGeolocationService) -> | |
locate = -> | |
defer = $q.defer() | |
navigator.geolocation.getCurrentPosition( | |
(position) -> | |
ReverseGeolocationService.findAddress( | |
position.coords.latitude, | |
position.coords.longitude | |
).then (address) -> | |
position = {coords: position.coords} | |
position.address = address | |
defer.resolve(position) | |
, | |
(error) -> | |
defer.reject(error) | |
) | |
defer.promise | |
{locate: locate} | |
LocationService.$inject = ["$q", "$rootScope", "ReverseGeolocationService"] | |
YourAngularApp.factory("LocationService", LocationService) | |
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
ReverseGeolocationService = ($q, $rootScope, $http) -> | |
url = (latitude, longitude) -> | |
"http://nominatim.openstreetmap.org/reverse?format=json&lat=#{latitude}&lon=#{longitude}&zoom=18&addressdetails=0" | |
findAddress = (latitude, longitude) -> | |
defer = $q.defer() | |
$http(method: 'GET', url: url(latitude, longitude)) | |
.success (data) -> | |
defer.resolve(data.display_name) | |
.error (status) -> | |
defer.rejct(status) | |
defer.promise | |
{findAddress: findAddress} | |
ReverseGeolocationService.$inject = ["$q", "$rootScope", "$http"] | |
YourAngularApp.factory "ReverseGeolocationService", ReverseGeolocationService |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment