Permalink
Browse files

Automatically request location permission when accessing geolocation

Reviewed By: achen1

Differential Revision: D5180727

fbshipit-source-id: 0c67ba31e3271f4ea5167a50119c132ef238cc25
  • Loading branch information...
Akshat Asawa authored and facebook-github-bot committed Jun 6, 2017
1 parent dd8ed62 commit 9e026ec358aa6961aa3225290f76da1c7e17e886
Showing with 25 additions and 6 deletions.
  1. +25 −6 Libraries/Geolocation/Geolocation.js
@@ -20,6 +20,9 @@ const warning = require('fbjs/lib/warning');
const LocationEventEmitter = new NativeEventEmitter(RCTLocationObserver);
const Platform = require('Platform');
const PermissionsAndroid = require('PermissionsAndroid');
var subscriptions = [];
var updatesEnabled = false;
@@ -87,7 +90,7 @@ var Geolocation = {
* On Android, if the location is cached this can return almost immediately,
* or it will request an update which might take a while.
*/
getCurrentPosition: function(
getCurrentPosition: async function(
geo_success: Function,
geo_error?: Function,
geo_options?: GeoOptions
@@ -96,11 +99,27 @@ var Geolocation = {
typeof geo_success === 'function',
'Must provide a valid geo_success callback.'
);
RCTLocationObserver.getCurrentPosition(
geo_options || {},
geo_success,
geo_error || logError
);
let hasPermission = true;
// Supports Android's new permission model. For Android older devices,
// it's always on.
if (Platform.OS === 'android' && Platform.Version >= 23) {
hasPermission = await PermissionsAndroid.check(
PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
);
if (!hasPermission) {
const status = await PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
);
hasPermission = status === PermissionsAndroid.RESULTS.GRANTED;
}
}
if (hasPermission) {
RCTLocationObserver.getCurrentPosition(
geo_options || {},
geo_success,
geo_error || logError,
);
}
},
/*

0 comments on commit 9e026ec

Please sign in to comment.