Created
July 15, 2019 22:10
-
-
Save Ayyagaries/3e93db6e65394dab5d552adba625c5d3 to your computer and use it in GitHub Desktop.
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
/** | |
* Location Wrapper | |
* | |
* @class location | |
* @uses core | |
*/ | |
var App = require('core'), | |
lastKnownLocation, | |
showError = false, | |
callback = false, | |
callbackFired = false; | |
/** | |
* Update Device Location | |
* | |
* This method does not show error | |
* if it failed to get location | |
*/ | |
exports.refreshDeviceLocation = function() { | |
console.log("i am updating the device location"); | |
callback = false; | |
callbackFired = false; | |
obtainLocation({ | |
showError: false | |
}); | |
}; | |
/** | |
* Get Device Location | |
* | |
* Shows error if failed to get location | |
* | |
* @param {Object} params An object containing a showError flag and callback function | |
* @param {Function} parans.callback A function to execute with device's location | |
* @param {Boolean} params.showError Should an error be shown if couldn't find location, true by default | |
*/ | |
exports.getDeviceLocation = function(params) { | |
if (params['callback'] === undefined) { | |
params.callback = function() {}; | |
} | |
if (params['showError'] === undefined) { | |
params.showError = true; | |
} | |
callback = params.callback; | |
callbackFired = false; | |
return obtainLocation({ | |
showError: params.showError | |
}); | |
}; | |
/** | |
* Obtain Location | |
* | |
* The method tries the cached location first. | |
* If the cached location is more than 3 minutes old, | |
* we force a location update | |
* | |
* @params {Object} params An object with two possible values: | |
* @params {Function} params.callback Function to call when location is obtained | |
* @params {Boolean} params.showError Shows an error if showError is true | |
*/ | |
function obtainLocation(params) { | |
console.log(" I am obtaning the location " + JSON.stringfy(params)); | |
if (params.showError !== undefined) { | |
showError = params.showError; | |
} else { | |
showError = true; | |
} | |
// Try using cached location/low battery option first | |
Ti.Geolocation.getCurrentPosition(function(e) { | |
Ti.API.info('obtainLocation.getCurrentPosition: ' + JSON.stringify(e)); | |
console.log("Cordinated obtained success--> " + e.success); | |
if (e.success) { | |
console.log("Cordinated obtained success inside loop --> "); | |
// Check age of coords | |
var coords_time = ((new Date() - e.coords.timestamp) / 1000).toFixed(2); | |
logTime(coords_time); | |
// If cached location is more than 1 minute old | |
// we force a location update | |
if (coords_time > 60) { | |
Ti.API.warn('Coords Stale. Forcing location Update!'); | |
updateDeviceLocation(); | |
} else { | |
callbackFired = true; | |
if (callback) { | |
callback(e); | |
} | |
} | |
} else { | |
if (showError) { | |
console.log("----i am in show error and it is TRUE-----" + JSON.stringfy(showError)); | |
displayError(e.code); | |
} | |
} | |
}); | |
} | |
/** | |
* Get device location using more accurate methods | |
* | |
* For iOS: We setup best accuracy and add listener | |
* | |
* For Android: We setup two providers (GPS & Network) and | |
* each provider has it's own listener function. This is | |
* important because using the same listener method for | |
* both providers would overwrite the first one. | |
*/ | |
function updateDeviceLocation() { | |
console.log("i am updating the device location"); | |
if (OS_IOS) { | |
Ti.Geolocation.accuracy = Ti.Geolocation.ACCURACY_BEST; | |
Ti.Geolocation.distanceFilter = 10; | |
Ti.Geolocation.addEventListener('location', locationUpdate); | |
} | |
if (OS_ANDROID) { | |
console.log("i am updating the device location on Android"); | |
var gpsRule = Ti.Geolocation.Android.createLocationRule({ | |
provider: Ti.Geolocation.PROVIDER_GPS, | |
accuracy: 20, | |
//maxAge: 10000, | |
minAge: 10000, | |
}); | |
// Setup GPS | |
var providerGps = Ti.Geolocation.Android.createLocationProvider({ | |
name: Titanium.Geolocation.Android.PROVIDER_NETWORK, | |
minUpdateDistance: 0.0, | |
minUpdateTime: 0 | |
}); | |
Ti.Geolocation.Android.addLocationRule(gpsRule); | |
Ti.Geolocation.Android.addLocationProvider(providerGps); | |
Ti.Geolocation.Android.manualMode = true; | |
Titanium.Geolocation.addEventListener('location', gpsUpdate); | |
Titanium.Geolocation.addEventListener('location', networkUpdate); | |
}; | |
// Stop monitoring after 10 seconds | |
setTimeout(function() { | |
removeLocationListener(); | |
}, 50000); | |
} | |
/** | |
* Listens for location udpates | |
* | |
* We only remove the listeners if we have a location | |
* that was updated less than 10 seconds ago and is | |
* accurate up to 75 meters | |
* | |
* @param {Object} e | |
*/ | |
function locationUpdate(e) { | |
if (typeof e.coords !== 'undefined' && e.coords.accuracy < 75 && (new Date() - e.coords.timestamp < 10000)) { | |
Ti.API.info('locationUpdate.success: ' + JSON.stringify(e.coords)); | |
if (callback && !callbackFired) { | |
callback(e); | |
} | |
callbackFired = true; | |
removeLocationListener(); | |
} | |
} | |
/** | |
* Android specific function to handle | |
* location updates via GPS | |
* | |
* @param {Object} e | |
*/ | |
function gpsUpdate(e) { | |
Ti.API.info('gpsUpdate.e: ' + JSON.stringify(e)); | |
locationUpdate(e); | |
} | |
/** | |
* Android specific function to handle | |
* location updates via Network | |
* | |
* @param {Object} e | |
*/ | |
function networkUpdate(e) { | |
Ti.API.info('networkUpdate.e: ' + JSON.stringify(e)); | |
locationUpdate(e); | |
} | |
/** | |
* This method is used to remove any location | |
* listeners setup and may be fired multiple times | |
*/ | |
function removeLocationListener() { | |
console.log("Removing the event listener"); | |
if (OS_IOS) { | |
Ti.Geolocation.removeEventListener('location', locationUpdate); | |
} | |
if (OS_ANDROID) { | |
console.log("Removing the event listener on android"); | |
Ti.Geolocation.Android.removeEventListener('location', gpsUpdate); | |
Ti.Geolocation.Android.removeEventListener('location', networkUpdate); | |
} | |
if (!callbackFired && showError) { | |
callbackFired = true; | |
displayError(6); | |
} | |
} | |
/** | |
* Handle network errors | |
* | |
* @param {Object} code Error code returned by e.code | |
*/ | |
function displayError(code) { | |
var msg = 'Error trying to get location'; | |
switch (code) { | |
case Ti.Geolocation.ERROR_LOCATION_UNKNOWN: | |
msg = 'Location unknown'; | |
break; | |
case Ti.Geolocation.ERROR_DENIED: | |
msg = 'Access denied'; | |
break; | |
case Ti.Geolocation.ERROR_NETWORK: | |
msg = 'Network error'; | |
break; | |
case Ti.Geolocation.ERROR_HEADING_FAILURE: | |
msg = 'Failure to detect heading'; | |
break; | |
case Ti.Geolocation.ERROR_REGION_MONITORING_DENIED: | |
msg = 'Region monitoring access denied'; | |
break; | |
case Ti.Geolocation.ERROR_REGION_MONITORING_FAILURE: | |
msg = 'Region monitoring access failure'; | |
break; | |
case Ti.Geolocation.ERROR_REGION_MONITORING_DELAYED: | |
msg = 'Region monitoring setup delayed'; | |
break; | |
} | |
App.alert({ | |
title: 'Location Error', | |
message: msg | |
}); | |
} | |
function logTime(coords_time) { | |
var str; | |
if (coords_time >= 60) { | |
str = (coords_time / 60).toFixed(2) + 'mins'; | |
} else { | |
str = coords_time + 'seconds'; | |
} | |
Ti.API.info('Coords Updated: ' + str); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment