Skip to content

Instantly share code, notes, and snippets.

@brentonhouse
Forked from samueleastdev/base.js
Created May 6, 2020 15:22
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 brentonhouse/ea3c603bf6e2c2c3717a35c427e2e2d5 to your computer and use it in GitHub Desktop.
Save brentonhouse/ea3c603bf6e2c2c3717a35c427e2e2d5 to your computer and use it in GitHub Desktop.
Appcelerator requesting location permissions
/*
* function to check for all location services outcomes
*/
function requestLocationPermissions(authorizationType, callback) {
// FIXME: Always returns false on Android 6
// https://jira.appcelerator.org/browse/TIMOB-23135
if (OS_IOS && !Ti.Geolocation.locationServicesEnabled) {
return callback({
success : false,
error : 'Location Services Disabled'
});
}
// Permissions already granted
if (Ti.Geolocation.hasLocationPermissions(authorizationType)) {
return callback({
success : true
});
}
// On iOS we can determine why we do not have permission
if (OS_IOS) {
if (Ti.Geolocation.locationServicesAuthorization === Ti.Geolocation.AUTHORIZATION_RESTRICTED) {
return callback({
success : false,
error : 'Your device policy does not allow Geolocation'
});
} else if (Ti.Geolocation.locationServicesAuthorization === Ti.Geolocation.AUTHORIZATION_DENIED) {
dialogs.confirm({
title : 'You denied permission before',
message : 'Tap Yes to open the Settings app to restore permissions, then try again.',
callback : function() {
Ti.Platform.openURL(Ti.App.iOS.applicationOpenSettingsURL);
}
});
// return success:false without an error since we've informed the user already
return callback({
success : false
});
}
}
// Request permission
Ti.Geolocation.requestLocationPermissions(authorizationType, function(e) {
if (!e.success) {
return callback({
success : false,
error : e.error || 'Failed to request Location Permissions'
});
}
callback({
success : true
});
});
}
/*
* add a event listener for android
*/
function mapCompletelyLoadedEvent(e) {
// remove the event
$.shakeMapView.removeEventListener('complete', mapCompletelyLoadedEvent);
//Check if the user has location service enabled
requestLocationPermissions(Ti.Geolocation.AUTHORIZATION_WHEN_IN_USE, function(e) {
if (e.success) {
// Set the location monitoring
Titanium.Geolocation.getCurrentPosition(getGeoPosition);
} else {
var dialog = Ti.UI.createAlertDialog({
message : 'You do not have location permissions enabled shake locate needs these to work.',
ok : 'Got it',
title : 'Important'
});
dialog.addEventListener('click', function(e) {
if (OS_IOS) {
Ti.Platform.openURL('app-settings:');
}
if (OS_ANDROID) {
var intent = Ti.Android.createIntent({
action : 'android.settings.APPLICATION_SETTINGS',
});
intent.addFlags(Ti.Android.FLAG_ACTIVITY_NEW_TASK);
Ti.Android.currentActivity.startActivity(intent);
}
});
dialog.show();
}
});
}
// Fire the map complete event
$.shakeMapView.addEventListener('complete', mapCompletelyLoadedEvent);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment