关键词搜索

源码搜索 ×
×

Leaflet笔记-Leaflet与echarts结合使用(在地图上绘图表)

发布2019-09-10浏览9649次

详情内容

目录

 

基本概念

代码与实例


 

基本概念

在本地地图上绘制图表,图表经过响应弹出来。

想想真是令人兴奋的事情,到时候用Qt的Widget和QGraphicsView结合QWebEngine搞一层。来一个科技感十足的魔幻页面。

下面来说明下这个逻辑。

使用Leaflet的WMTS接口请求本地Geoserver服务,从而获取数据。然后再把echarts放到Leaflet的响应事件里面。比如框。弹框的时候构造图表。通过这样的方式,即可完成地图上显示图标。

 

 

代码与实例

程序运行截图如下:

当选中某一个城市后!

源码如下:

test8.html

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>Hello World</title>
  5. <meta charset="utf-8" />
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <link rel="shortcut icon" type="image/x-icon" href="docs/images/favicon.ico" />
  8. <link rel="stylesheet" href="leaflet.css" />
  9. <script src="leaflet.js"></script>
  10. <script src="leaflet-tilelayer-wmts-src.js"></script>
  11. <script src="echarts.js"></script>
  12. <style>
  13. html, body {
  14. height: 100%;
  15. margin: 0;
  16. }
  17. #map {
  18. width: 100%;
  19. height: 100%;
  20. }
  21. .chart{
  22. width: 600px;
  23. height: 300px;
  24. background-color: #fff;
  25. }
  26. </style>
  27. </head>
  28. <body>
  29. <script src="test6.js" type="text/javascript"></script>
  30. <div id='map'></div>
  31. <script type="text/javascript">
  32. var ign = new L.TileLayer.WMTS( "http://XXX.XXX.XXX.XXX:8080/geoserver/gwc/service/wmts" ,
  33. {
  34. layer: 'GG_9:gg_9',
  35. tilematrixset: "EPSG:900913",
  36. Format : 'image/png',
  37. TileMatrix: 'EPSG:900913:8'
  38. }
  39. );
  40. var map = L.map('map', {
  41. minZoom: 4,
  42. maxZoom: 7
  43. }).setView([32, 118], 7);
  44. L.control.scale({'position':'bottomleft','metric':true,'imperial':false}).addTo(map);
  45. map.addLayer(ign);
  46. map.invalidateSize(true);
  47. //添加的数据
  48. function onEachFeature(feature, layer){
  49. var idStr = feature.geometry.properties.idStr.toString() ;
  50. var popupContent = '<div style="width:350px;height:300px" id="' + idStr + '"></div>';
  51. layer.bindPopup(popupContent, {});
  52. layer.on('popupopen', function(e){
  53. var myChart = echarts.init(document.getElementById(idStr));
  54. // 指定图表的配置项和数据
  55. var option = {
  56. title: {
  57. text: '测试'
  58. },
  59. tooltip: {},
  60. legend: {
  61. data:['销量']
  62. },
  63. xAxis: {
  64. data: ["衬衫","羊毛衫","雪纺衫","裤子"]
  65. },
  66. yAxis: {},
  67. series: [{
  68. name: '销量',
  69. type: 'bar',
  70. data: [5, 20, 36, 10]
  71. }]
  72. };
  73. // 使用刚指定的配置项和数据显示图表。
  74. myChart.setOption(option);
  75. });
  76. }
  77. L.geoJSON([bicycleRental], {
  78. onEachFeature: onEachFeature,
  79. pointToLayer: function (feature, latlng) {
  80. return L.circleMarker(latlng, {
  81. radius: 8,
  82. fillColor: "#ff7800",
  83. color: "#000",
  84. weight: 1,
  85. opacity: 1,
  86. fillOpacity: 0.8
  87. });
  88. }
  89. }).addTo(map)
  90. //添加的数据
  91. </script>
  92. </body>
  93. </html>

