sid-address-verification-react-native
Version:
A sourceID react native package for address verification
92 lines (86 loc) • 2.73 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.LocationService = void 0;
var _geolocation = _interopRequireDefault(require("@react-native-community/geolocation"));
var _ApiService = require("./ApiService");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
class LocationService {
intervalId = null;
timeoutId = null;
isTracking = false;
constructor() {}
static getInstance() {
if (!LocationService.instance) {
LocationService.instance = new LocationService();
}
return LocationService.instance;
}
startLocationTracking(intervalSeconds, durationSeconds, onLocationUpdate) {
if (this.isTracking) {
console.log('Location tracking is already active');
return;
}
this.isTracking = true;
this.onLocationUpdate = onLocationUpdate;
console.log(`Starting location tracking: interval=${intervalSeconds}s, duration=${durationSeconds}s`);
// Start periodic location updates
this.intervalId = setInterval(() => {
this.getCurrentLocation();
}, intervalSeconds * 1000);
// Get initial location
this.getCurrentLocation();
// Stop tracking after duration
this.timeoutId = setTimeout(() => {
this.stopLocationTracking();
}, durationSeconds * 1000);
}
stopLocationTracking() {
if (this.intervalId) {
clearInterval(this.intervalId);
this.intervalId = null;
}
if (this.timeoutId) {
clearTimeout(this.timeoutId);
this.timeoutId = null;
}
this.isTracking = false;
this.onLocationUpdate = undefined;
console.log('Location tracking stopped');
}
getCurrentLocation() {
_geolocation.default.getCurrentPosition(position => {
const location = {
latitude: position.coords.latitude,
longitude: position.coords.longitude,
timestamp: Date.now()
};
console.log(`Location update: ${location.latitude}, ${location.longitude}`);
// Call the callback if provided
if (this.onLocationUpdate) {
this.onLocationUpdate(location);
}
// Submit to API
this.submitLocationToAPI(location);
}, error => {
console.error('Error getting location:', error);
}, {
enableHighAccuracy: true,
timeout: 15000,
maximumAge: 10000
});
}
async submitLocationToAPI(location) {
try {
await _ApiService.ApiService.getInstance().submitLocationForVerification(location);
} catch (error) {
console.error('Failed to submit location to API:', error);
}
}
isCurrentlyTracking() {
return this.isTracking;
}
}
exports.LocationService = LocationService;
//# sourceMappingURL=LocationService.js.map