Created
February 11, 2011 00:27
-
-
Save rusticphilosopher/821688 to your computer and use it in GitHub Desktop.
Geolocation blog source
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
var headingAdded = false; | |
var locationAdded = false; | |
Ti.Geolocation.preferredProvider = "gps"; | |
var headingCallback = function(e) | |
{ | |
Ti.API.info("Received heading callback"); | |
} | |
Ti.Geolocation.addEventListener('heading', headingCallback); | |
headingAdded = true; | |
var locationCallback = function(e) | |
{ | |
Ti.API.info("Received location callback"); | |
} | |
Ti.Geolocation.addEventListener('location', locationCallback); | |
locationAdded = true; | |
var removeGeolocationListeners = function(eventType) | |
{ | |
if (headingAdded) { | |
Ti.API.info("removing heading callback on " + eventType); | |
Ti.Geolocation.removeEventListener('heading', headingCallback); | |
headingAdded = false; | |
} | |
if (locationAdded) { | |
Ti.API.info("removing location callback on " + eventType); | |
Ti.Geolocation.removeEventListener('location', locationCallback); | |
locationAdded = false; | |
} | |
} | |
// as the destroy handler will remove the listener, only set | |
// the pause handler to remove if you need battery savings | |
Ti.Android.currentActivity.addEventListener('pause', function(e) { | |
Ti.API.info("pause event received"); | |
removeGeolocationListeners('pause'); | |
}); | |
Ti.Android.currentActivity.addEventListener('destroy', function(e) { | |
Ti.API.info("destroy event received"); | |
removeGeolocationListeners('destroy'); | |
}); | |
Ti.Android.currentActivity.addEventListener('resume', function(e) { | |
Ti.API.info("resume event received"); | |
if (!headingAdded) { | |
Ti.API.info("adding heading callback on resume"); | |
Ti.Geolocation.addEventListener('heading', headingCallback); | |
headingAdded = true; | |
} | |
if (!locationAdded) { | |
Ti.API.info("adding location callback on resume"); | |
Ti.Geolocation.addEventListener('location', locationCallback); | |
locationAdded = true; | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment