map.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. var api = require('../config/api.js');
  2. var area = require('./area.js');
  3. var util = require('./util.js');
  4. var QQMapWX = require('./qqmap-wx-jssdk.min.js');
  5. var qqmapsdk;
  6. var app = getApp();
  7. function getCity() {
  8. return new Promise(function (resolve, reject) {
  9. qqmapsdk = new QQMapWX({
  10. key: app.globalData.mapKey //这里自己的key秘钥进行填充
  11. });
  12. uni.getSetting({
  13. success: (res) => {
  14. console.log(JSON.stringify(res));
  15. // res.authSetting['scope.userLocation'] == undefined 表示 初始化进入该页面
  16. // res.authSetting['scope.userLocation'] == false 表示 非初始化进入该页面,且未授权
  17. // res.authSetting['scope.userLocation'] == true 表示 地理位置授权
  18. if (res.authSetting['scope.userLocation'] != undefined && res.authSetting['scope.userLocation'] != true) {
  19. uni.showModal({
  20. title: '请求授权当前位置',
  21. content: '需要获取您的地理位置,请确认授权',
  22. success: function (res) {
  23. if (res.cancel) {
  24. reject(1);
  25. } else if (res.confirm) {
  26. uni.openSetting({
  27. success: function (dataAu) {
  28. if (dataAu.authSetting['scope.userLocation'] == true) {
  29. uni.showToast({
  30. title: '授权成功',
  31. icon: 'success',
  32. duration: 1000
  33. });
  34. //再次授权,调用wx.getLocation的API
  35. getLocation()
  36. .then((res) => {
  37. resolve(res);
  38. })
  39. .catch((res) => {
  40. reject(res);
  41. });
  42. } else {
  43. reject(1);
  44. }
  45. }
  46. });
  47. }
  48. }
  49. });
  50. } else {
  51. //调用wx.getLocation的API
  52. getLocation()
  53. .then((res) => {
  54. resolve(res);
  55. })
  56. .catch((res) => {
  57. reject(res);
  58. });
  59. }
  60. }
  61. });
  62. });
  63. }
  64. // 微信获得经纬度
  65. function getLocation() {
  66. return new Promise(function (resolve, reject) {
  67. uni.getLocation({
  68. type: 'wgs84',
  69. success: function (res) {
  70. var latitude = res.latitude;
  71. var longitude = res.longitude;
  72. console.info(res);
  73. qqmapsdk.reverseGeocoder({
  74. location: {
  75. latitude: latitude,
  76. longitude: longitude
  77. },
  78. success: function (res) {
  79. ////reject内容 1:未授权(拿默认城市的默认门店) 2:非业务城市 不在服务范围(拿默认城市的默认门店) 3:异常解析()
  80. let province = res.result.ad_info.province;
  81. let city = res.result.ad_info.city;
  82. let address = res.result.address;
  83. console.info(city);
  84. if (!area.isBusinessCity(city)) {
  85. //非业务城市
  86. reject(2);
  87. } else {
  88. //判断是否有门店服务
  89. // util.request(api.OrderBookGetShop,{lng:longitude,lat:latitude}).then(res=>{
  90. // if(res.errno==0){
  91. var addressInfo = {
  92. address: address,
  93. lng: longitude,
  94. lat: latitude,
  95. shopId: '',
  96. city: city,
  97. shopName: ''
  98. //shopName:res.data.shopName
  99. };
  100. uni.setStorageSync('addressInfo', addressInfo);
  101. resolve(addressInfo);
  102. // }
  103. // else
  104. // reject(3)
  105. // }).catch(res=>{
  106. // reject(3)
  107. // })
  108. }
  109. console.info(res);
  110. },
  111. fail: function (res) {
  112. reject(3);
  113. }
  114. });
  115. },
  116. fail: function (res) {
  117. console.log('fail' + JSON.stringify(res));
  118. reject(3);
  119. }
  120. });
  121. });
  122. }
  123. //判断用户是否拒绝地理位置信息授权,拒绝的话重新请求授权
  124. function getUserLocation() {
  125. let that = this;
  126. uni.getSetting({
  127. success: (res) => {
  128. console.log(JSON.stringify(res));
  129. // res.authSetting['scope.userLocation'] == undefined 表示 初始化进入该页面
  130. // res.authSetting['scope.userLocation'] == false 表示 非初始化进入该页面,且未授权
  131. // res.authSetting['scope.userLocation'] == true 表示 地理位置授权
  132. if (res.authSetting['scope.userLocation'] != undefined && res.authSetting['scope.userLocation'] != true) {
  133. uni.showModal({
  134. title: '请求授权当前位置',
  135. content: '需要获取您的地理位置,请确认授权',
  136. success: function (res) {
  137. if (res.cancel) {
  138. uni.showToast({
  139. title: '拒绝授权',
  140. icon: 'none',
  141. duration: 1000
  142. });
  143. } else if (res.confirm) {
  144. uni.openSetting({
  145. success: function (dataAu) {
  146. if (dataAu.authSetting['scope.userLocation'] == true) {
  147. uni.showToast({
  148. title: '授权成功',
  149. icon: 'success',
  150. duration: 1000
  151. });
  152. //再次授权,调用wx.getLocation2的API
  153. that.getLocation2();
  154. } else {
  155. uni.showToast({
  156. title: '授权失败',
  157. icon: 'none',
  158. duration: 1000
  159. });
  160. }
  161. }
  162. });
  163. }
  164. }
  165. });
  166. } else if (res.authSetting['scope.userLocation'] == undefined) {
  167. //调用wx.getLocation2的API
  168. that.getLocation2();
  169. } else {
  170. //调用wx.getLocation2的API
  171. that.getLocation2();
  172. }
  173. }
  174. });
  175. }
  176. // 获取定位当前位置的经纬度
  177. // function getLocation2() {
  178. // let that = this;
  179. // wx.getLocation({
  180. // type: 'wgs84',
  181. // success: function (res) {
  182. // let latitude = res.latitude
  183. // let longitude = res.longitude
  184. // // app.globalData.lat = res.latitude;//
  185. // // app.globalData.lng = res.longitude;//把onload定位时候的经纬度存到全局
  186. // console.log(res);
  187. // that.getLocal(latitude, longitude)
  188. // },
  189. // fail: function (res) {
  190. // console.log('fail' + JSON.stringify(res))
  191. // }
  192. // })
  193. // }
  194. // 获取定位当前位置的经纬度
  195. function getLocation2() {
  196. return new Promise(function (resolve, reject) {
  197. uni.getLocation({
  198. type: 'wgs84',
  199. success: function (res) {
  200. console.info(res);
  201. app.globalData.lat = res.latitude; //
  202. app.globalData.lng = res.longitude; //把onload定位时候的经纬度存到全局
  203. app.globalData.currentLocationName = res.city;
  204. app.globalData.city = res.city;
  205. resolve(true);
  206. },
  207. fail: function (res) {
  208. console.log('fail' + JSON.stringify(res));
  209. reject(false);
  210. }
  211. });
  212. });
  213. }
  214. // 获取当前地理位置
  215. function getLocal(latitude, longitude) {
  216. let that = this;
  217. qqmapsdk = new QQMapWX({
  218. key: app.globalData.mapKey //这里自己的key秘钥进行填充
  219. });
  220. qqmapsdk.reverseGeocoder({
  221. location: {
  222. latitude: latitude,
  223. longitude: longitude
  224. },
  225. success: function (res) {
  226. console.log(res);
  227. // let province = res.result.ad_info.province
  228. // let city = res.result.ad_info.city
  229. // let district = res.result.ad_info.district;
  230. // // 保存一下当前定位的位置留着后面重新定位的时候搜索附近地址用
  231. // app.globalData.currentLocation = district;
  232. app.globalData.currentLocationName = res.result.address;
  233. // that.setData({
  234. // province: province,
  235. // city: city,
  236. // latitude: latitude,
  237. // longitude: longitude,
  238. // district: district
  239. // })
  240. },
  241. fail: function (res) {
  242. console.log(res);
  243. },
  244. complete: function (res) {
  245. // console.log(res);
  246. }
  247. });
  248. }
  249. module.exports = {
  250. getCity: getCity,
  251. getLocation: getLocation,
  252. getUserLocation: getUserLocation,
  253. getLocation2: getLocation2,
  254. getLocal: getLocal
  255. };