map.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. // pages/map/map.js
  2. import request from '../../utils/request'
  3. Page({
  4. /**
  5. * 页面的初始数据
  6. */
  7. data: {
  8. subkey: 'U7XBZ-KCDLU-AUOVO-BO64B-BQ6F2-HNFEI',
  9. showlocation: "true", //显示带有方向的当前定位点
  10. showcompass: "true", //显示指南针
  11. enabletraffic: "false", //是否开启实时路况
  12. longitude: "103.70618", //地图中心经度
  13. latitude: "30.51249", //地图中心纬度
  14. //标记点位
  15. marks: [],
  16. //搜索点位
  17. searchmarks: [{
  18. latitude: 30.61249,
  19. longitude: 103.69618,
  20. title: "",
  21. iconPath: "../../images/location.png",
  22. width: 30,
  23. height: 30
  24. }],
  25. points: [], //可视页面显示的点数组,通过遍历marks获得
  26. searchContent: '', //搜索内容
  27. showSearch: false //是否展示搜索内容区
  28. },
  29. /**
  30. * 生命周期函数--监听页面加载
  31. */
  32. onLoad: async function (options) {
  33. let result = await request('/merchant/merchantAndCallout')
  34. // console.log("结果数据:", result);
  35. var marker = [];
  36. for (let i = 0; i < result.length; i++) {
  37. //console.log(result[i].status);
  38. if (result[i].status == 1) {
  39. marker[i] = {
  40. id: result[i].id,
  41. latitude: result[i].latitude,
  42. longitude: result[i].longitude,
  43. title: result[i].title,
  44. iconPath: "../../images/location.png",
  45. width: result[i].width,
  46. height: result[i].height,
  47. callout: result[i].callout
  48. }
  49. }
  50. }
  51. // console.log(marker);
  52. this.setData({
  53. marks:marker,
  54. longitude:marker[0].longitude,
  55. latitude:marker[0].latitude
  56. })
  57. let marks = this.data.marks
  58. // console.log(marks);
  59. var arr = new Array(marks.length)
  60. for (let i = 0; i < marks.length; i++) {
  61. let latitude = marks[i].latitude
  62. let longitude = marks[i].longitude
  63. arr[i] = {
  64. longitude,
  65. latitude
  66. }
  67. }
  68. // console.log(arr);
  69. this.setData({
  70. points: arr
  71. })
  72. },
  73. /**
  74. * 生命周期函数--监听页面初次渲染完成
  75. */
  76. onReady() {
  77. //页面加载后默认定位到用户当前所在位置
  78. // const map = wx.createMapContext('map');
  79. // map.moveToLocation();
  80. },
  81. // 输入框聚焦时的事件
  82. getList() {
  83. this.setData({
  84. showSearch: true,
  85. searchContent: ''
  86. })
  87. },
  88. //定义模糊查找的函数
  89. findTarget(list, keyword, attribute = "name") {
  90. const reg = new RegExp(keyword)
  91. const arr = []
  92. for (let i = 0; i < list.length; i++) {
  93. if (reg.test(list[i][attribute])) {
  94. arr.push(list[i])
  95. }
  96. }
  97. return arr
  98. },
  99. //获取输入的搜索数据
  100. getSearchInput(e) {
  101. // console.log(e.detail.value);
  102. this.setData({
  103. searchContent: e.detail.value
  104. })
  105. // console.log(this.data.searchContent);
  106. let marks = this.data.marks
  107. let searchContent = this.data.searchContent
  108. let arr = this.findTarget(marks, searchContent, 'title')
  109. if (arr.length) {
  110. this.setData({
  111. searchmarks: arr
  112. })
  113. }
  114. // console.log(this.data.searchmarks);
  115. },
  116. // 点击搜索待选区选项的事件
  117. myChosed(e) {
  118. // console.log(e.currentTarget.dataset.index);
  119. let index = e.currentTarget.dataset.index
  120. let marks = this.data.marks
  121. let searchmarks = this.data.searchmarks
  122. let searchContent = this.data.searchContent
  123. if (!searchContent) {
  124. this.setData({
  125. searchContent: marks[index].title,
  126. showSearch: false
  127. })
  128. } else {
  129. this.setData({
  130. searchContent: searchmarks[index].title,
  131. showSearch: false
  132. })
  133. }
  134. let arr = this.findTarget(marks, this.data.searchContent, 'title')
  135. this.setData({
  136. searchmarks: arr
  137. })
  138. // console.log(this.data.searchContent);
  139. },
  140. //点击蒙层的事件
  141. shut() {
  142. this.setData({
  143. showSearch: false
  144. })
  145. },
  146. //点击标记点的事件
  147. bmakt(e) {
  148. // console.log(e.markerId);
  149. var id = e.markerId;
  150. let marks = this.data.marks
  151. var index
  152. for(let i = 0;i < marks.length;i++){
  153. if(id == marks[i].id){
  154. index = i
  155. }
  156. }
  157. let lati = Number(marks[index].latitude)
  158. let longti = Number(marks[index].longitude)
  159. console.log(lati,longti);
  160. // console.log(typeof(lati));
  161. // //拉起外部地图,并传递经纬度和名称
  162. wx.openLocation({
  163. latitude: lati,
  164. longitude: longti,
  165. name: marks[index].title
  166. })
  167. },
  168. //我的位置点击事件
  169. myLocate() {
  170. const map = wx.createMapContext('map');
  171. map.moveToLocation();
  172. },
  173. //返回按钮点击事件
  174. returnBack() {
  175. let points = this.data.points
  176. const map = wx.createMapContext('map');
  177. map.includePoints({
  178. points: points,
  179. padding: [40, 40, 40, 40]
  180. });
  181. },
  182. /**
  183. * 生命周期函数--监听页面显示
  184. */
  185. onShow() {
  186. },
  187. /**
  188. * 生命周期函数--监听页面隐藏
  189. */
  190. onHide() {
  191. },
  192. /**
  193. * 生命周期函数--监听页面卸载
  194. */
  195. onUnload() {
  196. },
  197. /**
  198. * 页面相关事件处理函数--监听用户下拉动作
  199. */
  200. onPullDownRefresh() {
  201. },
  202. /**
  203. * 页面上拉触底事件的处理函数
  204. */
  205. onReachBottom() {
  206. },
  207. /**
  208. * 用户点击右上角分享
  209. */
  210. onShareAppMessage() {
  211. }
  212. })