MapUtils.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 './SystemUtils';
  6. import * as Linking from 'expo-linking';
  7. const key = 'c4faf80125b298f93bbc1477db10e69c';
  8. const tengxunKey = 'GLFBZ-ZR2W6-76XSA-MF7CQ-GDJ6Z-6FB5K';
  9. let lat = '31.981746';
  10. let lng = '118.734661';
  11. async function getLocation() {
  12. return Location.requestPermissionsAsync()
  13. .then((res) => {
  14. if (res.status === 'granted') {
  15. return Location.getCurrentPositionAsync({
  16. enableHighAccuracy: true,
  17. timeout: 5000,
  18. });
  19. } else {
  20. return Promise.reject();
  21. }
  22. })
  23. .then(({ coords }) => {
  24. lat = coords.latitude;
  25. lng = coords.longitude;
  26. return request.get(
  27. `https://apis.map.qq.com/ws/coord/v1/translate?locations=${lat},${lng}&type=1&key=${tengxunKey}&get_poi=1`,
  28. {
  29. prefix: '',
  30. mode: 'no-cors',
  31. }
  32. );
  33. })
  34. .then((res) => {
  35. if (res.status === 0) {
  36. lat = res.locations[0].lat;
  37. lng = res.locations[0].lng;
  38. }
  39. return request.get(
  40. `https://apis.map.qq.com/ws/geocoder/v1/?location=${lat},${lng}&key=${tengxunKey}&get_poi=1`,
  41. {
  42. prefix: '',
  43. mode: 'no-cors',
  44. }
  45. );
  46. })
  47. .then((res) => {
  48. if (res.status === 0) {
  49. return Promise.resolve({
  50. addressName: res.result.address,
  51. location: res.result.location,
  52. });
  53. } else {
  54. return Promise.reject();
  55. }
  56. })
  57. .catch((e) => {
  58. return request
  59. .get(`https://apis.map.qq.com/ws/location/v1/ip?key=${tengxunKey}`)
  60. .then((res) => {
  61. return Promise.resolve({
  62. addressName: '定位失败',
  63. location: res.result.location,
  64. });
  65. })
  66. .catch((e) => {
  67. return Promise.resolve({
  68. addressName: '定位失败',
  69. location: {
  70. lat,
  71. lng,
  72. },
  73. });
  74. });
  75. });
  76. }
  77. function getSearch(searchKey, boundary) {
  78. return request
  79. .get(
  80. `https://apis.map.qq.com/ws/place/v1/search?boundary=${boundary}&keyword=${searchKey}&page_size=20&page_index=1&orderby=_distance&key=${tengxunKey}`,
  81. {
  82. prefix: '',
  83. mode: 'no-cors',
  84. }
  85. )
  86. .then((res) => {
  87. if (res.status === 0) {
  88. return Promise.resolve({
  89. pois: res.data,
  90. });
  91. } else {
  92. return Promise.reject();
  93. }
  94. })
  95. .catch(() => {
  96. return Promise.resolve({
  97. pois: [],
  98. });
  99. });
  100. }
  101. function mapMarks(params) {
  102. 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}`;
  103. }
  104. function changeCord(lnglat) {
  105. if (lnglat) {
  106. const strs = lnglat.split(',');
  107. return strs[1] + ',' + strs[0];
  108. } else {
  109. return ',';
  110. }
  111. }
  112. function goMap(toName, tocoord, navigation) {
  113. var url = `qqmap://map/routeplan?type=drive&to=${toName}&tocoord=${tocoord}&referer=${tengxunKey}`;
  114. Linking.canOpenURL(url).then((res) => {
  115. if (res) {
  116. Linking.openURL(url);
  117. } else {
  118. alert(navigation, {
  119. msg: '请下载腾讯地图app进行精准导航',
  120. hasCancel: false,
  121. });
  122. }
  123. });
  124. }
  125. export { getLocation, getSearch, tengxunKey, changeCord, goMap };