@@ -40,6 +40,7 @@ import java.util.*
40
40
import java.util.concurrent.ConcurrentHashMap
41
41
import java.util.concurrent.CountDownLatch
42
42
import java.util.concurrent.TimeUnit
43
+ import java.util.concurrent.atomic.AtomicLong
43
44
44
45
/* *
45
46
* Base class for annotation managers
@@ -49,6 +50,7 @@ abstract class AnnotationManagerImpl<G : Geometry, T : Annotation<G>, S : Annota
49
50
final override val delegateProvider : MapDelegateProvider ,
50
51
private val annotationConfig : AnnotationConfig ?
51
52
) : AnnotationManager<G, T, S, D, U, V, I> {
53
+ internal val id: Long = ID_GENERATOR .incrementAndGet()
52
54
private var mapCameraManagerDelegate: MapCameraManagerDelegate =
53
55
delegateProvider.mapCameraManagerDelegate
54
56
private var mapFeatureQueryDelegate: MapFeatureQueryDelegate =
@@ -70,6 +72,9 @@ abstract class AnnotationManagerImpl<G : Geometry, T : Annotation<G>, S : Annota
70
72
protected abstract val dragLayerId: String
71
73
protected abstract val dragSourceId: String
72
74
75
+ private val associatedLayers = mutableListOf<String >()
76
+ private val associatedSources = mutableListOf<String >()
77
+
73
78
@Suppress(" UNCHECKED_CAST" )
74
79
private var gesturesPlugin: GesturesPlugin = delegateProvider.mapPluginProviderDelegate.getPlugin(
75
80
MAPBOX_GESTURES_PLUGIN_ID
@@ -207,6 +212,7 @@ abstract class AnnotationManagerImpl<G : Geometry, T : Annotation<G>, S : Annota
207
212
source?.let {
208
213
if (! style.styleSourceExists(it.sourceId)) {
209
214
style.addSource(it)
215
+ associatedSources.add(it.sourceId)
210
216
}
211
217
}
212
218
@@ -228,19 +234,22 @@ abstract class AnnotationManagerImpl<G : Geometry, T : Annotation<G>, S : Annota
228
234
if (! layerAdded) {
229
235
style.addPersistentLayer(it)
230
236
}
237
+ associatedLayers.add(it.layerId)
231
238
}
232
239
}
233
240
234
241
dragSource?.let {
235
242
if (! style.styleSourceExists(it.sourceId)) {
236
243
style.addSource(it)
244
+ associatedSources.add(it.sourceId)
237
245
}
238
246
}
239
247
dragLayer?.let {
240
248
if (! style.styleLayerExists(it.layerId)) {
241
249
layer?.layerId?.let { aboveLayerId ->
242
250
// Add drag layer above the annotation layer
243
251
style.addPersistentLayer(it, LayerPosition (aboveLayerId, null , null ))
252
+ associatedLayers.add(it.layerId)
244
253
}
245
254
}
246
255
}
@@ -257,17 +266,19 @@ abstract class AnnotationManagerImpl<G : Geometry, T : Annotation<G>, S : Annota
257
266
val clusterLevelLayer = createClusterLevelLayer(level, it.colorLevels)
258
267
if (! style.styleLayerExists(clusterLevelLayer.layerId)) {
259
268
style.addPersistentLayer(clusterLevelLayer)
269
+ associatedLayers.add(clusterLevelLayer.layerId)
260
270
}
261
271
}
262
272
val clusterTextLayer = createClusterTextLayer()
263
273
if (! style.styleLayerExists(clusterTextLayer.layerId)) {
264
274
style.addPersistentLayer(clusterTextLayer)
275
+ associatedLayers.add(clusterTextLayer.layerId)
265
276
}
266
277
}
267
278
}
268
279
269
280
private fun createClusterLevelLayer (level : Int , colorLevels : List <Pair <Int , Int >>) =
270
- circleLayer(" mapbox-android-cluster-circle-layer-$level " , sourceId) {
281
+ circleLayer(" mapbox-android-cluster-circle-layer-$id -level- $ level" , sourceId) {
271
282
circleColor(colorLevels[level].second)
272
283
annotationConfig?.annotationSourceOptions?.clusterOptions?.let {
273
284
if (it.circleRadiusExpression == null ) {
@@ -289,7 +300,7 @@ abstract class AnnotationManagerImpl<G : Geometry, T : Annotation<G>, S : Annota
289
300
)
290
301
}
291
302
292
- private fun createClusterTextLayer () = symbolLayer(CLUSTER_TEXT_LAYER_ID , sourceId) {
303
+ private fun createClusterTextLayer () = symbolLayer(" mapbox-android-cluster-text-layer- $id " , sourceId) {
293
304
annotationConfig?.annotationSourceOptions?.clusterOptions?.let {
294
305
textField(if (it.textField == null ) DEFAULT_TEXT_FIELD else it.textField as Expression )
295
306
if (it.textSizeExpression == null ) {
@@ -516,24 +527,14 @@ abstract class AnnotationManagerImpl<G : Geometry, T : Annotation<G>, S : Annota
516
527
*/
517
528
override fun onDestroy () {
518
529
delegateProvider.getStyle { style ->
519
- layer?.let {
520
- if (style.styleLayerExists(it.layerId)) {
521
- style.removeStyleLayer(it.layerId)
522
- }
523
- }
524
- source?.let {
525
- if (style.styleSourceExists(it.sourceId)) {
526
- style.removeStyleSource(it.sourceId)
527
- }
528
- }
529
- dragLayer?.let {
530
- if (style.styleLayerExists(it.layerId)) {
531
- style.removeStyleLayer(it.layerId)
530
+ associatedLayers.forEach {
531
+ if (style.styleLayerExists(it)) {
532
+ style.removeStyleLayer(it)
532
533
}
533
534
}
534
- dragSource?. let {
535
- if (style.styleSourceExists(it.sourceId )) {
536
- style.removeStyleSource(it.sourceId )
535
+ associatedSources.forEach {
536
+ if (style.styleSourceExists(it)) {
537
+ style.removeStyleSource(it)
537
538
}
538
539
}
539
540
}
@@ -874,7 +875,7 @@ abstract class AnnotationManagerImpl<G : Geometry, T : Annotation<G>, S : Annota
874
875
/* *
875
876
* Static variables and methods.
876
877
*/
877
- private companion object {
878
+ companion object {
878
879
/* *
879
880
* Tag for log
880
881
*/
@@ -883,7 +884,8 @@ abstract class AnnotationManagerImpl<G : Geometry, T : Annotation<G>, S : Annota
883
884
884
885
/* * At most wait 2 seconds to prevent ANR */
885
886
private const val QUERY_WAIT_TIME = 2L
886
- private const val CLUSTER_TEXT_LAYER_ID = " mapbox-android-cluster-text-layer"
887
887
private val DEFAULT_TEXT_FIELD = get(" point_count" )
888
+ /* * The generator for id */
889
+ var ID_GENERATOR = AtomicLong (0 )
888
890
}
889
891
}
0 commit comments