Created
June 5, 2015 04:21
-
-
Save brunoksato/5e7f9b2916289d1649a4 to your computer and use it in GitHub Desktop.
Gps Enable Cordova Ionic
This file contains 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 GeolocationSync(){ | |
var existLatLong = Application.getPosition(); | |
if(existLatLong === null){ | |
$ionicLoading.show({ | |
templateUrl: 'templates/components/sync.html', | |
scope: $scope | |
}); | |
if(window.cordova !== undefined){ | |
if($cordovaDevice.getDevice().platform === "Android"){ | |
isGpsEnabled(); | |
var intervalPromisse = $interval(function(){ | |
isGpsEnabled(); | |
},4000); | |
} | |
else{ | |
getLocation(); | |
} | |
} | |
else{ | |
getLocation(); | |
} | |
} | |
function isGpsEnabled() { | |
var gpsEnabled = window.plugins.locationAndSettings; | |
gpsEnabled.isGpsEnabled(function(result) { | |
if (result === false) { | |
if($scope.openConfigLocation === false) | |
switchToLocationSettings(); | |
} | |
else{ | |
$interval.cancel(intervalPromisse); | |
getLocation(); | |
} | |
}, function() { | |
alert("error"); | |
}); | |
} | |
function switchToLocationSettings() { | |
var configLocation = window.plugins.locationAndSettings; | |
configLocation.switchToLocationSettings(function(result) { | |
if(result){ | |
$scope.openConfigLocation = true; | |
} | |
}, function() { | |
alert("error"); | |
}); | |
} | |
function getLocation(){ | |
var watchOptions = { | |
frequency : 100000, | |
enableHighAccuracy: true | |
}; | |
var watch = $cordovaGeolocation.watchPosition(watchOptions); | |
watch.then( | |
null, | |
function(err) { | |
console.log(err); | |
}, | |
function(position) { | |
var lat = position.coords.latitude; | |
var lng = position.coords.longitude; | |
Application.setPosition([lat, lng]); | |
$ionicLoading.hide(); | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks a lot mate for the code, really helped.