Created
March 18, 2011 21:33
-
-
Save stereoket/876893 to your computer and use it in GitHub Desktop.
A titanium app built to test geo capabilities on Android (currently buggy on nadroid, fine for iOS)
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 GeoTest = { | |
init: function(){ | |
Ti.API.debug('Using an '+Ti.Platform.osname+' device'); | |
GeoTest.win2 = Ti.UI.createWindow({ | |
backgroundColor:"#d4fdfd", | |
title: 'GPS Android Test' | |
}); | |
GeoTest.win2.open(); | |
var gpsTestLabel = Titanium.UI.createLabel({ | |
text:'GPS Test started', | |
font:{fontSize:11}, | |
color:'#444', | |
top:170, | |
left:10, | |
height:25, | |
width:300, | |
zIndex:10, | |
font:{fontSize: 20} | |
}); | |
GeoTest.win2.add(gpsTestLabel); | |
// get current location | |
GeoTest.getGeoLocation(); | |
Ti.API.debug('Returned from Geolocation routine'); | |
}, | |
getGeoLocation: function(){ | |
// include geo object | |
Ti.Geolocation.preferredProvider = "gps"; | |
Ti.API.debug('Geolocation Lookup for tweet incident'); | |
var isIPhone3_2_Plus = function() | |
{ | |
// add iphone specific tests | |
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)) | |
{ | |
return true; | |
} | |
} | |
return false; | |
}; | |
// check for iPhone 3.2+ to set purpose | |
if (isIPhone3_2_Plus()){ | |
Ti.API.debug('Set Geo Purpose'); | |
Ti.Geolocation.purpose = "Finding your location for tweet updates"; | |
} | |
// check GEO file for current location | |
Ti.API.info('GeoLoc Object Location check'); | |
Ti.Geolocation.accuracy = Ti.Geolocation.ACCURACY_BEST; | |
Ti.Geolocation.distanceFilter = 10; | |
// | |
// GET CURRENT POSITION - THIS FIRES ONCE | |
// | |
Titanium.Geolocation.getCurrentPosition(function(e){ | |
var gpsTestLabel = Titanium.UI.createLabel({ | |
text:'Get Current Position', | |
font:{fontSize:11}, | |
color:'#444', | |
top:220, | |
left:10, | |
height:25, | |
width:300, | |
zIndex:10, | |
font:{fontSize: 20} | |
}); | |
GeoTest.win2.add(gpsTestLabel); | |
if (!e.success || e.error){ | |
Ti.API.debug('Could not get Current position '+JSON.stringify(e.error)); | |
} | |
}); | |
Titanium.Geolocation.addEventListener('location',GeoTest.assignLatLong); | |
}, | |
assignLatLong: function(e){ | |
Ti.API.debug('location event listener triggerred'); | |
var gpsTestLabel = Titanium.UI.createLabel({ | |
text:'Location Callback trigger', | |
font:{fontSize:11}, | |
color:'#444', | |
top:270, | |
left:10, | |
height:25, | |
width:300, | |
zIndex:10, | |
font:{fontSize: 20} | |
}); | |
GeoTest.win2.add(gpsTestLabel); | |
if (!e.success || e.error){ | |
return; | |
} | |
function IsNumeric(input){ | |
return (input - 0) == input && input.length > 0; | |
} | |
GeoTest.latitude = e.coords.latitude; | |
GeoTest.longitude = e.coords.longitude; | |
GeoTest.latlong = e.coords.latitude + ',' + e.coords.longitude; | |
GeoTest.accuracy = e.coords.accuracy; | |
GeoTest.timestamp = e.coords.timestamp; | |
Ti.API.info(e.type + ' handler (' + e.source + ') Accuracy: ' + GeoTest.accuracy); | |
setTimeout(function(e){ | |
GeoTest.assignLocationValues(GeoTest.latitude,GeoTest.longitude); | |
Titanium.API.info('Last location: ' + | |
new Date(GeoTest.timestamp) + | |
' long ' + | |
GeoTest.longitude + | |
' lat ' + | |
GeoTest.latitude + | |
' accuracy ' + | |
GeoTest.accuracy); | |
}, 1000); | |
}, | |
assignLocationValues: function(latitude, longitude){ | |
var gpsTestLabel = Titanium.UI.createLabel({ | |
text:'Quit callback - assign location', | |
font:{fontSize:11}, | |
color:'#444', | |
top:320, | |
left:10, | |
height:25, | |
width:300, | |
zIndex:10, | |
font:{fontSize: 20} | |
}); | |
GeoTest.win2.add(gpsTestLabel); | |
Ti.API.debug('Assign Location GPS event handler triggerred'); | |
Ti.Geolocation.removeEventListener('location', GeoTest.assignLatLong); | |
Ti.API.info('Removed Event Listener'); | |
GeoTest.latitude = latitude; | |
GeoTest.longitude = longitude; | |
GeoTest.latlong = latitude + ',' + longitude; | |
if (GeoTest.latlong) { | |
Ti.API.info(GeoTest.latlong); | |
} | |
} | |
} | |
GeoTest.init(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I moved the event handler out of the
getCurrentPosition
method, this seemed to be where it was failing. I do have android finding location data now.