map.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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.setStorageSync('storeId', storeId);
  104. var item = "";
  105. for (var i = 0; i < that.data.nearStoreList.length; i++) {
  106. if (storeId == that.data.nearStoreList[i].id) {
  107. item = that.data.nearStoreList[i];
  108. wx.setStorageSync('storeVo', JSON.stringify(item));
  109. break;
  110. }
  111. }
  112. var pages = getCurrentPages();
  113. var currPage = pages[pages.length - 1];  //当前页面
  114. var prevPage = pages[pages.length - 2]; //上一个页面
  115. if (item == "") {
  116. wx.removeStorageSync('nearStoreList');
  117. wx.removeStorageSync('storeId');
  118. wx.removeStorageSync('storeVo');
  119. that.setData({
  120. storeName: '附近暂无门店'
  121. })
  122. } else {
  123. //直接调用上一个页面的setData()方法,把数据存到上一个页面中去
  124. prevPage.setData({
  125. storeName: item.storeName
  126. })
  127. }
  128. prevPage.getIndexData();
  129. prevPage.enableActivity();
  130. prevPage.getGroupData();
  131. wx.navigateBack()
  132. }
  133. })
  134. }
  135. })