1
0

map.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. // console.log(nearStoreList)
  31. let thirdPartyMerchCode = wx.getStorageSync('thirdPartyMerchCode') ? wx.getStorageSync('thirdPartyMerchCode') : '';
  32. // console.log(thirdPartyMerchCode)
  33. let that = this;
  34. util.getLocation((lng, lat) => {
  35. util.request(api.NearbyList, { longitude: lng, latitude: lat, storeId:'', thirdPartyMerchCode: thirdPartyMerchCode }).then((res) => {
  36. let markers = [];
  37. let points = [];
  38. let nlist = res.data;
  39. // console.log(nlist);
  40. for (var i = 0; i < nlist.length; i++) {
  41. let marker = {
  42. iconPath: "/static/images/address.png",
  43. id: i,
  44. latitude: nlist[i].latitude,
  45. longitude: nlist[i].longitude,
  46. width: 35,
  47. height: 35,
  48. clickable: true,
  49. storeId: nlist[i].id,
  50. storeName: nlist[i].storeName,
  51. storeAddress: nlist[i].storeAddress,
  52. distance: nlist[i].distance,
  53. merchSn: nlist[i].merchSn
  54. };
  55. markers.push(marker);
  56. // console.log(markers);
  57. let point = {
  58. latitude: nlist[i].latitude,
  59. longitude: nlist[i].longitude,
  60. };
  61. points.push(point);
  62. // console.log(points);
  63. }
  64. this.mapCtx.includePoints({
  65. points: points,
  66. })
  67. that.setData({
  68. latitude: lat,
  69. longitude: lng,
  70. markers: markers
  71. })
  72. })
  73. })
  74. },
  75. onReady: function () {
  76. // 页面渲染完成
  77. wx.create
  78. },
  79. onShow: function () {
  80. // 页面显示
  81. },
  82. onHide: function () {
  83. // 页面隐藏
  84. },
  85. onUnload: function () {
  86. // 页面关闭
  87. },
  88. markersClick: function(res) {
  89. // console.log(res);
  90. let that = this;
  91. let marker = that.data.markers[res.markerId];
  92. that.setData({
  93. isShow: true,
  94. storeId: marker.storeId,
  95. merchSn: marker.merchSn,
  96. storeName: marker.storeName,
  97. storeAddress: marker.storeAddress,
  98. distance: marker.distance
  99. });
  100. },
  101. chooseStore(e) {
  102. // console.log(e.currentTarget.dataset.id)
  103. // console.log(e.currentTarget.dataset.merchSn)
  104. let storeId = e.currentTarget.dataset.id;
  105. let merchSn = e.currentTarget.dataset.merchSn;
  106. let that = this;
  107. util.request(api.ChooseStoreId, {
  108. storeId: storeId, merchSn: merchSn,
  109. isRefusedLogin: wx.getStorageSync('isRefusedLogin') }, 'POST').then(function (res) {
  110. if (res.errno === 0) {
  111. wx.removeStorageSync('nearStoreList');
  112. wx.removeStorageSync('storeId');
  113. wx.removeStorageSync('storeVo');
  114. wx.removeStorageSync('currentCategory');
  115. wx.removeStorageSync('mapStoreId');
  116. wx.setStorageSync('storeId', storeId);
  117. var item = "";
  118. for (var i = 0; i < that.data.nearStoreList.length; i++) {
  119. wx.setStorageSync('mapIndexStoreId', that.data.nearStoreList[i].id);//地图切换的门店id,提供给首页tab页做条件
  120. wx.setStorageSync('mapCatalogStoreId', that.data.nearStoreList[i].id);//地图切换的门店id,提供给分类tab页做条件
  121. if (storeId == that.data.nearStoreList[i].id) {
  122. item = that.data.nearStoreList[i];
  123. wx.setStorageSync('storeVo', JSON.stringify(item));
  124. break;
  125. }
  126. }
  127. var pages = getCurrentPages();
  128. var currPage = pages[pages.length - 1];  //当前页面
  129. var prevPage = pages[pages.length - 2]; //上一个页面
  130. // console.log(item)
  131. if (item == "") {
  132. // wx.removeStorageSync('nearStoreList');
  133. // wx.removeStorageSync('storeId');
  134. // wx.removeStorageSync('storeVo');
  135. // that.setData({
  136. // storeName: '附近暂无门店'
  137. // })
  138. } else {
  139. //直接调用上一个页面的setData()方法,把数据存到上一个页面中去
  140. prevPage.setData({
  141. storeName: item.storeName
  142. })
  143. }
  144. prevPage.getIndexData();
  145. prevPage.enableActivity();
  146. prevPage.getGroupData();
  147. wx.navigateBack()
  148. }
  149. })
  150. }
  151. })