map.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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:"",
  90. city:city,
  91. shopName:""
  92. //shopName:res.data.shopName
  93. }
  94. wx.setStorageSync('addressInfo', addressInfo)
  95. resolve(addressInfo)
  96. // }
  97. // else
  98. // reject(3)
  99. // }).catch(res=>{
  100. // reject(3)
  101. // })
  102. }
  103. console.info( res)
  104. },
  105. fail: function (res) {
  106. reject(3)
  107. }
  108. });
  109. },
  110. fail: function (res) {
  111. console.log('fail' + JSON.stringify(res))
  112. reject(3)
  113. }
  114. })
  115. })
  116. }
  117. function chooseLocation(){
  118. return new Promise(function(resolve, reject) {
  119. wx.getLocation({
  120. type: 'wgs84',
  121. success: function (res) {
  122. var latitude = res.latitude
  123. var longitude = res.longitude
  124. var speed = res.speed
  125. var accuracy = res.accuracy;
  126. wx.chooseLocation({
  127. success: function(res){
  128. app.globalData.lng=res.longitude;
  129. app.globalData.lat=res.latitude;
  130. app.globalData.address=res.address;
  131. resolve(true)
  132. },
  133. fail: function() {
  134. reject(false)
  135. }
  136. })
  137. },
  138. fail: function (res) {
  139. console.log('fail' + JSON.stringify(res))
  140. reject(false)
  141. }
  142. })
  143. })
  144. }
  145. //判断用户是否拒绝地理位置信息授权,拒绝的话重新请求授权
  146. function getUserLocation() {
  147. let that = this;
  148. wx.getSetting({
  149. success: (res) => {
  150. console.log(JSON.stringify(res))
  151. // res.authSetting['scope.userLocation'] == undefined 表示 初始化进入该页面
  152. // res.authSetting['scope.userLocation'] == false 表示 非初始化进入该页面,且未授权
  153. // res.authSetting['scope.userLocation'] == true 表示 地理位置授权
  154. if (res.authSetting['scope.userLocation'] != undefined && res.authSetting['scope.userLocation'] != true) {
  155. wx.showModal({
  156. title: '请求授权当前位置',
  157. content: '需要获取您的地理位置,请确认授权',
  158. success: function (res) {
  159. if (res.cancel) {
  160. wx.showToast({
  161. title: '拒绝授权',
  162. icon: 'none',
  163. duration: 1000
  164. })
  165. } else if (res.confirm) {
  166. wx.openSetting({
  167. success: function (dataAu) {
  168. if (dataAu.authSetting["scope.userLocation"] == true) {
  169. wx.showToast({
  170. title: '授权成功',
  171. icon: 'success',
  172. duration: 1000
  173. })
  174. //再次授权,调用wx.getLocation2的API
  175. that.getLocation2();
  176. } else {
  177. wx.showToast({
  178. title: '授权失败',
  179. icon: 'none',
  180. duration: 1000
  181. })
  182. }
  183. }
  184. })
  185. }
  186. }
  187. })
  188. } else if (res.authSetting['scope.userLocation'] == undefined) {
  189. //调用wx.getLocation2的API
  190. that.getLocation2();
  191. }
  192. else {
  193. //调用wx.getLocation2的API
  194. that.getLocation2();
  195. }
  196. }
  197. })
  198. }
  199. // 获取定位当前位置的经纬度
  200. function getLocation2() {
  201. let that = this;
  202. wx.getLocation({
  203. type: 'wgs84',
  204. success: function (res) {
  205. let latitude = res.latitude
  206. let longitude = res.longitude
  207. // app.globalData.lat = res.latitude;//
  208. // app.globalData.lng = res.longitude;//把onload定位时候的经纬度存到全局
  209. console.log(res);
  210. that.getLocal(latitude, longitude)
  211. },
  212. fail: function (res) {
  213. console.log('fail' + JSON.stringify(res))
  214. }
  215. })
  216. }
  217. // 获取当前地理位置
  218. function getLocal (latitude, longitude) {
  219. let that = this;
  220. qqmapsdk = new QQMapWX({
  221. key: app.globalData.mapKey //这里自己的key秘钥进行填充
  222. });
  223. qqmapsdk.reverseGeocoder({
  224. location: {
  225. latitude: latitude,
  226. longitude: longitude
  227. },
  228. success: function (res) {
  229. console.log(res);
  230. // let province = res.result.ad_info.province
  231. // let city = res.result.ad_info.city
  232. // let district = res.result.ad_info.district;
  233. // // 保存一下当前定位的位置留着后面重新定位的时候搜索附近地址用
  234. // app.globalData.currentLocation = district;
  235. app.globalData.currentLocationName= res.result.address;
  236. // that.setData({
  237. // province: province,
  238. // city: city,
  239. // latitude: latitude,
  240. // longitude: longitude,
  241. // district: district
  242. // })
  243. },
  244. fail: function (res) {
  245. console.log(res);
  246. },
  247. complete: function (res) {
  248. // console.log(res);
  249. }
  250. });
  251. }
  252. module.exports = {
  253. getCity:getCity,
  254. getLocation:getLocation,
  255. getUserLocation:getUserLocation,
  256. getLocation2:getLocation2,
  257. getLocal:getLocal
  258. }