Skip to content

Instantly share code, notes, and snippets.

@rpavez
Created October 1, 2014 14:21
Show Gist options
  • Save rpavez/c0aa5d14812f6012be7c to your computer and use it in GitHub Desktop.
Save rpavez/c0aa5d14812f6012be7c to your computer and use it in GitHub Desktop.
BasicGeo with iOS8 locationServicesAuthorization
var basicgeo = require("bencoding.basicgeo");
var available = basicgeo.createAvailability();
function sendLocation(location) {
/*
xhr.send({
latitude : location.coords.latitude,
longitude : location.coords.longitude
});
*/
}
//Define our location monitor object
var locationMonitor = {
module : null,
errorEvt : function(e) {
Ti.API.info("Location Monitor Error " + JSON.stringify(e));
},
changeEvt : function(e) {
Ti.API.info("ChangeEvt!\n"+JSON.stringify(e));
sendLocation(e);
},
startEvt : function(e) {
Ti.API.info("Monitor startEvt");
},
stopEvt : function(e) {
Ti.API.info("Monitor stopEvt");
},
timerEvt : function(e) {
Ti.API.info("Timer fired!");
},
start : function() {
//First we start everything up
if (locationMonitor.module == null) {
locationMonitor.module = require("bencoding.basicgeo").createLocationMonitor();
locationMonitor.module.addEventListener('error', locationMonitor.errorEvt);
locationMonitor.module.addEventListener('start', locationMonitor.startEvt);
locationMonitor.module.addEventListener('stop', locationMonitor.stopEvt);
locationMonitor.module.addEventListener('change', locationMonitor.changeEvt);
locationMonitor.module.addEventListener('timerFired', locationMonitor.timerEvt);
Ti.API.info('Location Monitor Listeners Added');
}
//Add our configuration parameters
locationMonitor.module.purpose = "GSP Purpose";
locationMonitor.module.staleLimit = 5;
locationMonitor.module.accuracy = Ti.Geolocation.ACCURACY_LOW;
locationMonitor.module.distanceFilter = 300;
//Start monitoring for changes
locationMonitor.module.startMonitoring();
},
stop : function() {
if (locationMonitor.module != null) {
locationMonitor.module.stopMonitoring();
Ti.API.info('locationMonitor Stopped');
locationMonitor.module.removeEventListener('error', locationMonitor.errorEvt);
locationMonitor.module.removeEventListener('start', locationMonitor.startEvt);
locationMonitor.module.removeEventListener('stop', locationMonitor.stopEvt);
locationMonitor.module.removeEventListener('change', locationMonitor.changeEvt);
locationMonitor.module.removeEventListener('timerFired', locationMonitor.timerEvt);
Ti.API.info('locationMonitor Listeners Removed');
locationMonitor.module = null;
}
}
};
gpsAuthStarted = false;
function gpsAuthorized(){
alert(printAuthorizedResults(Ti.Geolocation.locationServicesAuthorization));
locationMonitor.start();
};
setInterval(function(){
if(Ti.Geolocation.locationServicesAuthorization == Ti.Geolocation.AUTHORIZATION_ALWAYS && !gpsAuthStarted)
{
gpsAuthStarted = true;
gpsAuthorized();
}
},500);
setTimeout(function() {
if(Ti.Geolocation.locationServicesAuthorization != Ti.Geolocation.AUTHORIZATION_ALWAYS)
{ Ti.Geolocation.requestAuthorization(Titanium.Geolocation.AUTHORIZATION_ALWAYS); }
}, 500);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment