MapUtils.js 2.5 KB

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