Created
May 13, 2016 19:26
-
-
Save kettleio/5b74418c5a6f149ccbc7fcacc1980b8f to your computer and use it in GitHub Desktop.
Compass controller
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
.controller('compassCtrl', function($scope, $ionicPlatform, Location) { | |
$scope.$on('$ionicView.afterLeave', function(){ | |
console.log("Before leaving clear the compass"); | |
if(positionTimerId) navigator.geolocation.clearWatch(positionTimerId); | |
if(compassTimerId) navigator.compass.clearWatch(compassTimerId); | |
}); | |
$scope.location = Location.get(); | |
console.log($scope.location); | |
$scope.isError = false; | |
var destinationPosition; | |
var destinationBearing; | |
var positionTimerId; | |
var currentPosition; | |
var prevPosition; | |
var prevPositionError; | |
var compassTimerId; | |
var currentHeading; | |
var prevHeading; | |
var prevCompassErrorCode; | |
$scope.initCompass = function(){ | |
minPositionAccuracy = 50; // Minimum accuracy in metres to accept as a reliable position | |
$scope.watchPosition(); | |
$scope.watchCompass(); | |
$scope.updateDestination(); | |
}; | |
$scope.watchPosition = function(){ | |
if(positionTimerId) navigator.geolocation.clearWatch(positionTimerId); | |
positionTimerId = navigator.geolocation.watchPosition($scope.onPositionUpdate, $scope.onPositionError, { | |
enableHighAccuracy: true, | |
timeout: 1000, | |
maxiumumAge: 0 | |
}); | |
} | |
$scope.watchCompass = function(){ | |
if(compassTimerId) navigator.compass.clearWatch(compassTimerId); | |
compassTimerId = navigator.compass.watchHeading($scope.onCompassUpdate, $scope.onCompassError, { | |
frequency: 100 // Update interval in ms | |
}); | |
} | |
$scope.onPositionUpdate = function(position){ | |
if(position.coords.accuracy > minPositionAccuracy) return; | |
prevPosition = currentPosition; | |
currentPosition = new LatLon(position.coords.latitude, position.coords.longitude); | |
$scope.updatePositions(); | |
} | |
$scope.onPositionError = function(error){ | |
$scope.watchPosition(); | |
if(prevPositionError && prevPositionError.code == error.code && prevPositionError.message == error.message){ | |
$scope.isError = false; | |
return ; | |
} | |
$scope.isError = true; | |
$scope.errorMessage = "Error while retrieving current position. <br/>Error code: " + error.code + "<br/>Message: " + error.message; | |
prevPositionError = { | |
code: error.code, | |
message: error.message | |
}; | |
} | |
$scope.onCompassUpdate = function(heading){ | |
prevHeading = currentHeading; | |
currentHeading = Math.round(heading.magneticHeading); | |
if(currentHeading == prevHeading) return; | |
$scope.updateHeading(); | |
} | |
function onCompassError(error){ | |
console.log('COMPASS ERROR'); | |
console.log(error); | |
$scope.watchCompass(); | |
if(prevCompassErrorCode && prevCompassErrorCode == error.code) return; | |
var errorType; | |
switch(error.code){ | |
case 1: | |
errorType = "Compass not supported"; | |
break; | |
case 2: | |
errorType = "Compass internal error"; | |
break; | |
default: | |
errorType = "Unknown compass error"; | |
} | |
$scope.isError = true; | |
$scope.errorMessage = "Error while retrieving compass heading: "+errorType; | |
prevCompassErrorCode = error.code; | |
} | |
$scope.updateDestination = function(){ | |
destinationPosition = new LatLon($scope.location.lat, $scope.location.lng); | |
$scope.updatePositions(); | |
} | |
$scope.updatePositions = function(){ | |
if(!currentPosition) return; | |
$scope.isError = false; | |
destinationBearing = Math.round(currentPosition.bearingTo(destinationPosition)); | |
$scope.distance = Math.round(currentPosition.distanceTo(destinationPosition)*1000); | |
$scope.bearing = destinationBearing; | |
$scope.updateDifference(); | |
} | |
$scope.updateHeading = function(){ | |
$scope.heading = currentHeading; | |
$scope.updateDifference(); | |
} | |
$scope.updateDifference = function(){ | |
var diff = destinationBearing - currentHeading; | |
$scope.difference = diff; | |
$scope.animateRotation = "-webkit-transform: rotate("+diff+"deg)"; | |
$scope.$apply(); | |
} | |
$ionicPlatform.ready(function() { | |
$scope.initCompass(); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment