1
0

map.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. let marker = that.data.markers[res.markerId];
  75. that.setData({
  76. isShow: true,
  77. storeId: marker.storeId,
  78. storeName: marker.storeName,
  79. storeAddress: marker.storeAddress,
  80. distance: marker.distance
  81. });
  82. },
  83. chooseStore(e) {
  84. console.log(e.currentTarget.id)
  85. let storeId = e.currentTarget.id;
  86. let that = this;
  87. util.request(api.ChooseStoreId, { storeId: storeId }, 'POST').then(function (res) {
  88. if (res.errno === 0) {
  89. wx.setStorageSync('storeId', storeId);
  90. var item = "";
  91. for (var i = 0; i < that.data.nearStoreList.length; i++) {
  92. if (storeId == that.data.nearStoreList[i].id) {
  93. item = that.data.nearStoreList[i];
  94. wx.setStorageSync('storeVo', JSON.stringify(item));
  95. }
  96. }
  97. var pages = getCurrentPages();
  98. var currPage = pages[pages.length - 1];  //当前页面
  99. var prevPage = pages[pages.length - 2]; //上一个页面
  100. //直接调用上一个页面的setData()方法,把数据存到上一个页面中去
  101. prevPage.setData({
  102. storeName: item.storeName
  103. })
  104. prevPage.getIndexData();
  105. prevPage.enableActivity();
  106. prevPage.getGroupData();
  107. wx.navigateBack()
  108. }
  109. })
  110. }
  111. })