map-shq.js 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. // pages/map-shq/map-shq.js
  2. import {
  3. imgUrl,
  4. newImgUrl
  5. } from "../api/request"
  6. import {
  7. getMapDataAll,
  8. getHandMatterPage
  9. } from "../api/yxna-api"
  10. var QQMapWX = require('../../libs/qqmap-wx-jssdk.min.js');
  11. var BMap = new QQMapWX({
  12. key: 'KN3BZ-KUDND-PGH4Y-POVRP-43KF3-RNF4K'
  13. });
  14. Page({
  15. /**
  16. * 页面的初始数据
  17. */
  18. data: {
  19. imgUrl: imgUrl,
  20. newImgUrl: newImgUrl,
  21. custGap: 100000,
  22. latitude: 0,
  23. longitude: 0,
  24. scrollId: 0,
  25. clickType: false,
  26. detail: {},
  27. marks: [],
  28. addressList: [],
  29. circle: [],
  30. deptNow: '',
  31. navType: 0,
  32. page: 1,
  33. limit: 6,
  34. pagePrev: true,
  35. pageNext: false,
  36. eventList: [],
  37. depMap: {
  38. '重庆市南岸区政务服务中心(江南新城中心)': ['重庆市南岸区残联', '重庆市南岸区财政局', '重庆市南岸区文化和旅游发展委员会', '重庆市南岸区档案馆', '重庆市南岸区卫生健康委员会', '重庆市南岸区交通局', '重庆市南岸区规划自然资源局', '重庆市南岸区住房和城乡建设委员会', '重庆市南岸区生态环境局', '重庆市南岸区农业农村委员会', '重庆市南岸区发展和改革委员会', '重庆市南岸区消防救援支队', '重庆市南岸区城市管理局', '重庆市公安局南岸分局'],
  39. '重庆市南岸区政务服务中心(南坪中心)': ['重庆市南岸区市场监管局', '重庆市公安局南岸分局'],
  40. }
  41. },
  42. /**
  43. * 生命周期函数--监听页面加载
  44. */
  45. onLoad(options) {
  46. const _this = this
  47. wx.getLocation({
  48. type: 'gcj02',
  49. success: (res) => {
  50. console.log(res)
  51. initListApi(_this)
  52. initAddress(_this)
  53. _this.setData({
  54. latitude: res.latitude,
  55. longitude: res.longitude,
  56. circle: [{
  57. latitude: res.latitude,
  58. longitude: res.longitude,
  59. radius: _this.data.custGap + 500,
  60. fillColor: '#c4d2ec6a',
  61. color: '#c4d2ec'
  62. }],
  63. marks: [{
  64. id: -1,
  65. latitude: res.latitude,
  66. longitude: res.longitude,
  67. iconPath: imgUrl+"/location.png",
  68. width: '15px',
  69. height: '20px',
  70. label: {
  71. anchorX: 10,
  72. anchorY: -22,
  73. bgColor: '#fff',
  74. borderRadius: 5,
  75. padding: 5,
  76. content: '当前位置',
  77. color: '#333'
  78. }
  79. }]
  80. })
  81. }
  82. })
  83. },
  84. /**
  85. * 生命周期函数--监听页面初次渲染完成
  86. */
  87. onReady() {
  88. },
  89. /**
  90. * 生命周期函数--监听页面显示
  91. */
  92. onShow() {
  93. },
  94. /**
  95. * 生命周期函数--监听页面隐藏
  96. */
  97. onHide() {
  98. },
  99. /**
  100. * 生命周期函数--监听页面卸载
  101. */
  102. onUnload() {
  103. },
  104. /**
  105. * 页面相关事件处理函数--监听用户下拉动作
  106. */
  107. onPullDownRefresh() {
  108. },
  109. /**
  110. * 页面上拉触底事件的处理函数
  111. */
  112. onReachBottom() {
  113. },
  114. /**
  115. * 用户点击右上角分享
  116. */
  117. onShareAppMessage() {
  118. },
  119. openVR(e) {
  120. wx.navigateTo({
  121. url: '../map-vr/map-vr',
  122. });
  123. },
  124. markTap(e) {
  125. clickMark(this, e.markerId)
  126. },
  127. labelTap(e) {
  128. clickMark(this, e.markerId)
  129. },
  130. itemTap(e) {
  131. const index = e.currentTarget.dataset.index
  132. let detail = this.data.addressList[index]
  133. if (detail.tel && !(detail.tel instanceof Array)) {
  134. let arr = detail.tel.split(";")
  135. detail.tel = arr
  136. }
  137. const depMap = this.data.depMap
  138. if (depMap[detail.dep]) {
  139. detail.dept = depMap[detail.dep]
  140. let arr = []
  141. for (let i = 0; i < detail.dept.length; i++) {
  142. arr.push({
  143. dep: detail.dept[i],
  144. name: detail.dept[i].replace('重庆市', '').replace('南岸', '')
  145. })
  146. }
  147. detail.deptAll = depMap[detail.dep].join(',')
  148. detail.dept = arr
  149. }
  150. matterApi(this, {
  151. page: 1,
  152. type: 0,
  153. matterDept: depMap[detail.dep] ? depMap[detail.dep].join(",") : detail.dep.replace('政府', '').replace('便民服务中心', '')
  154. })
  155. this.setData({
  156. clickType: true,
  157. detail: detail,
  158. deptNow: depMap[detail.dep] ? detail.deptAll : '',
  159. page: 1,
  160. navType: 0,
  161. })
  162. },
  163. clickDept(e) {
  164. this.setData({
  165. deptNow: e.currentTarget.dataset.dep || ''
  166. })
  167. const _this = this
  168. matterApi(_this, {
  169. page: 1,
  170. type: 0,
  171. matterDept: e.currentTarget.dataset.dep
  172. })
  173. },
  174. goBack(e) {
  175. this.setData({
  176. clickType: false
  177. })
  178. },
  179. telTap(e) {
  180. let phoneNumber = e.currentTarget.dataset.value;
  181. wx.makePhoneCall({
  182. phoneNumber: phoneNumber,
  183. });
  184. },
  185. matterTab(e) {
  186. this.setData({
  187. navType: e.currentTarget.dataset.type
  188. })
  189. const _this = this
  190. matterApi(_this, {
  191. page: _this.data.page,
  192. type: e.currentTarget.dataset.type,
  193. matterDept: _this.data.deptNow || _this.data.detail.dep.replace('政府', '').replace('便民服务中心', '')
  194. })
  195. },
  196. clickPageTo(e) {
  197. const type = e.currentTarget.dataset.type
  198. if (type == 0 && this.data.pagePrev) {
  199. return
  200. } else if (type == 1 && this.data.pageNext) {
  201. return
  202. }
  203. let page = this.data.page
  204. if (type == 0) {
  205. page--
  206. } else {
  207. page++
  208. }
  209. const _this = this
  210. matterApi(_this, {
  211. page: page,
  212. type: _this.data.navType,
  213. matterDept: _this.data.deptNow || _this.data.detail.dep.replace('政府', '').replace('便民服务中心', '')
  214. })
  215. },
  216. searchMap(e) {
  217. let key = 'KN3BZ-KUDND-PGH4Y-POVRP-43KF3-RNF4K'; //使用在腾讯位置服务申请的key
  218. let referer = '掌上南岸'; //调用插件的app的名称
  219. let changeValue = "transit";
  220. const _this = this
  221. let endPoint = JSON.stringify({
  222. 'name': _this.data.detail.dep,
  223. 'latitude': _this.data.detail.latitude,
  224. 'longitude': _this.data.detail.longitude
  225. });
  226. wx.navigateTo({
  227. url: 'plugin://routePlan/index?key=' + key + '&referer=' + referer + '&endPoint=' + endPoint + "&mode=" + changeValue
  228. });
  229. }
  230. })
  231. function clickMark(_this, markerId) {
  232. if (markerId <= 0) {
  233. return
  234. }
  235. let marks = _this.data.marks
  236. for (let i = 0; i < marks.length; i++) {
  237. if (markerId == marks[i].id) {
  238. marks[i].label.bgColor = '#e1f5fe'
  239. } else {
  240. marks[i].label.bgColor = '#fff'
  241. }
  242. }
  243. _this.setData({
  244. scrollId: markerId,
  245. marks: marks
  246. })
  247. }
  248. function matterApi(_this, params) {
  249. console.log(params)
  250. params.limit = _this.data.limit
  251. _this.setData({
  252. pagePrev: params.page == 1,
  253. page: params.page,
  254. })
  255. getHandMatterPage(params).then((res) => {
  256. _this.setData({
  257. pageNext: params.page * params.limit >= res.count,
  258. eventList: res.data
  259. })
  260. })
  261. }
  262. function initListApi(_this) {
  263. getMapDataAll().then((res) => {
  264. if (res.data) {
  265. let arr = []
  266. for (let key in res.data) {
  267. arr = arr.concat(res.data[key])
  268. }
  269. let scrollId = 0
  270. let depArr = []
  271. let data = []
  272. let markArr = _this.data.marks
  273. for (let i = 0; i < arr.length; i++) {
  274. if (depArr.indexOf(arr[i].dep) == -1) {
  275. depArr.push(arr[i].dep)
  276. arr[i].gap = getDistances(arr[i].latitude, arr[i].longitude, _this.data.latitude, _this.data.longitude)
  277. if (arr[i].gap.m <= _this.data.custGap) {
  278. // if (arr[i].dep == '重庆市南岸区政务服务中心(江南新城中心)'||i<10) {
  279. if (scrollId == 0) {
  280. scrollId = arr[i].id
  281. }
  282. data.push(arr[i])
  283. markArr.push({
  284. id: arr[i].id,
  285. latitude: arr[i].latitude,
  286. longitude: arr[i].longitude,
  287. iconPath: imgUrl+"/location.png",
  288. width: '15px',
  289. height: '20px',
  290. label: {
  291. anchorX: 10,
  292. anchorY: -22,
  293. bgColor: '#fff',
  294. borderRadius: 5,
  295. padding: 5,
  296. content: arr[i].dep,
  297. color: '#333'
  298. }
  299. })
  300. }
  301. }
  302. }
  303. data.sort((a, b) => {
  304. return a.gap.m - b.gap.m
  305. })
  306. _this.setData({
  307. marks: markArr,
  308. addressList: data,
  309. scrollId: scrollId
  310. })
  311. }
  312. })
  313. }
  314. function initAddress(_this) {
  315. console.log(BMap)
  316. BMap.reverseGeocoder({
  317. success: function(res) {
  318. console.log(res)
  319. _this.setData({
  320. address: res.result.address
  321. })
  322. }, fail: function(err) {
  323. console.log(err)
  324. }
  325. })
  326. }
  327. // 根据经纬度计算距离,参数分别为第一点的纬度,经度;第二点的纬度,经度
  328. function getDistances(lat1, lng1, lat2, lng2) {
  329. let EARTH_RADIUS = 6378.137;// 地球半径
  330. let radLat1 = lat1 * Math.PI / 180.0; //lat1 * Math.PI / 180.0=>弧度计算
  331. let radLat2 = lat2 * Math.PI / 180.0;
  332. let a = radLat1 - radLat2;
  333. let b = lng1 * Math.PI / 180.0 - lng2 * Math.PI / 180.0;
  334. let 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)));
  335. s = s * EARTH_RADIUS;
  336. s = Math.round(s * 10000) / 10000;// 输出为公里
  337. return { m: Number((s * 1000).toFixed(2)), km: Number(s.toFixed(2)) }
  338. }