api.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. import { db } from "../database/db";
  2. import axios from "axios";
  3. const headers = {
  4. 'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8',
  5. 'Nmtoken': '6B905B9C-32E1-4653-8038-FDB54A18DE3F'
  6. };
  7. export async function fetchRegion(region) {
  8. const apiUrl = `https://nomadmania.com/static/geojson/${region}.geojson`
  9. try {
  10. const response = await axios.get(apiUrl, {region}, {headers});
  11. if (response.data) {
  12. await db.transaction(tx => {
  13. tx.executeSql(
  14. 'INSERT INTO regions (region_id, data) VALUES (?, ?);',
  15. [region, JSON.stringify(response.data)],
  16. (res) => {},
  17. (error) => { console.error("Error saving GeoJSON data: ", error); }
  18. );
  19. });
  20. }
  21. } catch (error) {
  22. console.error('Error fetching data from API:', error);
  23. }
  24. }
  25. export async function getForRegion(region) {
  26. const apiUrl = 'https://nomadmania.com/webapi/series/get-for-region';
  27. try {
  28. const response = await axios.post(apiUrl, {region}, {headers});
  29. if (response.data.data) {
  30. const first100Markers = response.data.data.slice(0, 20);
  31. first100Markers.forEach(async (marker) => {
  32. await db.transaction(tx => {
  33. tx.executeSql(
  34. 'INSERT INTO markers (a, d, g, i, marker_id, l, m, n, new, s, sid, v) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);',
  35. [marker.a, marker.d, marker.g, marker.i, marker.marker_id, marker.l, marker.m, marker.n, marker.new, marker.s, marker.sid, marker.v],
  36. (res) => {},
  37. (error) => { console.error("Error saving GeoJSON data: ", error); }
  38. );
  39. });
  40. });
  41. }
  42. } catch (error) {
  43. console.error('Error fetching data from API:', error);
  44. }
  45. }