Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
async function requestLocationPermission() {
if (Platform.OS === 'ios') {
Geolocation.setRNConfiguration({
authorizationLevel: 'whenInUse'
})
Geolocation.requestAuthorization()
// IOS permission request does not offer a callback :/
return null
} else if (Platform.OS === 'android') {
try {
const granted = await PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION
)
if (granted === PermissionsAndroid.RESULTS.GRANTED) {
return true
} else {
return false
}
} catch (err) {
console.warn(err.message)
return false
}
async function getCurrentPosition(callback) {
const hasLocationPermission = await requestLocationPermission()
/* This will only be fired on Android. On Apple we can not detect when/if a
* location permission has been granted or denied. For that reason after a
* predefined period we just timeout.
*/
if (hasLocationPermission === false) {
callback({
locationAvailable: false,
error: 'Can not obtain location permission'
})
return
}
Geolocation.getCurrentPosition(
position => {
callback({
locationAvailable: true,
position
})
},
error => {
callback({
locationAvailable: false,
error: error.message,
errorCode: error.code
})
},
{ enableHighAccuracy: true, timeout: 20000, maximumAge: 10000 }
)
}
async function requestLocationPermission() {
if (Platform.OS === 'ios') {
Geolocation.setRNConfiguration({
authorizationLevel: 'whenInUse'
})
Geolocation.requestAuthorization()
// IOS permission request does not offer a callback :/
return null
} else if (Platform.OS === 'android') {
try {
const granted = await PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION
)
if (granted === PermissionsAndroid.RESULTS.GRANTED) {
return true
} else {
return false
}
getNearbyBars = () => {
Geolocation.getCurrentPosition(
(position) => {
this.setState(
{
latitude: position.coords.latitude,
longitude: position.coords.longitude,
},
() => {
this.searchBars();
},
);
},
error => console.log(error),
{ enableHighAccuracy: true, timeout: 15000, maximumAge: 10000 },
);
};
this.setState({ loading: true }, () => {
Geolocation.getCurrentPosition(
(position) => {
this.setState({ location: position, loading: false });
console.log(position);
},
(error) => {
this.setState({ location: error, loading: false });
console.log(error);
},
{ enableHighAccuracy: true, timeout: 15000, maximumAge: 10000, distanceFilter: 50, forceRequestLocation: true }
);
});
}
getNearbyBars = () => {
if (this.mounted) {
Geolocation.getCurrentPosition(
(position) => {
this.setState(
{
latitude: position.coords.latitude,
longitude: position.coords.longitude,
},
() => {
this.searchBars();
},
);
},
error => console.log(error),
{
enableHighAccuracy: true,
timeout: 30000,
maximumAge: 10000,
getNearbyBars = () => {
if (this.mounted) {
Geolocation.getCurrentPosition(
(position) => {
this.setState(
{
latitude: position.coords.latitude,
longitude: position.coords.longitude,
},
() => {
this.searchBars();
},
);
},
error => console.log(error),
{ enableHighAccuracy: false, timeout: 25000, distanceFilter: 0 },
);
}
};
this.setState({ updatesEnabled: true }, () => {
this.watchId = Geolocation.watchPosition(
(position) => {
this.setState({ location: position });
console.log(position);
},
(error) => {
this.setState({ location: error });
console.log(error);
},
{ enableHighAccuracy: true, distanceFilter: 0, interval: 5000, fastestInterval: 2000 }
);
});
}
async _findClosestBuilding(): Promise {
this.setState({ locating: true });
const locationPermissionGranted = await Permissions.requestLocationPermission(Platform.OS, PermissionsAndroid);
if (locationPermissionGranted) {
Geolocation.getCurrentPosition((position: any) => {
const location = { latitude: position.coords.latitude, longitude: position.coords.longitude };
this.setState({
closestBuilding: Navigation.findClosestBuilding(location, this.props.buildingList, MAXIMUM_DISTANCE),
locating: false,
locationError: undefined,
});
},
(err: any) => this._showLocationErrorMessage(err),
{ enableHighAccuracy: true, timeout: 20000, maximumAge: 1000 }
);
}
}
removeLocationUpdates = () => {
if (this.watchId !== null) {
Geolocation.clearWatch(this.watchId);
this.setState({ updatesEnabled: false })
}
}