index.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. import * as echarts from '../../ec-canvas/echarts.js';
  2. import geoJson from './china.js';
  3. import china from './china2.js';
  4. let that;
  5. //获取应用实例
  6. const
  7. app = getApp(),
  8. Bmob = require('../../utils/Bmob-1.6.1.min.js'),
  9. Storage = require('../../utils/storage.js'),
  10. API = require('../../api/API.js'),
  11. utils = require('../../utils/util.js');
  12. // picker 初始化第二列的数据
  13. let regular = [
  14. {"name":"北京", "log":"116.46", "lat":"39.92"},
  15. {"name":"平谷", "log":"117.1", "lat":"40.13"},
  16. {"name":"密云", "log":"116.85", "lat":"40.37"},
  17. {"name":"顺义", "log":"116.65", "lat":"40.13"},
  18. {"name":"通县", "log":"116.67", "lat":"39.92"},
  19. {"name":"怀柔", "log":"116.62", "lat":"40.32"},
  20. {"name":"大兴", "log":"116.33", "lat":"39.73"},
  21. {"name":"房山", "log":"115.98", "lat":"39.72"},
  22. {"name":"延庆", "log":"115.97", "lat":"40.47"},
  23. {"name":"昌平", "log":"116.2", "lat":"40.22"}
  24. ];
  25. let chart;
  26. var myData = [
  27. {name: '海门', value: [121.15, 31.89]}
  28. // {name: '鄂尔多斯', value: [109.78, 39.60]},
  29. // {name: '招远', value: [120.38, 37.35]},
  30. // {name: '舟山', value: [122.20, 29.98]}
  31. ]
  32. // 初始化 地图
  33. function initChart(canvas, width, height) {
  34. chart = echarts.init(canvas, null, {
  35. width: width,
  36. height: height
  37. });
  38. canvas.setChart(chart);
  39. echarts.registerMap('china', geoJson);
  40. const option = {
  41. tooltip: {},
  42. title:{
  43. show:true,
  44. text:'我的足迹',
  45. textStyle:{
  46. color:'#fff',
  47. fontStyle:'normal',
  48. fontFamily:'monospace',
  49. align:'center'
  50. },
  51. subtext:'遍布'+ myData.length + '城市'
  52. },
  53. backgroundColor:'rgba(0, 0, 0, 0.3)',
  54. // dataZoom: [{ show: true, start: 0, end: 100 }],
  55. geo: {
  56. map: 'china',
  57. roam: false,
  58. zoom:1.5,
  59. // aspectScale:1,
  60. label: {
  61. normal: {
  62. show: true,
  63. textStyle: {
  64. color: '#000'
  65. }
  66. },
  67. emphasis:{
  68. show:false
  69. }
  70. },
  71. itemStyle: {
  72. normal: {
  73. borderColor: '#FFFFCC',
  74. areaColor: '#FFCCCC',
  75. color: '#fff',
  76. textStyle: {
  77. color: '#fff'
  78. }
  79. },
  80. emphasis: {
  81. borderColor: '#FFFFCC',
  82. areaColor: '#FFCCCC',
  83. color: '#fff',
  84. textStyle: {
  85. color: '#fff'
  86. },
  87. borderWidth: 0
  88. }
  89. },
  90. },
  91. series: [
  92. {
  93. name: '足迹', // series名称
  94. type: 'scatter', // series图表类型
  95. coordinateSystem: 'geo', // series坐标系类型
  96. data: myData,
  97. symbolSize: 50,
  98. symbol:'pin',
  99. animation: true,
  100. hoverAnimation:true,
  101. cursor:'pointer',
  102. itemStyle:{
  103. color:'#FF0033'
  104. },
  105. label:{
  106. formatter: '{b}',
  107. show:true
  108. }
  109. }
  110. ],
  111. };
  112. chart.setOption(option);
  113. return chart;
  114. }
  115. Page({
  116. data: {
  117. ec: {
  118. onInit: initChart
  119. },
  120. touch:{
  121. touchFlag:true,
  122. distance: 0,
  123. scale: 1.5,
  124. },
  125. multiIndex:[0,0],
  126. multiArray: [
  127. [
  128. {name: '北京市'},
  129. {name: '上海市'},
  130. {name: '天津市'},
  131. {name: '重庆市'},
  132. {name: '河北省'},
  133. {name: '山西省'},
  134. {name: '辽宁省'},
  135. {name: '吉林省'},
  136. {name: '黑龙江省'},
  137. {name: '浙江省'},
  138. {name: '福建省'},
  139. {name: '山东省'},
  140. {name: '河南省'},
  141. {name: '湖北省'},
  142. {name: '湖南省'},
  143. {name: '广东省'},
  144. {name: '海南省'},
  145. {name: '四川省'},
  146. {name: '贵州省'},
  147. {name: '云南省'},
  148. {name: '江西省'},
  149. {name: '陕西省'},
  150. {name: '青海省'},
  151. {name: '甘肃省'},
  152. {name: '广西壮族自治区'},
  153. {name: '新疆维吾尔自治区'},
  154. {name: '内蒙古自治区'},
  155. {name: '西藏自治区'},
  156. {name: '宁夏回族自治区'},
  157. {name: '中国台湾'},
  158. {name: '中国香港'},
  159. {name: '中国澳门'},
  160. {name: '安徽省'},
  161. {name: '江苏省'}
  162. ], regular
  163. ],
  164. objectMultiArray:china,
  165. selected:regular
  166. },
  167. onLoad: function () {
  168. that = this;
  169. },
  170. onReady:function () {
  171. //监听页面初次渲染完成
  172. },
  173. touchStartHandle(e) {
  174. // 单手指缩放开始,也不做任何处理
  175. if (e.touches.length == 1) {
  176. console.log("单滑了")
  177. return
  178. }
  179. console.log('双手指触发开始')
  180. // 注意touchstartCallback 真正代码的开始
  181. // 一开始我并没有这个回调函数,会出现缩小的时候有瞬间被放大过程的bug
  182. // 当两根手指放上去的时候,就将distance 初始化。
  183. let xMove = e.touches[1].clientX - e.touches[0].clientX;
  184. let yMove = e.touches[1].clientY - e.touches[0].clientY;
  185. let distance = Math.sqrt(xMove * xMove + yMove * yMove);
  186. this.setData({
  187. 'touch.distance': distance,
  188. })
  189. },
  190. touchMoveHandle(e) {
  191. let touch = this.data.touch;
  192. // if(!touch.flag){
  193. // wx.showToast({
  194. // title: '正在缩放中',
  195. // icon: 'none',
  196. // duration: 3000
  197. // })
  198. // return;
  199. // }
  200. // 单手指缩放我们不做任何操作
  201. if (e.touches.length == 1) {
  202. console.log("单滑了");
  203. return
  204. }
  205. console.log('双手指运动开始')
  206. let xMove = e.touches[1].clientX - e.touches[0].clientX;
  207. let yMove = e.touches[1].clientY - e.touches[0].clientY;
  208. // 新的 ditance
  209. let distance = Math.sqrt(xMove * xMove + yMove * yMove);
  210. let distanceDiff = distance - touch.distance;
  211. let newScale = touch.scale + 0.005 * distanceDiff
  212. // 为了防止缩放得太大,所以scale需要限制,同理最小值也是
  213. if (newScale >= 3) {
  214. newScale = 3
  215. }
  216. if (newScale <= 0.6) {
  217. newScale = 0.6
  218. }
  219. // 赋值 新的 => 旧的
  220. this.setData({
  221. 'touch.distance': distance,
  222. 'touch.scale': newScale,
  223. 'touch.diff': distanceDiff
  224. },function () {
  225. //手动设置画布的缩放
  226. touch.flag = false;
  227. if(chart){
  228. chart.setOption({geo:{
  229. zoom:touch.scale
  230. }})
  231. }
  232. setTimeout(function () {
  233. touch.touchFlag = true;
  234. },2000)
  235. })
  236. },
  237. onShow:function () {
  238. //监听页面显示
  239. API.getMap().then(res =>{
  240. if(res.length != 0){
  241. myData = res[0].myData;
  242. if(chart){
  243. chart.setOption({series:[
  244. {
  245. name: '足迹', // series名称
  246. type: 'scatter', // series图表类型
  247. coordinateSystem: 'geo', // series坐标系类型
  248. data: myData,
  249. symbolSize: 50,
  250. symbol:'pin',
  251. animation: true,
  252. hoverAnimation:true,
  253. cursor:'pointer',
  254. itemStyle:{
  255. color:'#FF0033'
  256. },
  257. label:{
  258. formatter: '{b}',
  259. show:true
  260. }
  261. }
  262. ]
  263. })
  264. }
  265. }
  266. })
  267. },
  268. onHide:function () {
  269. saveMap(myData,function () {
  270. console.log('保存map成功')
  271. })
  272. console.error('map:hidden')
  273. //监听页面隐藏
  274. },
  275. onPullDownRefresh: function() {
  276. console.log(1)
  277. },
  278. onReachBottom: function() {
  279. console.log(2)
  280. },
  281. pagemove: function (event) {
  282. },
  283. onShareAppMessage:function () {
  284. //用户点击右上角转发
  285. return {
  286. title: '回到你的幸福時光',
  287. path: '../main/index'
  288. }
  289. },
  290. bindMultiPickerChange: function(e){
  291. let obj = that.data.selected[e.detail.value[1]];
  292. console.log(obj)
  293. let setObj = {
  294. name:obj.name,
  295. value:[Number(obj.log),Number(obj.lat)]
  296. };
  297. console.error(myData)
  298. myData.push(setObj);
  299. if(chart){
  300. chart.setOption({series:[
  301. {
  302. name: '足迹', // series名称
  303. type: 'scatter', // series图表类型
  304. coordinateSystem: 'geo', // series坐标系类型
  305. data: myData,
  306. symbolSize: 50,
  307. symbol:'pin',
  308. animation: true,
  309. hoverAnimation:true,
  310. cursor:'pointer',
  311. itemStyle:{
  312. color:'#FF0033'
  313. },
  314. label:{
  315. formatter: '{b}',
  316. show:true
  317. }
  318. }
  319. ]
  320. })
  321. }
  322. },
  323. bindMultiPickerColumnChange: function (e){
  324. console.log(e)
  325. switch (e.detail.column){
  326. case 0:
  327. for (var i = 0; i < that.data.objectMultiArray.length;i++){
  328. if (that.data.objectMultiArray[i].name == that.data.objectMultiArray[e.detail.value].name){
  329. that.data.selected = that.data.objectMultiArray[i].children
  330. }
  331. }
  332. that.setData({
  333. "multiArray[1]": that.data.selected,
  334. "multiIndex[0]": e.detail.value,
  335. "multiIndex[1]" : 0
  336. })
  337. }
  338. }
  339. })
  340. function saveMap(data,cal) {
  341. console.log(data);
  342. const map = Bmob.Query('map');
  343. map.equalTo("openid","==",app.globalData.userInfo.openid);
  344. map.find().then(res => {
  345. console.log(res);
  346. if(res.length !=0){
  347. map.set('id', res[0].objectId);
  348. map.set('myData',data);
  349. }else {
  350. map.set('openid',app.globalData.userInfo.openid);
  351. map.set('nickName',app.globalData.userInfo.nickName);
  352. map.set('avatarUrl',app.globalData.userInfo.avatarUrl);
  353. map.set('myData',data);
  354. }
  355. map.save().then(res =>{
  356. cal(res);
  357. })
  358. })
  359. }