functions.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. import { db } from "./database/db";
  2. import * as FileSystem from 'expo-file-system';
  3. export async function downloadTile(url, z, x, y) {
  4. try {
  5. const fileUri = FileSystem.cacheDirectory + `tiles/${z}/${x}/${y}.png`;
  6. if (!FileSystem.getInfoAsync(FileSystem.cacheDirectory + `tiles/${z}/${x}`).exists) {
  7. await FileSystem.makeDirectoryAsync(FileSystem.cacheDirectory + `tiles/${z}/${x}`, { intermediates: true });
  8. }
  9. if (FileSystem.getInfoAsync(FileSystem.cacheDirectory + `tiles/${z}/${x}/${y}.png`).exists) {
  10. console.log('File already exists');
  11. } else {
  12. const downloadResult = await FileSystem.downloadAsync(url, fileUri);
  13. if (downloadResult.status !== 200) {
  14. throw new Error('Failed to download the tile');
  15. }
  16. }
  17. } catch (error) {
  18. console.error("Error downloading tile:", error);
  19. }
  20. }
  21. export async function loadGeoJSONData(region) {
  22. const selectQuery = `SELECT data FROM regions WHERE region_id = ${region};`;
  23. return new Promise((resolve, reject) => {
  24. db.transaction(tx => {
  25. tx.executeSql(
  26. selectQuery,
  27. [],
  28. (_, { rows }) => {
  29. if (rows.length > 0) {
  30. resolve(JSON.parse(rows._array[0].data));
  31. } else {
  32. resolve(null);
  33. }
  34. },
  35. (_, error) => {
  36. reject(error);
  37. }
  38. );
  39. });
  40. });
  41. }
  42. export async function loadMarkersData() {
  43. const selectQuery = `SELECT marker_id, l, g, i, n, d FROM markers;`;
  44. return new Promise((resolve, reject) => {
  45. db.transaction(tx => {
  46. tx.executeSql(
  47. selectQuery,
  48. [],
  49. (_, { rows }) => {
  50. if (rows.length > 0) {
  51. resolve(rows._array.slice(0, 20));
  52. } else {
  53. resolve(null);
  54. }
  55. },
  56. (_, error) => {
  57. reject(error);
  58. }
  59. );
  60. });
  61. });
  62. }