map.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. // activity/map/map.js
  2. const App = getApp()
  3. var QQMapWX = require('../../utils/qqmap-wx-jssdk.js');
  4. const {
  5. timestampToTime
  6. } = require('../../utils/util.js');
  7. var map;
  8. map = new QQMapWX({
  9. key: 'RA3BZ-PSGW4-GZUUX-DDAU7-6B54E-KJFQ7' // 必填
  10. });
  11. Page({
  12. /**
  13. * 页面的初始数据
  14. */
  15. data: {
  16. lat: '',
  17. lng: '',
  18. markers: []
  19. },
  20. /**
  21. * 生命周期函数--监听页面加载
  22. */
  23. onLoad(options) {
  24. this.setData({
  25. lng: options.lng,
  26. lat: options.lat
  27. })
  28. // this.getCoord()
  29. },
  30. /**
  31. * 生命周期函数--监听页面初次渲染完成
  32. */
  33. onReady() {
  34. },
  35. /**
  36. * 生命周期函数--监听页面显示
  37. */
  38. onShow() {
  39. },
  40. // 获取经纬度
  41. getCoord() {
  42. wx.getLocation({
  43. type: 'gcj02',
  44. success: (res) => {
  45. const latitude = res.latitude
  46. const longitude = res.longitude
  47. this.setData({
  48. lng: Number(longitude),
  49. lat: Number(latitude)
  50. })
  51. },
  52. fail: (e) => {}
  53. })
  54. },
  55. selectAddress(e) {
  56. var pages = getCurrentPages();
  57. var currPage = pages[pages.length - 1]; //当前页面
  58. var prevPage = pages[pages.length - 2]; //上一个页面
  59. prevPage.setData({
  60. address: e,
  61. coordinate: this.data.lng + ',' + this.data.lat
  62. })
  63. wx.navigateBack()
  64. },
  65. mapTap(e) {
  66. wx.showLoading({
  67. title: '获取位置中...',
  68. })
  69. let lat = e.detail.latitude
  70. let lng = e.detail.longitude
  71. let obj = {
  72. latitude: lat,
  73. longitude: lng,
  74. width: 17,
  75. height: 24
  76. }
  77. this.data.markers.push(obj)
  78. this.setData({
  79. markers: this.data.markers
  80. })
  81. App._get('store/getDetailsAddress', {
  82. lon: lng,
  83. lat: lat
  84. }, res => {
  85. console.log(res, 111)
  86. if (res.code === 0) {
  87. wx.hideLoading()
  88. this.selectAddress(res.data.street_number)
  89. }
  90. })
  91. // this.pointToAddress(lat, lng, res => {
  92. // console.log(res, 11111111)
  93. // })
  94. },
  95. bindmarkertap(e) {
  96. console.log(e, 222)
  97. },
  98. bindpoitap(e) {
  99. console.log(e, 222)
  100. },
  101. bindlabeltap(e) {
  102. console.log(e, 333)
  103. },
  104. // 定义 pointToAddress 方法
  105. pointToAddress: function (latitude, longitude, callback) {
  106. var _this = this;
  107. // 调用接口
  108. map.reverseGeocoder({
  109. location: {
  110. latitude: latitude,
  111. longitude: longitude
  112. },
  113. success: function (res) {
  114. // 解析成功返回地址
  115. callback(res.result.ad_info);
  116. },
  117. fail: function (res) {
  118. console.log(res);
  119. },
  120. complete: function (res) {}
  121. });
  122. },
  123. /**
  124. * 生命周期函数--监听页面隐藏
  125. */
  126. onHide() {
  127. },
  128. /**
  129. * 生命周期函数--监听页面卸载
  130. */
  131. onUnload() {
  132. },
  133. /**
  134. * 页面相关事件处理函数--监听用户下拉动作
  135. */
  136. onPullDownRefresh() {
  137. },
  138. /**
  139. * 页面上拉触底事件的处理函数
  140. */
  141. onReachBottom() {
  142. },
  143. /**
  144. * 用户点击右上角分享
  145. */
  146. onShareAppMessage() {
  147. }
  148. })