import { Big } from "big.js" ; import Geolocation , { GeolocationError , GeolocationOptions , GeolocationResponse } from "@react-native-community/geolocation" ; param { Big } timeout param { Big } maximumAge param { boolean } highAccuracy param { Big } minimumAccuracy returns { Promise . < MxObject > } export async function GetCurrentLocationMinimumAccuracy ( timeout ? , maximumAge ? , highAccuracy ? , minimumAccuracy ? ) { if ( navigator && navigator . product === "ReactNative" && ! navigator . geolocation ) { ( navigator . geolocation as any ) = Geolocation ; } return new Promise ( ( resolve , reject ) => { const options = getOptions ( ) ; if ( navigator && ( ! navigator . product || navigator . product !== "ReactNative" ) ) { navigator . geolocation . getCurrentPosition ( ( ) => { } , ( ) => { } , { } ) ; } const watchId = navigator . geolocation . watchPosition ( onSuccess , onError , options ) ; const timeStart = Date . now ( ) ; let lastAccruedPosition ; function createGeolocationObject ( position ) { mx . data . create ( { entity : "NanoflowCommons.Geolocation" , callback : mxObject => resolve ( mapPositionToMxObject ( mxObject , position ) ) , error : ( ) => reject ( new Error ( "Could not create 'NanoflowCommons.Geolocation' object to store location" ) ) } ) ; } function onSuccess ( position ) { if ( watchId && ( ! minimumAccuracy || minimumAccuracy >= position . coords . accuracy ) ) { navigator . geolocation . clearWatch ( watchId ) ; createGeolocationObject ( position ) ; } else { if ( ! lastAccruedPosition || position . coords . accuracy < lastAccruedPosition . coords . accuracy ) { lastAccruedPosition = position ; } const timeDiff = Date . now ( ) - timeStart ; if ( ! timeout || timeout . lte ( timeDiff ) ) { navigator . geolocation . clearWatch ( watchId ) ; createGeolocationObject ( lastAccruedPosition ) ; } } } function onError ( error ) { return reject ( new Error ( error . message ) ) ; } function getOptions ( ) { const timeoutNumber = timeout && Number ( timeout . toString ( ) ) ; const maximumAgeNumber = maximumAge && Number ( maximumAge . toString ( ) ) ; return { timeout : timeoutNumber , maximumAge : maximumAgeNumber , enableHighAccuracy : highAccuracy } ; } function mapPositionToMxObject ( mxObject , position ) { mxObject . set ( "Timestamp" , new Date ( position . timestamp ) ) ; mxObject . set ( "Latitude" , new Big ( position . coords . latitude . toFixed ( 8 ) ) ) ; mxObject . set ( "Longitude" , new Big ( position . coords . longitude . toFixed ( 8 ) ) ) ; mxObject . set ( "Accuracy" , new Big ( position . coords . accuracy . toFixed ( 8 ) ) ) ; if ( position . coords . altitude != null ) { mxObject . set ( "Altitude" , new Big ( position . coords . altitude . toFixed ( 8 ) ) ) ; } if ( position . coords . altitudeAccuracy != null && position . coords . altitudeAccuracy !== - 1 ) { mxObject . set ( "AltitudeAccuracy" , new Big ( position . coords . altitudeAccuracy . toFixed ( 8 ) ) ) ; } if ( position . coords . heading != null && position . coords . heading !== - 1 ) { mxObject . set ( "Heading" , new Big ( position . coords . heading . toFixed ( 8 ) ) ) ; } if ( position . coords . speed != null && position . coords . speed !== - 1 ) { mxObject . set ( "Speed" , new Big ( position . coords . speed . toFixed ( 8 ) ) ) ; } return mxObject ; } } ) ; }