|
13 | 13 |
|
14 | 14 | .container { |
15 | 15 | width: 100%; |
16 | | - height: 100% |
| 16 | + height: 100%; |
| 17 | + background-color: black; |
17 | 18 | } |
18 | 19 | </style> |
19 | 20 | <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/maptalks/dist/maptalks.css"> |
20 | 21 | <script type="text/javascript" src="https://cdn.jsdelivr.net/npm/maptalks/dist/maptalks.min.js"></script> |
21 | | -<script type="text/javascript" src="./../dist/maptalks.tileclusterlayer.js"></script> |
| 22 | +<script type="text/javascript" src="https://unpkg.com/maptalks.tileclusterlayer@0.0.9/dist/maptalks.tileclusterlayer.js"></script> |
22 | 23 | <script type="text/javascript" src="https://maptalks.org/maptalks.markercluster/demo/realworld.50000.1.js"></script> |
23 | 24 | <script type="text/javascript" src="https://maptalks.org/maptalks.markercluster/demo/realworld.50000.2.js"></script> |
| 25 | +<script type="text/javascript" src="https://unpkg.com/turf@7.0.0-alpha.1/dist/turf.js"></script> |
24 | 26 |
|
25 | 27 | <body> |
26 | 28 |
|
|
60 | 62 | map.setBaseLayer(tdt); |
61 | 63 | } |
62 | 64 |
|
| 65 | + const layer = new maptalks.VectorLayer('templayer').addTo(map); |
| 66 | + |
| 67 | + map.on('viewchange', () => { |
| 68 | + layer.clear(); |
| 69 | + currentClusterMarker = null; |
| 70 | + }) |
| 71 | + |
63 | 72 | function getClusterMarkerSymbol(count) { |
64 | 73 | const symbol = { |
65 | | - markerType: 'ellipse', |
66 | | - markerWidth: 65, |
67 | | - markerHeight: 65, |
68 | | - markerFill: '#fff', |
69 | | - markerLineWidth: 0, |
70 | | - markerFillOpacity: 1, |
71 | | - markerOpacity: 1, |
| 74 | + // markerType: 'ellipse', |
| 75 | + // markerWidth: 65, |
| 76 | + // markerHeight: 65, |
| 77 | + // markerFill: '#fff', |
| 78 | + // markerLineWidth: 0, |
| 79 | + // markerFillOpacity: 1, |
| 80 | + // markerOpacity: 1, |
| 81 | + markerFile: './marker-case-clusterer1.png', |
| 82 | + markerVerticalAlignment: 'middle', |
72 | 83 | textSize: 15, |
73 | 84 | textName: count, |
74 | 85 | textHaloFill: '#000', |
75 | 86 | textHaloRadius: 1.2, |
76 | 87 | textFill: '#fff' |
77 | 88 | }; |
78 | 89 | if (count > 5000) { |
79 | | - symbol.markerFill = 'red'; |
| 90 | + symbol.markerFile = './marker-case-clusterer3.png'; |
80 | 91 | } else if (count > 1000) { |
81 | | - symbol.markerFill = 'yellow'; |
| 92 | + symbol.markerFile = './marker-case-clusterer2.png'; |
82 | 93 | }; |
83 | 94 | return symbol; |
84 | 95 | } |
|
90 | 101 | dispersionDuration: 300, |
91 | 102 | clusterMarkerSymbol: getClusterMarkerSymbol, |
92 | 103 | markerEvents: { |
93 | | - 'click': function (e) { |
94 | | - console.log(e); |
95 | | - } |
| 104 | + 'click': mouseClick, |
| 105 | + 'mouseover': mouseOver, |
| 106 | + 'mouseout': mouseOut |
96 | 107 | } |
97 | 108 | }); |
98 | 109 | tileClusterLayer.addTo(map); |
99 | 110 |
|
| 111 | + function isClusterMarker(e) { |
| 112 | + return (e.target.getProperties() || {}).isCluster; |
| 113 | + } |
| 114 | + |
| 115 | + function mouseClick(e) { |
| 116 | + if (!isClusterMarker(e)) { |
| 117 | + return; |
| 118 | + } |
| 119 | + // map.setCenter(e.target.getCenter()); |
| 120 | + // map.zoomIn(); |
| 121 | + } |
| 122 | + |
| 123 | + let currentClusterMarker; |
| 124 | + function mouseOver(e) { |
| 125 | + if (!isClusterMarker(e)) { |
| 126 | + return; |
| 127 | + } |
| 128 | + if (currentClusterMarker === e.target) { |
| 129 | + return; |
| 130 | + } |
| 131 | + const features = e.target.getProperties().features || []; |
| 132 | + if (features.length > e.target.getLayer().options.dispersionCount) { |
| 133 | + return; |
| 134 | + } |
| 135 | + const points = features.map(f => { |
| 136 | + return f; |
| 137 | + }); |
| 138 | + const result = turf.concave(turf.featureCollection(points)); |
| 139 | + if (!result) { |
| 140 | + return; |
| 141 | + } |
| 142 | + layer.clear(); |
| 143 | + const polygons = maptalks.GeoJSON.toGeometry(result); |
| 144 | + polygons.forEach(p => { |
| 145 | + p.setSymbol({ |
| 146 | + lineColor: 'yellow', |
| 147 | + lineWidth: 1.5 |
| 148 | + }) |
| 149 | + }) |
| 150 | + layer.addGeometry(polygons); |
| 151 | + currentClusterMarker = e.target; |
| 152 | + } |
| 153 | + |
| 154 | + function mouseOut(e) { |
| 155 | + layer.clear(); |
| 156 | + currentClusterMarker = null; |
| 157 | + } |
| 158 | + |
100 | 159 | var geojson = { |
101 | 160 | type: 'FeatureCollection', |
102 | 161 | features: [] |
|
124 | 183 | }); |
125 | 184 | } |
126 | 185 | geojson.features.forEach(f => { |
127 | | - // f.symbol = { |
128 | | - // markerType: 'ellipse', |
129 | | - // markerWidth: 15, |
130 | | - // markerHeight: 15, |
131 | | - // markerFill: '#fff', |
132 | | - // markerLineWidth: 1, |
133 | | - // markerFillOpacity: 1, |
134 | | - // markerOpacity: 1, |
135 | | - // } |
| 186 | + f.symbol = { |
| 187 | + markerType: 'ellipse', |
| 188 | + markerWidth: 15, |
| 189 | + markerHeight: 15, |
| 190 | + markerFill: '#fff', |
| 191 | + markerLineWidth: 1, |
| 192 | + markerFillOpacity: 1, |
| 193 | + markerOpacity: 1, |
| 194 | + } |
136 | 195 | }); |
137 | 196 | tileClusterLayer.setData(geojson); |
138 | 197 |
|
|
0 commit comments