Skip to content

Instantly share code, notes, and snippets.

@jpadilla
Created March 6, 2012 23:04
Show Gist options
  • Select an option

  • Save jpadilla/1989634 to your computer and use it in GitHub Desktop.

Select an option

Save jpadilla/1989634 to your computer and use it in GitHub Desktop.
function Geo() {}
Geo.prototype.getCurrentPosition = function(_callback) {
var longitude = 0;
var latitude = 0;
var altitude = 0;
var heading = 0;
var accuracy = 0;
var speed = 0;
var timestamp = 0;
var altitudeAccuracy = 0;
Ti.Geolocation.preferredProvider = "gps";
Ti.Geolocation.purpose = "GPS";
Titanium.Geolocation.accuracy = Titanium.Geolocation.ACCURACY_BEST;
Titanium.Geolocation.distanceFilter = 10;
if( typeof (Number.prototype.toRad) === "undefined") {
Number.prototype.toRad = function() {
return this * Math.PI / 180;
}
}
Titanium.Geolocation.addEventListener('location', function(e) {
if(e.error) {
// manage the error
return;
}
longitude = e.coords.longitude;
latitude = e.coords.latitude;
altitude = e.coords.altitude;
heading = e.coords.heading;
accuracy = e.coords.accuracy;
speed = e.coords.speed;
timestamp = e.coords.timestamp;
altitudeAccuracy = e.coords.altitudeAccuracy;
});
var outData = {};
if(Titanium.Geolocation.locationServicesEnabled === false) {
Titanium.UI.createAlertDialog({
title : 'Barrandiando',
message : 'Your device has geo turned off - turn it on.'
}).show();
} else {
if(Titanium.Platform.name != 'android') {
var authorization = Titanium.Geolocation.locationServicesAuthorization;
Ti.API.info('Authorization: ' + authorization);
if(authorization == Titanium.Geolocation.AUTHORIZATION_DENIED) {
Ti.UI.createAlertDialog({
title : 'Barrandiando',
message : 'You have disallowed Barrandiando from running geolocation services.'
}).show();
} else if(authorization == Titanium.Geolocation.AUTHORIZATION_RESTRICTED) {
Ti.UI.createAlertDialog({
title : 'Barrandiando',
message : 'Your system has disallowed Barrandiando from running geolocation services.'
}).show();
}
}
//
// GET CURRENT POSITION - THIS FIRES ONCE
//
Titanium.Geolocation.getCurrentPosition(function(e) {
if(!e.success || e.error) {
alert('error ' + JSON.stringify(e.error));
return;
}
longitude = e.coords.longitude;
latitude = e.coords.latitude;
altitude = e.coords.altitude;
heading = e.coords.heading;
accuracy = e.coords.accuracy;
speed = e.coords.speed;
timestamp = e.coords.timestamp;
altitudeAccuracy = e.coords.altitudeAccuracy;
outData = {
latitude : latitude,
longitude : longitude,
altitude : altitude,
heading : heading,
accuracy : accuracy,
speed : speed,
timestamp : timestamp,
altitudeAccuracy : altitudeAccuracy
}
if(_callback) {
_callback(outData);
}
});
}
};
module.exports = Geo;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment