Created
April 18, 2012 13:44
-
-
Save egomez99/2413645 to your computer and use it in GitHub Desktop.
Geolocation commonJS Module
This file contains hidden or 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 win = Titanium.UI.createWindow({ | |
title: 'Main', | |
exitOnClose: true, | |
fullscreen: false, | |
navBarHidden: false, | |
backgroundColor: 'gray' | |
}); | |
win.addEventListener('open', function() { | |
var GeoModule = require('geolocation'); | |
var activity = win.activity; | |
GeoModule.run(activity); | |
}); | |
win.open(); |
This file contains hidden or 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
// | |
// SET ACCURACY - THE FOLLOWING VALUES ARE SUPPORTED | |
// | |
// Titanium.Geolocation.ACCURACY_BEST | |
// Titanium.Geolocation.ACCURACY_NEAREST_TEN_METERS | |
// Titanium.Geolocation.ACCURACY_HUNDRED_METERS | |
// Titanium.Geolocation.ACCURACY_KILOMETER | |
// Titanium.Geolocation.ACCURACY_THREE_KILOMETERS | |
// | |
Titanium.Geolocation.accuracy = Titanium.Geolocation.ACCURACY_BEST; | |
//Titanium.Geolocation.accuracy = Titanium.Geolocation.ACCURACY_THREE_KILOMETERS | |
// (lowest accuracy and power consumption)... | |
// | |
// SET DISTANCE FILTER. THIS DICTATES HOW OFTEN AN EVENT FIRES BASED ON THE DISTANCE THE DEVICE MOVES | |
// THIS VALUE IS IN METERS | |
// | |
Titanium.Geolocation.distanceFilter = 1; | |
Ti.Geolocation.preferredProvider = Ti.Geolocation.PROVIDER_NETWORK; | |
var locationAdded = false; | |
// | |
// EVENT LISTENER FOR GEO EVENTS - THIS WILL FIRE REPEATEDLY (BASED ON DISTANCE FILTER) | |
// | |
var locationCallback2 = function(e) | |
{ | |
if (!e.success || e.error) | |
{ | |
var updatedLocation = 'Unexpected Error While Updating Location: ' + JSON.stringify(e.error); | |
var updatedLatitude = ''; | |
var updatedLocationAccuracy = ''; | |
var updatedLocationTime = ''; | |
Ti.API.info("Code translation: "+translateErrorCode(e.code)); | |
return; | |
} | |
var longitude = e.coords.longitude; | |
var latitude = e.coords.latitude; | |
var altitude = e.coords.altitude; | |
var heading = e.coords.heading; | |
var accuracy = e.coords.accuracy; | |
var speed = e.coords.speed; | |
var timestamp = e.coords.timestamp; | |
var altitudeAccuracy = e.coords.altitudeAccuracy; | |
//Titanium.Geolocation.distanceFilter = 100; //changed after first location event | |
updatedLocation = 'long: ' + longitude; | |
updatedLatitude.text = 'lat: '+ latitude; | |
updatedLocationAccuracy = 'accuracy: ' + accuracy; | |
updatedLocationTime = 'timestamp: ' +new Date(timestamp); | |
setTimeout(function() | |
{ | |
updatedLatitude = ' - '; | |
updatedLocation = ' - '; | |
updatedLocationAccuracy = ' - '; | |
updatedLocationTime = ' - '; | |
},1000); | |
var reverseGeo; | |
// reverse geo | |
Titanium.Geolocation.reverseGeocoder(latitude,longitude,function(evt) | |
{ | |
if (evt.success) { | |
var places = evt.places; | |
if (places && places.length) { | |
reverseGeo = places[0].address; | |
} else { | |
reverseGeo.text = "No address found"; | |
} | |
Ti.API.debug(" Reverse geolocation result: "+JSON.stringify(evt)); | |
} | |
else { | |
Ti.UI.createAlertDialog({ | |
title:'Reverse geo error', | |
message:evt.error | |
}).show(); | |
Ti.API.info("Code translation: "+translateErrorCode(e.code)); | |
} | |
}); | |
Titanium.API.info(' Geo - location updated: ' + new Date(timestamp) + ' long ' + longitude + ' lat ' + latitude + ' accuracy ' + accuracy); | |
}; | |
//geolocation2 | |
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"; | |
} | |
} | |
var locationCallback = function(e) { | |
var msg = null; | |
if(Ti.Geolocation.locationServicesEnabled) { | |
Titanium.Geolocation.getCurrentPosition(function(e) { | |
var longitude = e.coords.longitude; | |
var latitude = e.coords.latitude; | |
msg = ' NO ERRORS on LOCATION my Coords are: ' + longitude + ' - ' + latitude; | |
Ti.API.info(msg); | |
}); | |
} else {//if enabled | |
if(!e.success || e.error) { | |
msg = 'error:' + JSON.stringify(e.error); | |
msg += "Code translation: " + translateErrorCode(e.error.code); | |
Ti.API.info(msg); | |
alert('Please turn on Location Services to enable this Push Notification'); | |
return; | |
} | |
} | |
}; | |
function OnAppStart() | |
{ | |
Ti.API.info("start event received"); | |
Ti.API.info("adding location callback on Start"); | |
Titanium.Geolocation.addEventListener('location', locationCallback); | |
} | |
function OnAppResume() | |
{ | |
Ti.API.info("resume event received"); | |
if(!locationAdded) { | |
Ti.API.info("adding location callback on resume"); | |
Titanium.Geolocation.addEventListener('location', locationCallback); | |
locationAdded = true; | |
} | |
} | |
function OnAppPause() | |
{ | |
Ti.API.info("pause event received"); | |
if(locationAdded) { | |
Ti.API.info("removing location callback on pause"); | |
Titanium.Geolocation.removeEventListener('location', locationCallback); | |
locationAdded = false; | |
} | |
} | |
function OnAppDestroy() | |
{ | |
Ti.API.info("destroy event received"); | |
if(locationAdded) { | |
Ti.API.info("removing location callback on destroy"); | |
Titanium.Geolocation.removeEventListener('location', locationCallback); | |
locationAdded = false; | |
} | |
} | |
exports.run = function( myActivity ) { | |
Ti.API.info(' Current Activity: ' + myActivity); | |
locationAdded = true; | |
// as the destroy handler will remove the listener, | |
// only set the pause handler to remove if you need battery savings | |
myActivity.addEventListener('start', OnAppStart); | |
myActivity.addEventListener('pause', OnAppPause); | |
myActivity.addEventListener('destroy',OnAppDestroy); | |
myActivity.addEventListener('resume', OnAppResume); | |
}; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment