MapUtils.ts 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. });
  17. } else {
  18. return Promise.reject();
  19. }
  20. })
  21. .then(({ coords }) => {
  22. lat = coords.latitude;
  23. lng = coords.longitude;
  24. return request.get(
  25. `https://apis.map.qq.com/ws/coord/v1/translate?locations=${lat},${lng}&type=1&key=${tengxunKey}&get_poi=1`,
  26. {
  27. prefix: '',
  28. mode: 'no-cors',
  29. }
  30. );
  31. })
  32. .then((res) => {
  33. if (res.status === 0) {
  34. lat = res.locations[0].lat;
  35. lng = res.locations[0].lng;
  36. }
  37. return request.get(
  38. `https://apis.map.qq.com/ws/geocoder/v1/?location=${lat},${lng}&key=${tengxunKey}&get_poi=1`,
  39. {
  40. prefix: '',
  41. mode: 'no-cors',
  42. }
  43. );
  44. })
  45. .then((res) => {
  46. if (res.status === 0) {
  47. return Promise.resolve({
  48. addressName: res.result.address,
  49. location: res.result.location,
  50. });
  51. } else {
  52. return Promise.reject();
  53. }
  54. })
  55. .catch((e) => {
  56. return Promise.resolve({
  57. addressName: '定位失败',
  58. location: {
  59. lat,
  60. lng,
  61. },
  62. });
  63. });
  64. }
  65. function getSearch(searchKey, boundary) {
  66. return request
  67. .get(
  68. `https://apis.map.qq.com/ws/place/v1/search?boundary=${boundary}&keyword=${searchKey}&page_size=20&page_index=1&orderby=_distance&key=${tengxunKey}`,
  69. {
  70. prefix: '',
  71. mode: 'no-cors',
  72. }
  73. )
  74. .then((res) => {
  75. if (res.status === 0) {
  76. return Promise.resolve({
  77. pois: res.data,
  78. });
  79. } else {
  80. return Promise.reject();
  81. }
  82. })
  83. .catch(() => {
  84. return Promise.resolve({
  85. pois: [],
  86. });
  87. });
  88. }
  89. function mapMarks(params) {
  90. 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}`;
  91. }
  92. export { getLocation, getSearch };