map.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. var util = require('../../utils/util.js');
  2. var api = require('../../config/api.js');
  3. var app = getApp();
  4. Page({
  5. /**
  6. * 页面的初始数据
  7. */
  8. data: {
  9. // 中心点纬度、经度
  10. latitude: "",
  11. longitude: "",
  12. // 标记点 当前位置
  13. markers: [],
  14. nearStoreList: [],
  15. isShow: false,
  16. storeId: "",
  17. storeName: "",
  18. storeAddress: "",
  19. distance: "",
  20. merchSn: ""
  21. },
  22. onLoad: function (options) {
  23. // 页面初始化 options为页面跳转所带来的参数
  24. this.mapCtx = wx.createMapContext('myMap');
  25. this.mapCtx.moveToLocation();
  26. let nearStoreList = wx.getStorageSync('nearStoreList') ? JSON.parse(wx.getStorageSync('nearStoreList')) : [];
  27. this.setData({
  28. nearStoreList: nearStoreList
  29. })
  30. let that = this;
  31. util.getLocation((lng, lat) => {
  32. util.request(api.NearbyList, { longitude: lng, latitude: lat }).then((res) => {
  33. let markers = [];
  34. let points = [];
  35. let nlist = res.data;
  36. for (var i = 0; i < nlist.length; i++) {
  37. let marker = {
  38. iconPath: "/static/images/address.png",
  39. id: i,
  40. latitude: nlist[i].latitude,
  41. longitude: nlist[i].longitude,
  42. width: 35,
  43. height: 35,
  44. clickable: true,
  45. storeId: nlist[i].id,
  46. storeName: nlist[i].storeName,
  47. storeAddress: nlist[i].storeAddress,
  48. distance: nlist[i].distance,
  49. merchSn: nlist[i].merchSn
  50. };
  51. markers.push(marker);
  52. let point = {
  53. latitude: nlist[i].latitude,
  54. longitude: nlist[i].longitude,
  55. };
  56. points.push(point);
  57. }
  58. this.mapCtx.includePoints({
  59. points: points,
  60. })
  61. that.setData({
  62. latitude: lat,
  63. longitude: lng,
  64. markers: markers
  65. })
  66. })
  67. })
  68. },
  69. onReady: function () {
  70. // 页面渲染完成
  71. wx.create
  72. },
  73. onShow: function () {
  74. // 页面显示
  75. },
  76. onHide: function () {
  77. // 页面隐藏
  78. },
  79. onUnload: function () {
  80. // 页面关闭
  81. },
  82. markersClick: function(res) {
  83. // console.log(res);
  84. let that = this;
  85. let marker = that.data.markers[res.markerId];
  86. that.setData({
  87. isShow: true,
  88. storeId: marker.storeId,
  89. merchSn: marker.merchSn,
  90. storeName: marker.storeName,
  91. storeAddress: marker.storeAddress,
  92. distance: marker.distance
  93. });
  94. },
  95. chooseStore(e) {
  96. // console.log(e.currentTarget.dataset.id)
  97. // console.log(e.currentTarget.dataset.merchSn)
  98. let storeId = e.currentTarget.dataset.id;
  99. let merchSn = e.currentTarget.dataset.merchSn;
  100. let that = this;
  101. util.request(api.ChooseStoreId, { storeId: storeId, merchSn: merchSn }, 'POST').then(function (res) {
  102. if (res.errno === 0) {
  103. wx.removeStorageSync('nearStoreList');
  104. wx.removeStorageSync('storeId');
  105. wx.removeStorageSync('storeVo');
  106. wx.removeStorageSync('currentCategory');
  107. wx.setStorageSync('storeId', storeId);
  108. var item = "";
  109. for (var i = 0; i < that.data.nearStoreList.length; i++) {
  110. if (storeId == that.data.nearStoreList[i].id) {
  111. item = that.data.nearStoreList[i];
  112. wx.setStorageSync('storeVo', JSON.stringify(item));
  113. break;
  114. }
  115. }
  116. var pages = getCurrentPages();
  117. var currPage = pages[pages.length - 1];  //当前页面
  118. var prevPage = pages[pages.length - 2]; //上一个页面
  119. if (item == "") {
  120. wx.removeStorageSync('nearStoreList');
  121. wx.removeStorageSync('storeId');
  122. wx.removeStorageSync('storeVo');
  123. that.setData({
  124. storeName: '附近暂无门店'
  125. })
  126. } else {
  127. //直接调用上一个页面的setData()方法,把数据存到上一个页面中去
  128. prevPage.setData({
  129. storeName: item.storeName
  130. })
  131. // that.setData({
  132. // storeName: item.storeName
  133. // })
  134. }
  135. // wx.switchTab({
  136. // url: '/pages/index/index'
  137. // });
  138. prevPage.getIndexData();
  139. prevPage.enableActivity();
  140. prevPage.getGroupData();
  141. wx.navigateBack()
  142. }
  143. })
  144. }
  145. })