map.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. var api = require('../api/jms.js');
  2. var util=require('../utils/jmsUtil.js');
  3. var QQMapWX = require('../utils/qqmap-wx-jssdk.min.js');
  4. var qqmapsdk;
  5. var app = getApp();
  6. function getCity(){
  7. return new Promise(function(resolve, reject) {
  8. qqmapsdk = new QQMapWX({
  9. key: app.globalData.mapKey //这里自己的key秘钥进行填充
  10. });
  11. wx.getSetting({
  12. success: (res) => {
  13. console.log(JSON.stringify(res))
  14. // res.authSetting['scope.userLocation'] == undefined 表示 初始化进入该页面
  15. // res.authSetting['scope.userLocation'] == false 表示 非初始化进入该页面,且未授权
  16. // res.authSetting['scope.userLocation'] == true 表示 地理位置授权
  17. if (res.authSetting['scope.userLocation'] != undefined && res.authSetting['scope.userLocation'] != true) {
  18. wx.showModal({
  19. title: '请求授权当前位置',
  20. content: '需要获取您的地理位置,请确认授权',
  21. success: function (res) {
  22. if (res.cancel) {
  23. reject(1);
  24. } else if (res.confirm) {
  25. wx.openSetting({
  26. success: function (dataAu) {
  27. if (dataAu.authSetting["scope.userLocation"] == true) {
  28. wx.showToast({
  29. title: '授权成功',
  30. icon: 'success',
  31. duration: 1000
  32. })
  33. //再次授权,调用wx.getLocation的API
  34. getLocation().then((res)=>{
  35. resolve(res)
  36. }).catch((res)=>{
  37. reject(res)
  38. });
  39. } else {
  40. reject(1);
  41. }
  42. }
  43. })
  44. }
  45. }
  46. })
  47. } else {
  48. //调用wx.getLocation的API
  49. getLocation().then((res)=>{
  50. resolve(res)
  51. }).catch((res)=>{
  52. reject(res)
  53. });
  54. }
  55. }
  56. })
  57. })
  58. }
  59. // 微信获得经纬度
  60. function getLocation () {
  61. return new Promise(function(resolve, reject) {
  62. wx.getLocation({
  63. type: 'wgs84',
  64. success: function (res) {
  65. var latitude = res.latitude
  66. var longitude = res.longitude
  67. //console.info(res)
  68. qqmapsdk.reverseGeocoder({
  69. location: {
  70. latitude: latitude,
  71. longitude: longitude
  72. },
  73. success: function (res) {
  74. let province = res.result.ad_info.province
  75. let city = res.result.ad_info.city
  76. let address=res.result.address; //判断是否有门店服务
  77. app.globalData.city=city;
  78. util.request(api.AuthGetShop,{lng:longitude,lat:latitude},"GET").then(response=>{
  79. //console.info( response)
  80. if(response.errno==0){
  81. var addressInfo={
  82. address:address,
  83. lng:longitude,
  84. lat:latitude,
  85. shopId:response.data.storeId
  86. }
  87. wx.setStorageSync('addressInfo', addressInfo)
  88. resolve(addressInfo)
  89. }
  90. else
  91. reject(3)
  92. }).catch(res=>{
  93. reject(4)
  94. })
  95. },
  96. fail: function (res) {
  97. reject(5)
  98. }
  99. });
  100. },
  101. fail: function (res) {
  102. console.log('fail' + JSON.stringify(res))
  103. reject(3)
  104. }
  105. })
  106. })
  107. }
  108. module.exports = {
  109. getCity:getCity,
  110. getLocation:getLocation
  111. }