-
-
Save cloudshooterhuman/43fc99bcb2a0af726d5904e8f75f788d to your computer and use it in GitHub Desktop.
Example of Angular service using ngCordova GeoLocation
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
(function() { | |
var app = angular.module('app', ['ionic', 'ngCordova']); | |
app.factory('GeoService', function($ionicPlatform, $cordovaGeolocation) { | |
var positionOptions = {timeout: 10000, enableHighAccuracy: true}; | |
return { | |
getPosition: function() { | |
return $ionicPlatform.ready() | |
.then(function() { | |
return $cordovaGeolocation.getCurrentPosition(positionOptions); | |
}) | |
} | |
}; | |
}); | |
app.controller('LocationCtrl', function($scope, GeoService) { | |
function showMap(coords) { | |
var mapOptions = { | |
center: { lat: coords.latitude, lng: coords.longitude}, | |
zoom: 8 | |
}; | |
var map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions); | |
} | |
GeoService.getPosition() | |
.then(function(position) { | |
$scope.coords = position.coords; | |
showMap(position.coords); | |
}, function(err) { | |
console.log('getCurrentPosition error: ' + angular.toJson(err)); | |
}); | |
}); | |
app.run(function($ionicPlatform) { | |
$ionicPlatform.ready(function() { | |
if (window.cordova && window.cordova.plugins.Keyboard) { | |
cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true); | |
} | |
if (window.StatusBar) { | |
StatusBar.styleDefault(); | |
} | |
}); | |
}); | |
}()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment