Skip to content

Instantly share code, notes, and snippets.

@joshualambert
Created October 10, 2012 02:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joshualambert/3862747 to your computer and use it in GitHub Desktop.
Save joshualambert/3862747 to your computer and use it in GitHub Desktop.
var isAndroid = false;
if(Titanium.Platform.name == 'android') {
isAndroid = true;
}
Ti.Geolocation.preferredProvider = Ti.Geolocation.PROVIDER_GPS;
Ti.Geolocation.accuracy = Ti.Geolocation.ACCURACY_BEST;
Ti.Geolocation.distanceFilter = 0;
Ti.Geolocation.purpose = "Save your last known position.";
if (isAndroid) {
Ti.Geolocation.frequency=10000;
}
var mainWindow = Ti.UI.createWindow({
width:'100%',
height:'100%',
layout:'vertical',
backgroundColor:'#401325'
});
mainWindow.open();
var mainView = Ti.UI.createView({
width:'100%',
height:'100%',
layout:'vertical'
});
mainWindow.add(mainView);
var mainMapView = Ti.Map.createView({
width:'90%',
top:'5%',
left:'5%',
height:'70%',
borderRadius:10,
animate:true,
userLocation:true
});
mainView.add(mainMapView);
var loadTrail = Ti.UI.createButton({
title:'Load Trail.',
width:'90%',
height:'15%',
top:'5%',
left:'5%'
});
mainView.add(loadTrail);
loadTrail.addEventListener('click', function(e) {
var location = JSON.parse(Titanium.App.Properties.getString('last_known_location'));
var mapRegion = {latitude:location.latitude,longitude:location.longitude,latitudeDelta:0.002,longitudeDelta:0.002};
mainMapView.region = mapRegion;
});
var routeCount = 0;
var oldLat = 0;
var oldLong = 0;
// Location update daemon.
function doLocationUpdate() {
Ti.API.info('doLocationUpdate(); - Ran.');
Ti.Geolocation.getCurrentPosition(function(loc) {
if (loc.coords) {
Titanium.App.Properties.setString('last_known_location', JSON.stringify(loc.coords));
} else {
alert('Failed to get location data.');
}
});
}
if (!isAndroid) {
setInterval(doLocationUpdate, 15000);
} else {
Ti.Geolocation.addEventListener('location', function(e) {
doLocationUpdate();
});
/*
Ti.Geolocation.addEventListener('heading', function(e) {
// Do nothing.
Ti.API.info('Hit the HEADING placeholder geolocation event.');
});
*/
}
doLocationUpdate();
var location = JSON.parse(Titanium.App.Properties.getString('last_known_location'));
var mapRegion = {latitude:location.latitude,longitude:location.longitude,latitudeDelta:0.002,longitudeDelta:0.002};
mainMapView.region = mapRegion;
var oldLat = location.latitude;
var oldLong = location.longitude;
var locObj = [];
function doMapUpdate() {
Ti.API.info('doMapUpdate(); - Ran.');
if (Ti.App.Properties.getString('last_known_location') != '') { // Make sure LAST KNOWN LOCATION variable has been set.
var location = JSON.parse(Titanium.App.Properties.getString('last_known_location'));
if (location.latitude != oldLat) { // Only update if new position data is available.
Ti.API.info('doMapUpdate(); - Updated Location.');
locObj[routeCount] = {
name:'maroute' + routeCount,
color:'yellow',
width:8,
points:[{longitude:oldLong,latitude:oldLat},{longitude:location.longitude,latitude:location.latitude}]
};
mainMapView.addRoute(locObj[routeCount]);
if (routeCount >= 10) {
mainMapView.removeRoute(locObj[(routeCount - 10)]);
}
var mapRegion = {latitude:location.latitude,longitude:location.longitude,latitudeDelta:0.002,longitudeDelta:0.002};
mainMapView.region = mapRegion;
oldLat = location.latitude;
oldLong = location.longitude;
routeCount = routeCount + 1;
} else {
Ti.API.info('doMapUpdate(); - Did Not Update Locaiton.');
}
}
// setTimeout(doMapUpdate, 10000);
}
doMapUpdate();
setInterval(doMapUpdate, 10000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment