Skip to content

Instantly share code, notes, and snippets.

@mirkonasato
Last active March 22, 2017 07:12
Show Gist options
  • Save mirkonasato/08789b5a724f5db9db72 to your computer and use it in GitHub Desktop.
Save mirkonasato/08789b5a724f5db9db72 to your computer and use it in GitHub Desktop.
Example of Angular service using ngCordova GeoLocation
(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();
}
});
});
}());
@jlflores
Copy link

jlflores commented Jul 8, 2015

Thank you, I tested something similar but didn't work so I added a setTimeout with 10 milliseconds for delay and that worked.
I will try this again

@sugir93
Copy link

sugir93 commented Apr 20, 2016

hi, am facing problem in the watcher,
the watcher is triggering even if the movement is not happened.
can u help me to fix this issue?
thanks in advance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment