-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
146 lines (132 loc) · 5.94 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
mapboxgl.accessToken = 'pk.eyJ1IjoicG9ueWJveSIsImEiOiJjajVremgyNTYydnA0MnFyeXJmczk2MDRnIn0.XFHQyRIcVOqyd5R7k88Pfw';
var map = new mapboxgl.Map({
container: 'map',
style: 'mapbox://styles/ponyboy/clj0gk00l00u301p7cx0abm95',
center: [104.195397, 35.86166],
zoom: 2.5
});
map.on('load', function () {
map.addControl(new mapboxgl.NavigationControl());
fetch('https://mercurydrinker.github.io/yaogun/nanjing_artists.csv')
.then(response => response.text())
.then(csvText => {
const rows = csvText.trim().split('\n');
const labels = rows.slice(1).map(row => row.split(',')[2]); // 获取C列作为标签
fetch('https://mercurydrinker.github.io/GeoMapData_CN-master/citys/320100.json')
.then(response => response.json())
.then(data => {
const dissolved = turf.dissolve(turf.flatten(data));
const bbox = turf.bbox(dissolved);
const pointsWithin = turf.randomPoint(labels.length, {bbox: bbox});
pointsWithin.features.forEach((feature, index) => {
feature.properties.description = labels[index % labels.length];
});
map.addSource('dissolved-polygon', {
'type': 'geojson',
'data': dissolved
});
map.addLayer({
'id': 'dissolved-polygon-layer',
'type': 'fill',
'source': 'dissolved-polygon',
'paint': {
'fill-color': 'rgba(255, 255, 255, 0.5)',
'fill-outline-color': 'white'
}
});
// 添加各个多边形的图层并描绘绿色的边界
data.features.forEach((feature, index) => {
map.addSource(`polygon-${index}`, {
'type': 'geojson',
'data': feature.geometry
});
map.addLayer({
'id': `polygon-layer-${index}`,
'type': 'line',
'source': `polygon-${index}`,
'paint': {
'line-color': 'green', // 多边形边界颜色
'line-width': 2 // 多边形边界宽度
}
});
});
map.addSource('random-selected-points', {
'type': 'geojson',
'data': pointsWithin,
'cluster': true,
'clusterMaxZoom': 14,
'clusterRadius': 50
});
// 添加聚类图层
map.addLayer({
id: 'clusters',
type: 'circle',
source: 'random-selected-points',
filter: ['has', 'point_count'],
paint: {
'circle-color': [
'step',
['get', 'point_count'],
'#51bbd6',
100,
'#f1f075',
750,
'#f28cb1'
],
'circle-radius': [
'step',
['get', 'point_count'],
20,
100,
30,
750,
40
]
}
});
// 添加聚类计数图层
map.addLayer({
id: 'cluster-count',
type: 'symbol',
source: 'random-selected-points',
filter: ['has', 'point_count'],
layout: {
'text-field': '{point_count_abbreviated}',
'text-font': ['DIN Offc Pro Medium', 'Arial Unicode MS Bold'],
'text-size': 12
}
});
// 添加未聚类点图层
map.addLayer({
id: 'unclustered-point',
type: 'circle',
source: 'random-selected-points',
filter: ['!', ['has', 'point_count']],
paint: {
'circle-color': '#11b4da',
'circle-radius': 4,
'circle-stroke-width': 1,
'circle-stroke-color': '#fff'
}
});
// 添加未聚类点的文本标签图层
map.addLayer({
id: 'unclustered-point-label',
type: 'symbol',
source: 'random-selected-points',
filter: ['!', ['has', 'point_count']],
layout: {
'text-field': ['get', 'description'],
'text-variable-anchor': ['top', 'bottom', 'left', 'right'],
'text-radial-offset': 0.5,
'text-justify': 'auto',
'text-size': 32
},
paint: {
'text-color': '#FFFFFF'
}
});
});
})
.catch(error => console.error('Error:', error));
});