MapUtils.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 key = 'c4faf80125b298f93bbc1477db10e69c';
  7. const tengxunKey = 'GLFBZ-ZR2W6-76XSA-MF7CQ-GDJ6Z-6FB5K';
  8. let lat = '31.981746';
  9. let lng = '118.734661';
  10. async function getLocation() {
  11. return Location.requestPermissionsAsync()
  12. .then((res) => {
  13. if (res.status === 'granted') {
  14. return Location.getCurrentPositionAsync({
  15. enableHighAccuracy: true,
  16. timeout: 5000,
  17. });
  18. } else {
  19. return Promise.reject();
  20. }
  21. })
  22. .then(({ coords }) => {
  23. lat = coords.latitude;
  24. lng = coords.longitude;
  25. return request.get(
  26. `https://apis.map.qq.com/ws/coord/v1/translate?locations=${lat},${lng}&type=1&key=${tengxunKey}&get_poi=1`,
  27. {
  28. prefix: '',
  29. mode: 'no-cors',
  30. }
  31. );
  32. })
  33. .then((res) => {
  34. if (res.status === 0) {
  35. lat = res.locations[0].lat;
  36. lng = res.locations[0].lng;
  37. }
  38. return request.get(
  39. `https://apis.map.qq.com/ws/geocoder/v1/?location=${lat},${lng}&key=${tengxunKey}&get_poi=1`,
  40. {
  41. prefix: '',
  42. mode: 'no-cors',
  43. }
  44. );
  45. })
  46. .then((res) => {
  47. if (res.status === 0) {
  48. return Promise.resolve({
  49. addressName: res.result.address,
  50. location: res.result.location,
  51. });
  52. } else {
  53. return Promise.reject();
  54. }
  55. })
  56. .catch((e) => {
  57. return request
  58. .get(`https://apis.map.qq.com/ws/location/v1/ip?key=${tengxunKey}`)
  59. .then((res) => {
  60. return Promise.resolve({
  61. addressName: '定位失败',
  62. location: res.result.location,
  63. });
  64. })
  65. .catch((e) => {
  66. return Promise.resolve({
  67. addressName: '定位失败',
  68. location: {
  69. lat,
  70. lng,
  71. },
  72. });
  73. });
  74. });
  75. }
  76. function getSearch(searchKey, boundary) {
  77. return request
  78. .get(
  79. `https://apis.map.qq.com/ws/place/v1/search?boundary=${boundary}&keyword=${searchKey}&page_size=20&page_index=1&orderby=_distance&key=${tengxunKey}`,
  80. {
  81. prefix: '',
  82. mode: 'no-cors',
  83. }
  84. )
  85. .then((res) => {
  86. if (res.status === 0) {
  87. return Promise.resolve({
  88. pois: res.data,
  89. });
  90. } else {
  91. return Promise.reject();
  92. }
  93. })
  94. .catch(() => {
  95. return Promise.resolve({
  96. pois: [],
  97. });
  98. });
  99. }
  100. function mapMarks(params) {
  101. 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}`;
  102. }
  103. export { getLocation, getSearch };