123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. // pages/map/testmap.js
  2. var QQMapWX = require('../../utils/qqmap-wx-jssdk.js');
  3. var qqmapsdk;
  4. import fetch from '../../utils/http'
  5. const $api = require('../../config/api.js').$api;
  6. const page = require('../../utils/page.js');
  7. Page({
  8. /**
  9. * 页面的初始数据
  10. */
  11. data: {
  12. longitude: 116.313972,
  13. latitude: 39.980014,
  14. controls: [],
  15. markers: [],
  16. height: 'auto',
  17. markers: [],
  18. showLayer: false,
  19. windowHeight: 0,
  20. scale: 12,
  21. defaultMarkers: [],
  22. buildings: [],
  23. navList: [{
  24. value: '交通',
  25. id: 'bus',
  26. count: '20'
  27. }, {
  28. value: '教育',
  29. id: 'school',
  30. count: '20'
  31. }, {
  32. value: '医疗',
  33. id: 'hospital',
  34. count: '20'
  35. }, {
  36. value: '商业',
  37. id: 'business',
  38. count: '20'
  39. }],
  40. navIndex: 0,
  41. list: [],
  42. actionBuilding: {},
  43. buildingId: ''
  44. },
  45. /**
  46. * 生命周期函数监听页面加载
  47. */
  48. onLoad: function (options) {
  49. const id = page.getCurrentPageOptions().id
  50. if (id) {
  51. this.setData({
  52. buildingId: id
  53. })
  54. }
  55. qqmapsdk = new QQMapWX({
  56. key: 'KJCBZ-G2MKX-DB443-Z4CBR-7E6K2-GJF5D'
  57. });
  58. //保证wx.getSystemInfo的回调函数中能够使用this
  59. var that = this
  60. //调用wx.getSystemInfo接口,然后动态绑定组件高度
  61. wx.getSystemInfo({
  62. success: function (res) {
  63. that.setData({
  64. windowHeight: res.windowHeight,
  65. height: res.windowHeight
  66. })}
  67. })
  68. },
  69. clickMarker(marker) {
  70. this.setAction(marker.markerId)
  71. },
  72. setAction(buildingId) {
  73. const building = this.data.buildings.filter(x => x.buildingId == buildingId)[0]
  74. if (!building) {
  75. return
  76. }
  77. this.setData({
  78. actionBuilding: building
  79. })
  80. this.search()
  81. },
  82. search() {
  83. const building = this.data.actionBuilding
  84. const _this = this
  85. qqmapsdk.search({
  86. keyword: _this.data.navList[_this.data.navIndex].value, //搜索关键词
  87. page_size: 20,
  88. location: building.coordinate, //设置周边搜索中心点
  89. success: function (res) { //搜索成功后的回调
  90. var navlist = []
  91. for (var i = 0; i < res.data.length; i++) {
  92. navlist.push({
  93. value: res.data[i].title,
  94. id: res.data[i].id,
  95. distance: res.data[i]._distance
  96. })
  97. }
  98. _this.setData({ //设置markers属性,将搜索结果显示在地图中
  99. showLayer: true,
  100. scale: 14,
  101. latitude: building.coordinate.split(',')[0],
  102. longitude: building.coordinate.split(',')[1],
  103. height: 300,
  104. list: navlist,
  105. })
  106. },
  107. fail: function (res) {
  108. console.log(res);
  109. },
  110. complete: function (res) {
  111. console.log(res);
  112. }
  113. });
  114. },
  115. GetGreatCircleDistance(lat1, lng1, lat2, lng2) {
  116. if (lat1 === lat2 && lng1 === lng2) {
  117. return 0
  118. }
  119. var EARTH_RADIUS = 6378137.0//单位M
  120. var PI = Math.PI
  121. var radLat1 = lat1 * PI / 180.0
  122. var radLat2 = lat2 * PI / 180.0
  123. var a = radLat1 - radLat2
  124. var b = lng1 * PI / 180.0 - lng2 * PI / 180.0
  125. var s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) + Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2)))
  126. s = s * EARTH_RADIUS
  127. s = Math.round(s * 10000) / 10000.0
  128. return s / 100
  129. },
  130. /**
  131. * 生命周期函数监听页面初次渲染完成
  132. */
  133. onReady: function() {
  134. const _that = this
  135. fetch({
  136. url: $api.building.list.url,
  137. method: $api.building.list.method,
  138. data: {
  139. pageNum: 1,
  140. pageSize: 10
  141. }
  142. }).then((buildings) => {
  143. let list = buildings.data
  144. if (_that.data.buildingId && _that.data.buildingId != '') {
  145. list = list.filter(x => x.buildingId == _that.data.buildingId)
  146. }
  147. const markers = list.map(x => {
  148. return {
  149. id: x.buildingId,
  150. latitude: x.coordinate.split(',')[0],
  151. longitude: x.coordinate.split(',')[1],
  152. iconPath: '',
  153. label: {
  154. content: x.buildingName,
  155. color: '#fff',
  156. bgColor: '#bb9c79',
  157. padding: 5,
  158. borderRadius: 5
  159. }
  160. }
  161. })
  162. _that.setData({
  163. defaultMarkers: markers,
  164. markers: markers,
  165. longitude: markers[0].longitude,
  166. latitude: markers[0].latitude,
  167. buildings: list
  168. })
  169. if (_that.data.buildingId && _that.data.buildingId != ''){
  170. _that.setAction(_that.data.buildingId)
  171. }
  172. })
  173. },
  174. /**
  175. * 生命周期函数监听页面显示
  176. */
  177. onShow: function() {
  178. },
  179. /**
  180. * 生命周期函数监听页面隐藏
  181. */
  182. onHide: function() {
  183. },
  184. /**
  185. * 生命周期函数监听页面卸载
  186. */
  187. onUnload: function() {
  188. },
  189. /**
  190. * 页面相关事件处理函数监听用户下拉动作
  191. */
  192. onPullDownRefresh: function() {
  193. },
  194. /**
  195. * 页面上拉触底事件的处理函数
  196. */
  197. onReachBottom: function() {
  198. },
  199. /**
  200. * 用户点击右上角分享
  201. */
  202. onShareAppMessage: function() {
  203. },
  204. cutNav(e) {
  205. this.setData({
  206. navIndex: e.target.dataset.index
  207. })
  208. this.search()
  209. },
  210. closeLayer(){
  211. if (this.data.buildingId == '') {
  212. this.setData({
  213. showLayer: false,
  214. height: this.data.windowHeight
  215. })
  216. }
  217. },
  218. toFirstPage() {
  219. wx.navigateTo({
  220. url: '/pages/index/index'
  221. })
  222. },
  223. toDaohang() {
  224. wx.openLocation({
  225. latitude: this.data.actionBuilding.coordinate.split(',')[0] - 0,
  226. longitude: this.data.actionBuilding.coordinate.split(',')[1] - 0,
  227. scale: 16
  228. })
  229. }
  230. })