• 微信小程序+echart实现点亮旅游地图


    背景

    最近看抖音有个很火的特效就是点亮地图,去过哪些地方,于是乎自己也想做一个,结合自己之前做的以家庭为单位的小程序,可以考虑做一个家庭一起点亮地图的功能。

    效果图

    过程

    1,首先就是得去下微信小程序适配的echarts-for-weixin,这个网上很多,就不贴链接了。

    下好后把ec-canvas文件夹放在小程序文件夹中,

    2,然后到echart官网下载echart.js,这里个人建议定制化下载,因为小程序有限制一个文件不能超过2M,定制化一般只有几百k,

    3,去下载中国地图数据的json,去阿里云地图下载

    DataV.GeoAtlas地理小工具系列

    4,开始写代码

    页面

    1. #这是css代码
    2. .container {
    3. position:absolute;
    4. top: 10rpx;
    5. bottom: 0;
    6. left: 0;
    7. right: 0;
    8. height: 800rpx;
    9. display: flex;
    10. flex-direction: column;
    11. align-items: center;
    12. }
    13. ec-canvas {
    14. width: 100%;
    15. height: 100%;
    16. padding:300rpx,0rpx,10rpx,10rpx;
    17. }

    js代码

    1. import * as echarts from '../../ec-canvas/echarts.min';
    2. import geoJson from './mapData.js';
    3. #这是自己封装请求后段接口的工具类
    4. import {HTTP} from '../../config/http.js';
    5. let http = new HTTP();
    6. var chartMap = null;
    7. function setOption(chart,linghtData) {
    8. const option = {
    9. title: {
    10. subtext: '一起点亮地图',
    11. left: 'center',
    12. subtextStyle:{
    13. color:'#1cd9f1'
    14. }
    15. },
    16. tooltip: {
    17. show: true,
    18. trigger: 'item'
    19. },
    20. visualMap: {
    21. show: true,
    22. type: "piecewise",
    23. left: 10,
    24. bottom: "0",
    25. align: "left",
    26. itemWidth: 10,
    27. itemHeight: 10,
    28. textStyle: {
    29. fontSize: 10
    30. },
    31. pieces: [
    32. { min: 3, label: '大家一起去过', color: '#EE30A7' },
    33. { min: 2, max: 3, label: '两个人去过', color: '#FF00FF' },
    34. { min: 1, max: 1, label: '一个人去过', color: '#EE799F' },
    35. { min: 0, max: 0, label: '还没去过', color: '#FFE6BD' }
    36. ]
    37. },
    38. series: [{
    39. type: 'map',
    40. mapType: 'china',
    41. label: {
    42. show: true,
    43. fontSize: 8
    44. },
    45. itemStyle: {
    46. normal: {
    47. borderColor: '#737475',
    48. areaColor: '#fff',
    49. },
    50. emphasis: {
    51. areaColor: '#389BB7',
    52. borderWidth: 0
    53. }
    54. },
    55. animation: false,
    56. data: linghtData
    57. }]
    58. };
    59. chart.setOption(option);
    60. };
    61. Page({
    62. /**
    63. * 页面的初始数据
    64. */
    65. data: {
    66. ecMap: {
    67. lazyLoad: true,
    68. },
    69. array:['黑龙江','吉林','湖南','辽宁','内蒙古','北京','天津','河北','山西','陕西','山东','江苏','宁夏','甘肃','河南','安徽','湖北','上海','浙江','福建','江西','台湾','广东','广西','香港','澳门','海南','南海诸岛','云南','贵州','四川','重庆','西藏','青海','新疆'],
    70. index:0,
    71. familyId:0,
    72. target:"",
    73. targetList:[],
    74. type:"",
    75. itemId:"",
    76. active:0,
    77. lightData:[],
    78. },
    79. onLoad(options) {
    80. #加载地图
    81. this.ecComponent = this.selectComponent('#mychart-dom-map');
    82. this.getMapData();
    83. },
    84. //请求接口数据并初始化图标
    85. getMapData() {
    86. var that = this;
    87. let familyId = wx.getStorageSync('familyId');
    88. http.request({
    89. url: '/api/target/getMapList',
    90. method:'GET',
    91. data:{
    92. "familyId": familyId
    93. },
    94. success (res) {
    95. if (res.code == 0) {
    96. that.setData({
    97. lightData: res.data,
    98. });
    99. that.initChart(res.data);
    100. }
    101. }
    102. });
    103. },
    104. // 初始化图表
    105. initChart(lightData) {
    106. this.ecComponent.init((canvas, width, height, dpr) => {
    107. chartMap = echarts.init(canvas, null, {
    108. width: width,
    109. height: height,
    110. devicePixelRatio: dpr,
    111. });
    112. echarts.registerMap('china', geoJson);
    113. canvas.setChart(chartMap);
    114. setOption(chartMap,lightData);
    115. return chartMap;
    116. });
    117. },
    118. //这个方法就是保存数据调用后段接口后再刷新地图
    119. targetLight() {
    120. var that = this;
    121. http.request({
    122. url: '/api/target/saveLightMap',
    123. method:'POST',
    124. data:{
    125. "familyId": familyId,
    126. "userId":userId,
    127. "province":province,
    128. },
    129. success (res) {
    130. if (res.code == 0) {
    131. //重新加载地图
    132. that.getMapData();
    133. }
    134. }
    135. });
    136. },

    js实现了调用接口获得数据并初始化图标,然后用户可以选择一个省份,点击(点亮地图)按钮,实现动态刷新地图

    这是小程序码,欢迎扫码使用。

  • 相关阅读:
    ROS2使用colcon build编译的工程运行出错,cmake编译的没有问题。
    苹果ipad触控笔哪个好?ipad手写笔推荐
    LeetCode_动态规划_中等_740.删除并获得点数
    卷不动了,还卷吗?
    Vue非父子组件之间的通信
    大一新生HTML期末作业 学生个人网页设计作业 HTML5响应式个人简历网站模板 web前端网页制作课作业
    【Linux】线程同步
    JavaScript对象知识点总结
    一道有趣的最长子序列问题
    你听说过推挽电路吗?避免交越失真
  • 原文地址:https://blog.csdn.net/hq091117/article/details/132912248