1
0

map.js 3.7 KB

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