MapUtils.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. import * as Location from 'expo-location';
  2. import * as Permissions from 'expo-permissions';
  3. import * as TaskManager from 'expo-task-manager';
  4. import request from './RequestUtils';
  5. import { alert } from './TotastUtils';
  6. const LOCATION_TASK_NAME = 'background-location-task';
  7. TaskManager.defineTask(LOCATION_TASK_NAME, ({ data, error }) => {
  8. if (error) {
  9. // Error occurred - check `error.message` for more details.
  10. return;
  11. }
  12. if (data) {
  13. const { locations } = data;
  14. console.log(data);
  15. // do something with the locations captured in the background
  16. }
  17. });
  18. const key = 'c4faf80125b298f93bbc1477db10e69c';
  19. const tengxunKey = 'GLFBZ-ZR2W6-76XSA-MF7CQ-GDJ6Z-6FB5K';
  20. let lat = '31.981746';
  21. let lng = '118.734661';
  22. async function getLocation() {
  23. return Location.requestPermissionsAsync()
  24. .then((res) => {
  25. if (res.status === 'granted') {
  26. return Location.getCurrentPositionAsync(LOCATION_TASK_NAME, {
  27. enableHighAccuracy: false,
  28. timeout: 5000,
  29. maximumAge: 10000,
  30. });
  31. } else {
  32. return Promise.reject();
  33. }
  34. })
  35. .then(({ coords }) => {
  36. lat = coords.latitude;
  37. lng = coords.longitude;
  38. return request.get(
  39. `https://apis.map.qq.com/ws/coord/v1/translate?locations=${lat},${lng}&type=1&key=${tengxunKey}&get_poi=1`,
  40. {
  41. prefix: '',
  42. mode: 'no-cors',
  43. }
  44. );
  45. })
  46. .then((res) => {
  47. if (res.status === 0) {
  48. lat = res.locations[0].lat;
  49. lng = res.locations[0].lng;
  50. }
  51. return request.get(
  52. `https://apis.map.qq.com/ws/geocoder/v1/?location=${lat},${lng}&key=${tengxunKey}&get_poi=1`,
  53. {
  54. prefix: '',
  55. mode: 'no-cors',
  56. }
  57. );
  58. })
  59. .then((res) => {
  60. if (res.status === 0) {
  61. return Promise.resolve({
  62. addressName: res.result.address,
  63. location: res.result.location,
  64. });
  65. } else {
  66. return Promise.reject();
  67. }
  68. })
  69. .catch((e) => {
  70. console.log(e);
  71. return Promise.resolve({
  72. addressName: '定位失败',
  73. location: {
  74. lat,
  75. lng,
  76. },
  77. });
  78. });
  79. }
  80. function getSearch(searchKey, boundary) {
  81. return request
  82. .get(
  83. `https://apis.map.qq.com/ws/place/v1/search?boundary=${boundary}&keyword=${searchKey}&page_size=20&page_index=1&orderby=_distance&key=${tengxunKey}`,
  84. {
  85. prefix: '',
  86. mode: 'no-cors',
  87. }
  88. )
  89. .then((res) => {
  90. if (res.status === 0) {
  91. return Promise.resolve({
  92. pois: res.data,
  93. });
  94. } else {
  95. return Promise.reject();
  96. }
  97. })
  98. .catch(() => {
  99. return Promise.resolve({
  100. pois: [],
  101. });
  102. });
  103. }
  104. function mapMarks(params) {
  105. return `https://restapi.amap.com/v3/staticmap?zoom=15&size=500*500&paths=10,0x0000ff,1,,:116.31604,39.96491;116.320816,39.966606;116.321785,39.966827;116.32361,39.966957&key=${key}`;
  106. }
  107. export { getLocation, getSearch };