@@ -40,6 +40,7 @@ import java.util.*
4040import java.util.concurrent.ConcurrentHashMap
4141import java.util.concurrent.CountDownLatch
4242import java.util.concurrent.TimeUnit
43+ import java.util.concurrent.atomic.AtomicLong
4344
4445/* *
4546 * Base class for annotation managers
@@ -49,6 +50,7 @@ abstract class AnnotationManagerImpl<G : Geometry, T : Annotation<G>, S : Annota
4950 final override val delegateProvider : MapDelegateProvider ,
5051 private val annotationConfig : AnnotationConfig ?
5152) : AnnotationManager<G, T, S, D, U, V, I> {
53+ internal val id: Long = ID_GENERATOR .incrementAndGet()
5254 private var mapCameraManagerDelegate: MapCameraManagerDelegate =
5355 delegateProvider.mapCameraManagerDelegate
5456 private var mapFeatureQueryDelegate: MapFeatureQueryDelegate =
@@ -70,6 +72,9 @@ abstract class AnnotationManagerImpl<G : Geometry, T : Annotation<G>, S : Annota
7072 protected abstract val dragLayerId: String
7173 protected abstract val dragSourceId: String
7274
75+ private val associatedLayers = mutableListOf<String >()
76+ private val associatedSources = mutableListOf<String >()
77+
7378 @Suppress(" UNCHECKED_CAST" )
7479 private var gesturesPlugin: GesturesPlugin = delegateProvider.mapPluginProviderDelegate.getPlugin(
7580 MAPBOX_GESTURES_PLUGIN_ID
@@ -207,6 +212,7 @@ abstract class AnnotationManagerImpl<G : Geometry, T : Annotation<G>, S : Annota
207212 source?.let {
208213 if (! style.styleSourceExists(it.sourceId)) {
209214 style.addSource(it)
215+ associatedSources.add(it.sourceId)
210216 }
211217 }
212218
@@ -228,19 +234,22 @@ abstract class AnnotationManagerImpl<G : Geometry, T : Annotation<G>, S : Annota
228234 if (! layerAdded) {
229235 style.addPersistentLayer(it)
230236 }
237+ associatedLayers.add(it.layerId)
231238 }
232239 }
233240
234241 dragSource?.let {
235242 if (! style.styleSourceExists(it.sourceId)) {
236243 style.addSource(it)
244+ associatedSources.add(it.sourceId)
237245 }
238246 }
239247 dragLayer?.let {
240248 if (! style.styleLayerExists(it.layerId)) {
241249 layer?.layerId?.let { aboveLayerId ->
242250 // Add drag layer above the annotation layer
243251 style.addPersistentLayer(it, LayerPosition (aboveLayerId, null , null ))
252+ associatedLayers.add(it.layerId)
244253 }
245254 }
246255 }
@@ -257,17 +266,19 @@ abstract class AnnotationManagerImpl<G : Geometry, T : Annotation<G>, S : Annota
257266 val clusterLevelLayer = createClusterLevelLayer(level, it.colorLevels)
258267 if (! style.styleLayerExists(clusterLevelLayer.layerId)) {
259268 style.addPersistentLayer(clusterLevelLayer)
269+ associatedLayers.add(clusterLevelLayer.layerId)
260270 }
261271 }
262272 val clusterTextLayer = createClusterTextLayer()
263273 if (! style.styleLayerExists(clusterTextLayer.layerId)) {
264274 style.addPersistentLayer(clusterTextLayer)
275+ associatedLayers.add(clusterTextLayer.layerId)
265276 }
266277 }
267278 }
268279
269280 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) {
271282 circleColor(colorLevels[level].second)
272283 annotationConfig?.annotationSourceOptions?.clusterOptions?.let {
273284 if (it.circleRadiusExpression == null ) {
@@ -289,7 +300,7 @@ abstract class AnnotationManagerImpl<G : Geometry, T : Annotation<G>, S : Annota
289300 )
290301 }
291302
292- private fun createClusterTextLayer () = symbolLayer(CLUSTER_TEXT_LAYER_ID , sourceId) {
303+ private fun createClusterTextLayer () = symbolLayer(" mapbox-android-cluster-text-layer- $id " , sourceId) {
293304 annotationConfig?.annotationSourceOptions?.clusterOptions?.let {
294305 textField(if (it.textField == null ) DEFAULT_TEXT_FIELD else it.textField as Expression )
295306 if (it.textSizeExpression == null ) {
@@ -516,24 +527,14 @@ abstract class AnnotationManagerImpl<G : Geometry, T : Annotation<G>, S : Annota
516527 */
517528 override fun onDestroy () {
518529 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)
532533 }
533534 }
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)
537538 }
538539 }
539540 }
@@ -874,7 +875,7 @@ abstract class AnnotationManagerImpl<G : Geometry, T : Annotation<G>, S : Annota
874875 /* *
875876 * Static variables and methods.
876877 */
877- private companion object {
878+ companion object {
878879 /* *
879880 * Tag for log
880881 */
@@ -883,7 +884,8 @@ abstract class AnnotationManagerImpl<G : Geometry, T : Annotation<G>, S : Annota
883884
884885 /* * At most wait 2 seconds to prevent ANR */
885886 private const val QUERY_WAIT_TIME = 2L
886- private const val CLUSTER_TEXT_LAYER_ID = " mapbox-android-cluster-text-layer"
887887 private val DEFAULT_TEXT_FIELD = get(" point_count" )
888+ /* * The generator for id */
889+ var ID_GENERATOR = AtomicLong (0 )
888890 }
889891}
0 commit comments