@ -1,31 +1,25 @@
import { Alert , PermissionsAndroid , Platform , ToastAndroid } from 'react-native' ;
import Geolocation from 'react-native-geolocation-service' ;
import { strings } from './i18n' ;
import moment from 'moment' ;
import { DATE _FORMAT } from './general' ;
export const hasLocationPermission = async ( ) => {
if ( Platform . OS === 'ios' ||
( Platform . OS === 'android' && Platform . Version < 23 ) ) {
if ( Platform . OS === 'ios' || ( Platform . OS === 'android' && Platform . Version < 23 ) ) {
return true ;
}
const hasPermission = await PermissionsAndroid . check (
PermissionsAndroid . PERMISSIONS . ACCESS _FINE _LOCATION
// PermissionsAndroid.PERMISSIONS.ACCESS_BACKGROUND_LOCATION
) ;
if ( hasPermission ) return true ;
const status = await PermissionsAndroid . request (
PermissionsAndroid . PERMISSIONS . ACCESS _FINE _LOCATION ,
// PermissionsAndroid.PERMISSIONS.ACCESS_BACKGROUND_LOCATION
{
title : "NawakaraConnect Location Permission" ,
message : "NawakaraConnect wants to enable location permission. Let NawakaraConnect collects the device's location to get current position of the device." ,
// buttonNeutral: 'Ask Me Later',
title : "NeOps Location Permission" ,
message : "NeOps wants to enable location permission. Let NeOps collect the device's location to get the current position of the device." ,
buttonNegative : 'Deny' ,
buttonPositive : 'Accept' ,
}
@ -43,12 +37,15 @@ export const hasLocationPermission = async () => {
}
export const getCoords = async ( callback ) => {
if ( typeof callback !== 'function' ) {
throw new TypeError ( 'Callback must be a function' ) ;
}
const hasLocation = await hasLocationPermission ( ) ;
if ( ! hasLocation ) {
callback ( null )
return
callback ( { lat : null , lon : null } ) ;
return ;
}
Geolocation . getCurrentPosition (
@ -57,21 +54,19 @@ export const getCoords = async (callback) => {
Alert . alert (
strings ( 'takePicture.fakeGPSDetected' ) ,
strings ( 'takePicture.fakeGPSFailedMessage' ) ,
[
{ text : "OK" }
]
[ { text : "OK" } ]
) ;
return false ;
callback ( { lat : null , lon : null } ) ;
return ;
}
let lat = position . coords . latitude ;
let lon = position . coords . longitude ;
let timestamp = moment ( position . timestamp ) . format ( DATE _FORMAT ) ;
callback ( { lat , lon , timestamp } )
let timestamp = moment ( position . timestamp ) . format ( 'D MMMM YYYY HH:mm' ) ;
callback ( { lat , lon , timestamp } ) ;
} ,
( error ) => {
// See error code charts below.
console . log ( '[Home] error Geolocation.getCurrentPosition' , error . code , error . message ) ;
// callback(null)
callback ( { lat : null , lon : null } ) ;
} ,
{ enableHighAccuracy : true , timeout : 15000 , maximumAge : 10000 }
) ;