map.js 8.0 KB

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