Skip to content

Instantly share code, notes, and snippets.

@samueleastdev
Created September 23, 2016 09:49
Show Gist options
  • Save samueleastdev/95de3528e2c81ca7691c0d5792f5d91e to your computer and use it in GitHub Desktop.
Save samueleastdev/95de3528e2c81ca7691c0d5792f5d91e 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);
@gurudey
Copy link

gurudey commented Jan 21, 2018

To fix Ti.Geolocation.locationServicesEnabled Always returns false on Android 6
add this to your manifest:
<uses-feature android:name="android.hardware.location.gps" />

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment