getLocation.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. // #ifdef H5
  2. import { jsonp } from "vue-jsonp";
  3. // #endif
  4. // import wx from 'weixin-js-sdk'
  5. let locationLoading = false;
  6. export default function() {
  7. if (locationLoading) return;
  8. locationLoading = true;
  9. console.log("location start");
  10. // wx.config({
  11. // debug: false, // 开启调试模式,
  12. // appId: data.appId, //必填,企业号的唯一标识,此处企业号corpid
  13. // timestamp: data.timestamp, //必填,生成签名的时间戳
  14. // nonceStr: data.nonceStr, //必填,生成签名的随机串
  15. // signature: data.signature, //必填,签名,见附录1
  16. // jsApiList: ["chooseImage"], //必填,需要使用的JS接口列表JS接口列表见附录2
  17. // });
  18. return new Promise((resolve, reject) => {
  19. let locationType = "gcj02";
  20. // #ifdef APP-PLUS
  21. let ua = uni.getSystemInfoSync().platform;
  22. if (/ios/i.test(ua)) locationType = "wgs84";
  23. // #endif
  24. uni.getLocation({
  25. type: 'wgs84', //gcj02
  26. altitude: false,
  27. isHighAccuracy: false,
  28. geocode: true,
  29. success: function(res) {
  30. console.log("GPS", res);
  31. // const key = "MPRBZ-N4E66-BI3SY-M3L4N-S2AW5-BIBGN";
  32. let url = "";
  33. let latitude = res.latitude;
  34. let longitude = res.longitude;
  35. let data = {
  36. // ...res?.address,
  37. location: {
  38. lat: res.latitude,
  39. lng: res.longitude
  40. }
  41. };
  42. uni.setStorageSync('address', res);
  43. resolve(data);
  44. // let url = "https://apis.map.qq.com/ws/coord/v1/translate";
  45. // let data = {
  46. // locations: `${latitude},${longitude}`,
  47. // key,
  48. // output: "jsonp",
  49. // type: 1,
  50. // };
  51. // jsonp(url, data).then((res) => {
  52. // console.log("坐标转换", res);
  53. // if (res?.status == 0) {
  54. // const translateLocation = res?.locations;
  55. // if (translateLocation && translateLocation?.length) {
  56. // latitude = translateLocation[0]?.lat;
  57. // longitude = translateLocation[0]?.lng;
  58. // }
  59. // }
  60. // uni.request({
  61. // url: "https://api.niusenyun.com/api/v1/getRegionByLocation",
  62. // method: "POST",
  63. // data: {
  64. // latitude,
  65. // longitude,
  66. // },
  67. // success: (res) => {
  68. // console.log("geocoder", res);
  69. // if (res.data?.code == 0) {
  70. // const locationData = res.data?.data;
  71. // const address = locationData?.address_component ?? {};
  72. // const address_info = locationData?.ad_info ?? {};
  73. // const addressData = { ...address_info, ...address };
  74. // uni.setStorage({
  75. // key: "address",
  76. // data: addressData,
  77. // success: function () {},
  78. // });
  79. // resolve(addressData);
  80. // }
  81. // },
  82. // });
  83. // url = "https://apis.map.qq.com/ws/geocoder/v1/";
  84. // data = {
  85. // location: `${latitude},${longitude}`,
  86. // key,
  87. // output: "jsonp",
  88. // };
  89. // jsonp(url, data).then((res) => {
  90. // console.log("geocoder info", res);
  91. // const address = res.result?.address_component ?? {};
  92. // const address_info = res.result?.ad_info ?? {};
  93. // const location = res.result?.location ?? address_info?.location;
  94. // const addressData = {
  95. // ...address_info,
  96. // ...address,
  97. // location,
  98. // };
  99. // resolve(addressData);
  100. // });
  101. // });
  102. // #ifdef APP
  103. // const addressData = res?.address; addressData.location = {
  104. // lng: res?.longitude,
  105. // lat: res?.latitude,
  106. // };
  107. // uni.setStorage({
  108. // key: "address",
  109. // data: addressData,
  110. // success: function() {},
  111. // });
  112. // #endif
  113. },
  114. fail: function(ret) {
  115. console.log("定位失败", ret);
  116. reject(ret);
  117. },
  118. complete: function(ret) {
  119. locationLoading = false;
  120. },
  121. });
  122. });
  123. }