test6.js

  1. var bicycleRental = {
  2. "type" : "FeatureConllection",
  3. "features" : [
  4. {
  5. "geometry" : {
  6. "type" : "Point",
  7. "coordinates" : [118.8, 32.05],
  8. "properties" : {
  9. "popupContent" : "南京",
  10. "idStr" : "njChart"
  11. }
  12. },
  13. "type" : "Feature",
  14. "id" : 100
  15. },
  16. {
  17. "geometry" : {
  18. "type" : "Point",
  19. "coordinates" : [119.2, 34.55],
  20. "properties" : {
  21. "popupContent" : "连云港",
  22. "idStr" : "lygChart"
  23. }
  24. },
  25. "type" : "Feature",
  26. "id" : 101
  27. },
  28. {
  29. "geometry" : {
  30. "type" : "Point",
  31. "coordinates" : [118.26, 33.94],
  32. "properties" : {
  33. "popupContent" : "宿迁",
  34. "idStr" : "sqChart"
  35. }
  36. },
  37. "type" : "Feature",
  38. "id" : 102
  39. },
  40. {
  41. "geometry" : {
  42. "type" : "Point",
  43. "coordinates" : [119.12, 33.53],
  44. "properties" : {
  45. "popupContent" : "淮安",
  46. "idStr" : "haChart"
  47. }
  48. },
  49. "type" : "Feature",
  50. "id" : 103
  51. },
  52. {
  53. "geometry" : {
  54. "type" : "Point",
  55. "coordinates" : [120.15, 33.32],
  56. "properties" : {
  57. "popupContent" : "盐城",
  58. "idStr" : "ycChart"
  59. }
  60. },
  61. "type" : "Feature",
  62. "id" : 104
  63. },
  64. {
  65. "geometry" : {
  66. "type" : "Point",
  67. "coordinates" : [119.95, 32.40],
  68. "properties" : {
  69. "popupContent" : "泰州",
  70. "idStr" : "tzChart"
  71. }
  72. },
  73. "type" : "Feature",
  74. "id" : 105
  75. },
  76. {
  77. "geometry" : {
  78. "type" : "Point",
  79. "coordinates" : [119.41, 32.35],
  80. "properties" : {
  81. "popupContent" : "扬州",
  82. "idStr" : "yzChart"
  83. }
  84. },
  85. "type" : "Feature",
  86. "id" : 106
  87. },
  88. {
  89. "geometry" : {
  90. "type" : "Point",
  91. "coordinates" : [120.92, 31.94],
  92. "properties" : {
  93. "popupContent" : "南通",
  94. "idStr" : "ntChart"
  95. }
  96. },
  97. "type" : "Feature",
  98. "id" : 107
  99. },
  100. {
  101. "geometry" : {
  102. "type" : "Point",
  103. "coordinates" : [119.99, 31.755],
  104. "properties" : {
  105. "popupContent" : "常州",
  106. "idStr" : "czChart"
  107. }
  108. },
  109. "type" : "Feature",
  110. "id" : 108
  111. },
  112. {
  113. "geometry" : {
  114. "type" : "Point",
  115. "coordinates" : [120.33, 31.44],
  116. "properties" : {
  117. "popupContent" : "无锡",
  118. "idStr" : "wxChart"
  119. }
  120. },
  121. "type" : "Feature",
  122. "id" : 109
  123. },
  124. {
  125. "geometry" : {
  126. "type" : "Point",
  127. "coordinates" : [120.61, 31.26],
  128. "properties" : {
  129. "popupContent" : "苏州",
  130. "idStr" : "szChart"
  131. }
  132. },
  133. "type" : "Feature",
  134. "id" : 110
  135. }
  136. ]
  137. }

这里有一个要注意的地方!

echarts他加载的时候需要一个div,并且还得知道这个div的id,如果没有这个将会报错,说dom找不到这个div

这个弹出的点击小点弹出的功能,是在点击后才创建div的,所以没有点击前相关的dom树如下,当点击后,可以看到此处的结构如下!

 

 

相关技术文章

点击QQ咨询
开通会员
返回顶部
×
微信扫码支付
微信扫码支付
确定支付下载
请使用微信描二维码支付
×

提示信息

×

选择支付方式

  • 微信支付
  • 支付宝付款
确定支付下载