map.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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 nlist = res.data;
  34. for (var i = 0; i < nlist.length; i++) {
  35. let marker = {
  36. iconPath: "/static/images/address.png",
  37. id: i,
  38. latitude: nlist[i].latitude,
  39. longitude: nlist[i].longitude,
  40. width: 35,
  41. height: 35,
  42. clickable: true,
  43. storeId: nlist[i].id,
  44. storeName: nlist[i].storeName,
  45. storeAddress: nlist[i].storeAddress,
  46. distance: nlist[i].distance
  47. };
  48. markers.push(marker);
  49. }
  50. that.setData({
  51. latitude: lat,
  52. longitude: lng,
  53. markers: markers
  54. })
  55. })
  56. })
  57. },
  58. onReady: function () {
  59. // 页面渲染完成
  60. wx.create
  61. },
  62. onShow: function () {
  63. // 页面显示
  64. },
  65. onHide: function () {
  66. // 页面隐藏
  67. },
  68. onUnload: function () {
  69. // 页面关闭
  70. },
  71. markersClick: function(res) {
  72. console.log(res);
  73. let that = this;
  74. if (res.markerId == 0) {
  75. that.setData({
  76. isShow: false
  77. });
  78. return;
  79. }
  80. let marker = that.data.markers[res.markerId];
  81. that.setData({
  82. isShow: true,
  83. storeId: marker.storeId,
  84. storeName: marker.storeName,
  85. storeAddress: marker.storeAddress,
  86. distance: marker.distance
  87. });
  88. },
  89. chooseStore(e) {
  90. console.log(e.currentTarget.id)
  91. let storeId = e.currentTarget.id;
  92. let that = this;
  93. util.request(api.ChooseStoreId, { storeId: storeId }, 'POST').then(function (res) {
  94. if (res.errno === 0) {
  95. wx.setStorageSync('storeId', storeId);
  96. var item = "";
  97. for (var i = 0; i < that.data.nearStoreList.length; i++) {
  98. if (storeId == that.data.nearStoreList[i].id) {
  99. item = that.data.nearStoreList[i];
  100. wx.setStorageSync('storeVo', JSON.stringify(item));
  101. }
  102. }
  103. var pages = getCurrentPages();
  104. var currPage = pages[pages.length - 1];  //当前页面
  105. var prevPage = pages[pages.length - 2]; //上一个页面
  106. //直接调用上一个页面的setData()方法,把数据存到上一个页面中去
  107. prevPage.setData({
  108. storeName: item.storeName
  109. })
  110. prevPage.getIndexData();
  111. prevPage.enableActivity();
  112. prevPage.getGroupData();
  113. wx.navigateBack()
  114. }
  115. })
  116. }
  117. })