localtion.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. // #ifdef H5
  2. import { jsonp } from 'vue-jsonp';
  3. // #endif
  4. import store from '../store/index';
  5. // 导航去某地
  6. export const navigationAddress = (destination) => {
  7. const locationData = destination.split(',');
  8. uni.openLocation({
  9. latitude: locationData[1] * 1,
  10. longitude: locationData[0] * 1,
  11. success: function () {
  12. console.log('success');
  13. },
  14. fail(err) {
  15. console.log(err);
  16. }
  17. });
  18. // APP在搞吧
  19. // window.open(
  20. // `//uri.amap.com/navigation?from=118.115948,24.470662&to=${destination}&mode=car&policy=1&src=com.mzwu.www&callnative=1`
  21. // )
  22. };
  23. /**
  24. * 根据经纬度逆解析地址
  25. */
  26. export const getAdressDetailByLngLat = (lat, lng) => {
  27. return new Promise((resolve, reject) => {
  28. // #ifdef H5
  29. jsonp('https://restapi.amap.com/v3/geocode/regeo', {
  30. key: '5773f02930998e41b0de1d4e1bdbcaa9',
  31. location: `${lng},${lat}`
  32. })
  33. .then((res) => {
  34. console.log(res);
  35. resolve(res);
  36. })
  37. .catch((err) => {
  38. reject(err);
  39. });
  40. // #endif
  41. // #ifdef APP-PLUS
  42. uni.request({
  43. url: 'https://restapi.amap.com/v3/geocode/regeo',
  44. data: {
  45. key: '5773f02930998e41b0de1d4e1bdbcaa9',
  46. location: `${lng},${lat}`
  47. },
  48. header: {},
  49. success: (res) => {
  50. resolve(res.data);
  51. },
  52. fail() {
  53. reject();
  54. }
  55. });
  56. // #endif
  57. });
  58. };
  59. // 根据地址获取
  60. export const getLngLatByAddress = (address) => {
  61. return new Promise((resolve, reject) => {
  62. // #ifdef H5
  63. jsonp('https://restapi.amap.com/v3/geocode/geo', {
  64. key: '5773f02930998e41b0de1d4e1bdbcaa9',
  65. address
  66. })
  67. .then((res) => {
  68. resolve(res);
  69. })
  70. .catch((err) => {
  71. reject(err);
  72. });
  73. // #endif
  74. // #ifdef APP
  75. uni.request({
  76. url: 'https://restapi.amap.com/v3/geocode/geo',
  77. data: {
  78. key: '5773f02930998e41b0de1d4e1bdbcaa9',
  79. address
  80. },
  81. header: {},
  82. success: (res) => {
  83. resolve(res.data);
  84. },
  85. fail() {
  86. reject();
  87. }
  88. });
  89. // #endif
  90. });
  91. };
  92. // 高德
  93. export function MapLoader(onSuccess, onFail) {
  94. let aMapScript = document.createElement('script');
  95. aMapScript.setAttribute('src', 'https://webapi.amap.com/maps?v=1.4.11&key=262e1be2edfaf66333664f33a915ccf3&plugin=AMap.CitySearch');
  96. document.head.appendChild(aMapScript);
  97. return (aMapScript.onload = function () {
  98. AMap.plugin('AMap.Geolocation', function () {
  99. var geolocation = new AMap.Geolocation({
  100. enableHighAccuracy: true,
  101. timeout: 10000,
  102. buttonOffset: new AMap.Pixel(10, 20),
  103. zoomToAccuracy: true,
  104. buttonPosition: 'RB'
  105. });
  106. geolocation.getCurrentPosition();
  107. AMap.event.addListener(geolocation, 'complete', onComplete);
  108. AMap.event.addListener(geolocation, 'error', onError);
  109. function onComplete(data) {
  110. // data是具体的定位信息
  111. const position = data.position;
  112. onSuccess &&
  113. typeof onSuccess === 'function' &&
  114. onSuccess({
  115. latitude: position.lat,
  116. longitude: position.lng
  117. });
  118. }
  119. function onError(data) {
  120. onFail && typeof onFail === 'function' && onFail(data);
  121. }
  122. });
  123. });
  124. }
  125. // 判断用户是否给了定位权限
  126. export const isUserEmpowerLocationPermission = () => {
  127. return new Promise(async (resolve, reject) => {
  128. if (!navigator.permissions && !navigator.permissions.query) {
  129. reject(false);
  130. }
  131. const permissionStatus = await navigator.permissions.query({
  132. name: 'geolocation'
  133. });
  134. const state = permissionStatus.state;
  135. if (state === 'granted') {
  136. resolve(true);
  137. } else if (state === 'denied') {
  138. reject(false);
  139. }
  140. setTimeout(() => {
  141. reject('prompt');
  142. }, 3000);
  143. });
  144. };
  145. // 防止用户通过连接进入页面导致未获取到定位信息就直接发送需要携带当前定位信息的接口
  146. export const getCurrentLocation = (error = true, isRedirectImmediately, isAwait) => {
  147. let count = 2;
  148. let timer = null;
  149. return new Promise(async (resolve, reject) => {
  150. let currentAddress = store.getters.detailAddress;
  151. if (currentAddress) {
  152. resolve(currentAddress);
  153. }
  154. if (isAwait) {
  155. resolve(currentAddress);
  156. }
  157. timer = setInterval(() => {
  158. if (count === 0) {
  159. if (!currentAddress) {
  160. error ? resolve('') : reject(new Error('获取您的定位失败'));
  161. }
  162. clearInterval(timer);
  163. if (isRedirectImmediately) {
  164. timer = setTimeout(() => {
  165. uni.redirectTo({
  166. url: '/pages/choose-location/choose-locatio'
  167. });
  168. }, 2000);
  169. }
  170. }
  171. count--;
  172. }, 1000);
  173. await store.dispatch('location/getCurrentLocation', (data) => {
  174. currentAddress = data.detail;
  175. if (currentAddress) {
  176. resolve(currentAddress);
  177. }
  178. });
  179. });
  180. };