|
3 | 3 | <head> |
4 | 4 | <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> |
5 | 5 | <title>聚类显示</title> |
6 | | - <script include="jquery" src="./static/libs/include-lib-local.js"></script> |
| 6 | + <script include="jquery,papaparse" src="./static/libs/include-lib-local.js"></script> |
7 | 7 | <script include="" src="./static/libs/include-mapboxgl-local.js"></script> |
8 | 8 | <style> |
9 | 9 | body { |
|
26 | 26 | var tiandituKey = 'f5347cab4b28410a6e8ba5143e3d5a35'; |
27 | 27 | var map = new mapboxgl.Map({ |
28 | 28 | container: 'map', |
| 29 | + crs: 'EPSG:4326', |
29 | 30 | style: { |
30 | 31 | version: 8, |
31 | 32 | sources: { |
|
36 | 37 | //来源请求地址,请求天地图提供的全球矢量地图WMTS服务 |
37 | 38 | 'http://t' + |
38 | 39 | Math.round(Math.random() * 7) + |
39 | | - '.tianditu.gov.cn/vec_w/wmts?SERVICE=WMTS&REQUEST=GetTile&VERSION=1.0.0' + |
40 | | - '&LAYER=vec&STYLE=default&TILEMATRIXSET=w&FORMAT=tiles' + |
| 40 | + '.tianditu.gov.cn/vec_c/wmts?SERVICE=WMTS&REQUEST=GetTile&VERSION=1.0.0' + |
| 41 | + '&LAYER=vec&STYLE=default&TILEMATRIXSET=c&FORMAT=tiles' + |
41 | 42 | '&TILECOL={x}&TILEROW={y}&TILEMATRIX={z}' + |
42 | 43 | '&tk=' + |
43 | 44 | tiandituKey |
|
62 | 63 | }); |
63 | 64 |
|
64 | 65 | map.on('load', () => { |
65 | | - $.get('./../static/data/geojson/china-city.geojson', (data) => { |
66 | | - addCluster(data); |
| 66 | + $.get('../../static/data/geojson/chinaEarthquake.csv', function (response) { |
| 67 | + var dataObj = Papa.parse(response, { |
| 68 | + skipEmptyLines: true, |
| 69 | + header: true |
| 70 | + }); |
| 71 | + |
| 72 | + var data = dataObj.data; |
| 73 | + |
| 74 | + var geojson = { |
| 75 | + type: 'FeatureCollection', |
| 76 | + features: [] |
| 77 | + }; |
| 78 | + |
| 79 | + for (var i = 0; i < data.length; i++) { |
| 80 | + var item = data[i]; |
| 81 | + var date = new Date(item.date); |
| 82 | + var year = date.getFullYear(); |
| 83 | + |
| 84 | + //2w+地震数据 |
| 85 | + if (year > 2000 && year < 2015) { |
| 86 | + var feature = { |
| 87 | + type: 'feature', |
| 88 | + geometry: { |
| 89 | + type: 'Point', |
| 90 | + coordinates: [] |
| 91 | + }, |
| 92 | + properties: { |
| 93 | + value: parseFloat(item.level) |
| 94 | + } |
| 95 | + }; |
| 96 | + feature.geometry.coordinates = [parseFloat(item.X), parseFloat(item.Y)]; |
| 97 | + |
| 98 | + geojson.features.push(feature); |
| 99 | + } |
| 100 | + } |
| 101 | + addCluster(geojson); |
67 | 102 | }); |
68 | 103 | }); |
69 | 104 |
|
70 | 105 | function addCluster(geojson) { |
71 | 106 | // map.add |
72 | | - map.addSource('clusterdata', { |
| 107 | + map.addSource('earthquakes', { |
73 | 108 | type: 'geojson', |
74 | 109 | data: geojson, |
75 | 110 | cluster: true, |
|
78 | 113 | }); |
79 | 114 |
|
80 | 115 | map.addLayer({ |
81 | | - id: `clusters-layer`, |
| 116 | + id: 'clusters', |
82 | 117 | type: 'circle', |
83 | | - source: 'clusterdata', |
84 | | - filter: ['has', 'name'], |
| 118 | + source: 'earthquakes', |
| 119 | + filter: ['has', 'point_count'], |
85 | 120 | paint: { |
86 | | - 'circle-color': '#51bbd6', |
87 | | - 'circle-radius': 20, |
88 | | - 'circle-stroke-width': 5, //轮廓线宽度 |
89 | | - 'circle-stroke-color': '#FFFFFF' //轮廓线颜色 |
| 121 | + 'circle-color': ['step', ['get', 'point_count'], '#51bbd6', 100, '#f1f075', 750, '#f28cb1'], |
| 122 | + 'circle-radius': ['step', ['get', 'point_count'], 20, 100, 30, 750, 40], |
| 123 | + 'circle-stroke-color': '#FFFFFF', |
| 124 | + 'circle-stroke-width': 5 |
90 | 125 | } |
91 | 126 | }); |
92 | 127 |
|
93 | 128 | map.addLayer({ |
94 | 129 | id: 'cluster-count', |
95 | 130 | type: 'symbol', |
96 | | - source: 'clusterdata', |
97 | | - filter: ['has', 'name'], |
| 131 | + source: 'earthquakes', |
| 132 | + filter: ['has', 'point_count'], |
98 | 133 | layout: { |
99 | | - 'text-field': '{name}', |
| 134 | + 'text-field': '{point_count_abbreviated}', |
100 | 135 | 'text-font': ['宋体', '宋体'], |
101 | 136 | 'text-size': 12 |
102 | 137 | } |
103 | 138 | }); |
| 139 | + |
| 140 | + map.addLayer({ |
| 141 | + id: 'unclustered-point', |
| 142 | + type: 'circle', |
| 143 | + source: 'earthquakes', |
| 144 | + filter: ['!', ['has', 'point_count']], |
| 145 | + paint: { |
| 146 | + 'circle-color': '#11b4da', |
| 147 | + 'circle-radius': 4, |
| 148 | + 'circle-stroke-width': 1, |
| 149 | + 'circle-stroke-color': '#fff' |
| 150 | + } |
| 151 | + }); |
104 | 152 | } |
105 | 153 | </script> |
106 | 154 | </body> |
|
0 commit comments