Skip to content

Instantly share code, notes, and snippets.

@pebosi
Last active August 29, 2015 14:24
Show Gist options
  • Save pebosi/589d36348884074fa241 to your computer and use it in GitHub Desktop.
Save pebosi/589d36348884074fa241 to your computer and use it in GitHub Desktop.
/*
Place this file in your /lib directory.
Require geo in calling module like var geo = require("geo"); Then call geo.init() at the start of your app.
To get current location, place a fireEvent at the start of your app and anywhere else location needs updating like this:
Ti.App.addEventListener('resume', function() {
Ti.App.fireEvent('getCurrentPosition');
});
@author Michael Stelly, et al.
@date 31 Dec 2013
*/
function init() {
Ti.Geolocation.purpose = 'Koch Pipeline Public Awareness App';
checkGeoPermission();
if (OS_ANDROID) {
Titanium.Geolocation.Android.LocationProvider = Ti.Geolocation.PROVIDER_GPS;
}
/*
* Set accuracy - The following values are supported
*
* Ti.Geolocation.ACCURACY_BEST
* Ti.Geolocation.ACCURACY_NEAREST_TEN_METERS
* Ti.Geolocation.ACCURACY_HUNDRED_METERS
* Ti.Geolocation.ACCURACY_KILOMETER
* Ti.Geolocation.ACCURACY_THREE_KILOMETERS
*/
Ti.Geolocation.accuracy = Ti.Geolocation.ACCURACY_BEST;
/*
* Set distance filter. This dictates how often an event fires
* based on the distance the device moves (in meters).
*/
Ti.Geolocation.distanceFilter = 10;
Ti.App.fireEvent('getCurrentPosition');
return;
};
/* ===================
Check permission settings
=====================*/
function checkGeoPermission() {
if (Ti.Geolocation.locationServicesEnabled === false) {
helper.showDialog({
title : 'Mapping Error',
message : 'Your device has geo turned off - turn it on.'
});
} else {
if (OS_IOS) {
var authorization = Ti.Geolocation.getLocationServicesAuthorization();
}
if (authorization == Ti.Geolocation.AUTHORIZATION_DENIED) {
helper.showDialog({
title : 'Mapping Error',
message : 'You have disallowed App from running geolocation services.'
});
} else if (authorization == Ti.Geolocation.AUTHORIZATION_RESTRICTED) {
helper.showDialog({
title : 'Mapping Error',
message : 'Your system has disallowed App from running geolocation services.'
});
}
}
return;
};
/* ===================
Get current position
=====================*/
myGeolocation = {};
myGeolocation.lon = 0;
myGeolocation.lat = 0;
function updateGeolocation(e) {
myGeolocation.lat = e.coords.latitude;
myGeolocation.lon = e.coords.longitude;
tracker.save({
latitude: myGeolocation.lat,
longitude: myGeolocation.lon
});
tracker.fetch();
}
Ti.App.addEventListener('getCurrentPosition', function() {
Ti.Geolocation.getCurrentPosition(updateGeolocation);
});
//Public API
exports.init = init;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment