Skip to content

Instantly share code, notes, and snippets.

@stereoket
Created June 4, 2011 21:45
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stereoket/1008393 to your computer and use it in GitHub Desktop.
Save stereoket/1008393 to your computer and use it in GitHub Desktop.
GPS Lookup demo
var GO = {
init: function(){
// Delete the property data
Ti.API.debug('Removing properties');
Ti.App.Properties.removeProperty("latitude");
Ti.App.Properties.removeProperty("longitude");
Ti.App.Properties.removeProperty("geoAccuracy");
Ti.App.Properties.removeProperty("geoTimestamp");
var propTimestamp = Ti.App.Properties.getString("geoTimestamp");
// Number of cycles of the eventListener
GO.evCycles = 3;
// Delete data button
GO.win2 = Ti.UI.currentWindow;
GO.win2.exitOnClose = true;
GO.win2.backgroundColor = "#000";
GO.win2.addEventListener('close',function(){
Ti.API.info("Window closed");
if (headingAdded) {
Ti.API.info("removing heading callback on pause");
Titanium.Geolocation.removeEventListener('heading', headingCallback);
headingAdded = false;
}
if (locationAdded) {
Ti.API.info("removing location callback on pause");
Titanium.Geolocation.removeEventListener('location', locationCallback);
locationAdded = false;
}
});
Ti.API.debug('Using an '+Ti.Platform.osname+' device');
// set scroll view
var newHeight = Ti.Platform.displayCaps.platformHeight - (20* (Ti.Platform.displayCaps.platformHeight/100));
GO.sview = Titanium.UI.createScrollView({
contentWidth:'auto',
contentHeight:'auto',
borderRadius:10,
height: newHeight,
backgroundColor: '#000',
top:0,
showVerticalScrollIndicator:true
});
GO.view = Ti.UI.createView({
backgroundColor:'#336699',
borderRadius:10,
width: Ti.Platform.displayCaps.platformWidth,
height: 1500,
top:10,
layout: 'vertical'
});
GO.sview.add(GO.view);
GO.win2.add(GO.sview);
// get current location
GO.getGeoLocation();
Ti.API.debug('Returned from Geolocation routine');
},
printMessage: function(message, height){
Ti.API.info('Print Message routine');
if(height == null) height = 25;
if(Ti.Platform.displayCaps.platformWidth > 340) {
var fSize = 25;
var hAdj = 25;
} else {
fSize = 11;
hAdj = 0;
}
var printLabel = Titanium.UI.createLabel({
text:message,
color:'#fff',
top:5,
left: 5,
height:height+ hAdj,
width:'90%',
zIndex:10,
font:{fontSize: fSize}
});
GO.view.add(printLabel);
},
getGeoLocation: function(){
GO.undefinedCounter = 0;
GO.counter = 0;
GO.locErrors = 0;
headingAdded = false;
locationAdded = false;
// add iphone specific tests
// check for iPhone 3.2+ to set purpose / iPad ?
if (Titanium.Platform.name == 'iPhone OS') {
var version = Ti.Platform.version.split(".");
var major = parseInt(version[0]);
var minor = parseInt(version[1]);
// can only test this support on a 3.2+ device
if (major > 3 || (major == 3 && minor > 1)) {
Ti.API.debug('Set Geo Purpose');
Ti.Geolocation.purpose = "Finding your location for tweet updates";
}
}
function translateErrorCode(code) {
if (code == null) {
return null;
}
switch (code) {
case Ti.Geolocation.ERROR_LOCATION_UNKNOWN:
return "Location unknown";
case Ti.Geolocation.ERROR_DENIED:
return "Access denied";
case Ti.Geolocation.ERROR_NETWORK:
return "Network error";
case Ti.Geolocation.ERROR_HEADING_FAILURE:
return "Failure to detect heading";
case Ti.Geolocation.ERROR_REGION_MONITORING_DENIED:
return "Region monitoring access denied";
case Ti.Geolocation.ERROR_REGION_MONITORING_FAILURE:
return "Region monitoring access failure";
case Ti.Geolocation.ERROR_REGION_MONITORING_DELAYED:
return "Region monitoring setup delayed";
}
};
//
// IF WE HAVE COMPASS GET THE HEADING
//
if (Titanium.Geolocation.hasCompass){
//
// TURN OFF ANNOYING COMPASS INTERFERENCE MESSAGE
//
Titanium.Geolocation.showCalibration = false;
//
// SET THE HEADING FILTER (THIS IS IN DEGREES OF ANGLE CHANGE)
// EVENT WON'T FIRE UNLESS ANGLE CHANGE EXCEEDS THIS VALUE
Titanium.Geolocation.headingFilter = 90;
//
// GET CURRENT HEADING - THIS FIRES ONCE
//
Ti.Geolocation.getCurrentHeading(function(e)
{
if (e.error)
{
Ti.API.info("Code translation: "+translateErrorCode(e.code));
return;
}
var x = e.heading.x;
var y = e.heading.y;
var z = e.heading.z;
var magneticHeading = e.heading.magneticHeading;
var accuracy = e.heading.accuracy;
var trueHeading = e.heading.trueHeading;
var timestamp = e.heading.timestamp;
Titanium.API.info('geo - current heading: ' + new Date(timestamp) + ' x ' + x + ' y ' + y + ' z ' + z);
});
//
// EVENT LISTENER FOR COMPASS EVENTS - THIS WILL FIRE REPEATEDLY (BASED ON HEADING FILTER)
//
var headingCallback = function(e)
{
if (e.error)
{
Ti.API.info("Code translation: "+translateErrorCode(e.code));
return;
}
var x = e.heading.x;
var y = e.heading.y;
var z = e.heading.z;
var magneticHeading = e.heading.magneticHeading;
var accuracy = e.heading.accuracy;
var trueHeading = e.heading.trueHeading;
var timestamp = e.heading.timestamp;
Titanium.API.info('geo - heading updated: ' + new Date(timestamp) + ' x ' + x + ' y ' + y + ' z ' + z);
};
Titanium.Geolocation.addEventListener('heading', headingCallback);
headingAdded = true;
} else {
Titanium.API.info("No Compass on device");
}
// check GEO file for current location
Ti.API.info('GeoLoc Object Location check');
Ti.Geolocation.accuracy = Ti.Geolocation.ACCURACY_BEST;
Ti.Geolocation.preferredProvider = Ti.Geolocation.PROVIDER_GPS;
Ti.Geolocation.distanceFilter = 10;
//
// GET CURRENT POSITION - THIS FIRES ONCE
//
Titanium.Geolocation.getCurrentPosition(function(e) {
GO.printMessage('Get Position - 1st hit');
if (!e.success || e.error)
{
GO.printMessage('GPS ERROR:' + e.code);
if(Ti.Platform.name !== 'android'){
Ti.API.info("Code translation: "+translateErrorCode(e.code));
}
Ti.API.debug('error ' + JSON.stringify(e.error));
GO.locErrors++;
return;
}
if(e.coords.longitude !== undefined && e.coords.latitude !== undefined){
Ti.App.Properties.setString("latitude",e.coords.latitude);
Ti.App.Properties.setString("longitude",e.coords.longitude);
Ti.App.Properties.setString("geoAccuracy",e.coords.accuracy);
Ti.App.Properties.setString("geoTimestamp",e.coords.timestamp);
Ti.App.Properties.setString("startAccuracy",e.coords.accuracy)
GO.longitude = e.coords.longitude;
GO.latitude = e.coords.latitude;
GO.altitude = e.coords.altitude;
GO.heading = e.coords.heading;
GO.accuracy = e.coords.accuracy;
GO.timestamp = e.coords.timestamp;
GO.altitudeAccuracy = e.coords.altitudeAccuracy;
GO.latlong = e.coords.latitude + ',' + e.coords.longitude;
Titanium.API.info('geo - current location: '
+ new Date(GO.timestamp)
+ ' long '
+ GO.longitude
+ ' lat '
+ GO.latitude
+ ' accuracy '
+ GO.accuracy);
GO.printMessage(GO.latlong);
// Set timeout so that if the location handler can't find an updated value it will default to the last value
GO.printMessage('*** Setting location Timeout ***');
setTimeout(function(){
if (GO.counter < GO.evCycles){
GO.printMessage('Timeout of location - firing external');
GO.printMessage(GO.latlong);
removeLocationListener();
GO.externalMethod();
}
},25000);
} else {
Ti.API.info('Could net get GPS info');
GO.printMessage('Could net get GPS info');
Ti.Geolocation.fireEvent('locationCallback');
}
});
function locationCallback(e){
Ti.API.debug('location event listener triggerred');
var propertyAccuracy = Ti.App.Properties.getString("geoAccuracy");
if (!e.success || e.error)
{
GO.printMessage('GPS CALLBACK ERROR:' + e.code);
if(Ti.Platform.name !== 'android'){
Ti.API.info("Callback Code translation: "+translateErrorCode(e.code));
}
Ti.API.debug('error ' + JSON.stringify(e.error));
GO.locErrors++;
return;
}
function IsNumeric(input){
return (input - 0) == input && input.length > 0;
}
Ti.API.info('Stored accuracy value: '+ propertyAccuracy);
GO.printMessage('Stored accuracy value: '+ propertyAccuracy);
if(e.coords.longitude !== undefined &&
e.coords.latitude !== undefined &&
e.coords.accuracy < propertyAccuracy) {
GO.printMessage('Location Callback trigger (acc:'+e.coords.accuracy+')');
// First check properties cache
GO.longitude = e.coords.longitude;
GO.latitude = e.coords.latitude;
GO.altitude = e.coords.altitude;
GO.heading = e.coords.heading;
GO.accuracy = e.coords.accuracy;
GO.timestamp = e.coords.timestamp;
GO.altitudeAccuracy = e.coords.altitudeAccuracy;
GO.latlong = e.coords.latitude + ',' + e.coords.longitude;
GO.counter++;
Ti.App.Properties.setString("latitude",e.coords.latitude);
Ti.App.Properties.setString("longitude",e.coords.longitude);
Ti.App.Properties.setString("geoAccuracy",e.coords.accuracy);
Ti.App.Properties.setString("geoTimestamp",e.coords.timestamp);
GO.printMessage('Location callback fired: ' + GO.counter + ' times');
GO.printMessage(GO.latlong);
Titanium.API.info('geo - location updated: '
+ new Date(GO.timestamp)
+ ' long '
+ GO.longitude
+ ' lat '
+ GO.latitude
+ ' accuracy '
+ GO.accuracy);
Ti.API.info('Location callback fired: ' + GO.counter + ' times');
if (GO.counter > GO.evCycles ||
(GO.counter > GO.evCycles && e.accuracy < 10)) {
var latitude = Ti.App.Properties.getString("latitude");
var longitude = Ti.App.Properties.getString("longitude");
removeLocationListener();
setTimeout(function(){
Titanium.API.info('Using the following data for update: '+ GO.latitude + ','+ GO.longitude);
GO.externalMethod();
},500);
}
} else {
Titanium.API.info('Undefined geo data returned from handler');
GO.printMessage('Undefined GPS info');
GO.undefinedCounter++;
}
Ti.API.debug(Ti.Network.networkTypeName + ' is network type');
};
setTimeout(function(){
Titanium.Geolocation.addEventListener('location',locationCallback);
GO.printMessage('Location Listener attached');
locationAdded = true;
setTimeout(function(){
GO.printMessage('Firing Location Callback for first time');
Ti.Geolocation.fireEvent('locationCallback');
},2000);
},300);
function removeLocationListener(){
if (headingAdded) {
GO.printMessage('removing heading callback');
Ti.API.info("removing heading callback on destroy");
Titanium.Geolocation.removeEventListener('heading', headingCallback);
headingAdded = false;
}
if (locationAdded) {
Ti.API.info("removing location callback on pause");
GO.printMessage('Removing location handler');
Titanium.Geolocation.removeEventListener('location', locationCallback);
locationAdded = false;
}
}
},
externalMethod: function(){
GO.printMessage('External Method called');
GO.printMessage('************************');
GO.printMessage('GPS Summary');
GO.printMessage(GO.undefinedCounter+' Misfires of GPS location listener');
GO.printMessage(GO.locErrors+ ' GPS Errors encontered');
GO.printMessage(GO.counter+ ' Location Events fired');
GO.printMessage('Final Latitude\n' +GO.latitude, 70);
GO.printMessage('Accuracy '+ Ti.App.Properties.getString("startAccuracy") + ' > '+ GO.accuracy);
GO.printMessage('************************');
// Output for console
Ti.API.info('External Method called');
Ti.API.info('************************');
Ti.API.info('GPS Summary');
Ti.API.info(GO.undefinedCounter+' Misfires of GPS location listener');
Ti.API.info(GO.locErrors+ ' GPS Errors encontered');
Ti.API.info(GO.counter+ ' Location Events fired');
Ti.API.info('Final Latitude\n' +GO.latitude+ '\n', 70);
Ti.API.info('Final Longitude\n' + GO.longitude+ '\n', 70);
Ti.API.info('Accuracy '+ Ti.App.Properties.getString("startAccuracy") + ' > '+ GO.accuracy);
Ti.API.info('************************');
}
}
GO.init();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment