Skip to content

Commit 90f90d2

Browse files
authored
fix(andorid): fix error when clicking on a point annotation's callout (#3307)
* fix(andorid): fix error when clicking on a point annotation's callout * chore(ios): update Podfile
1 parent f308f35 commit 90f90d2

File tree

4 files changed

+682
-647
lines changed

4 files changed

+682
-647
lines changed

android/src/main/java/com/rnmapbox/rnmbx/components/annotation/RNMBXPointAnnotation.kt

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ import java.util.*
2525
import com.rnmapbox.rnmbx.v11compat.annotation.*;
2626

2727
class RNMBXPointAnnotation(private val mContext: Context, private val mManager: RNMBXPointAnnotationManager) : AbstractMapFeature(mContext), View.OnLayoutChangeListener {
28+
29+
var pointAnnotations: RNMBXPointAnnotationCoordinator? = null
2830
var annotation: PointAnnotation? = null
2931
private set
3032
private var mMap: MapboxMap? = null
@@ -79,6 +81,7 @@ class RNMBXPointAnnotation(private val mContext: Context, private val mManager:
7981
override fun addToMap(mapView: RNMBXMapView) {
8082
super.addToMap(mapView)
8183
mMap = mapView.getMapboxMap()
84+
pointAnnotations = mapView.pointAnnotations
8285
makeMarker()
8386
if (mChildView != null) {
8487
if (!mChildView!!.isAttachedToWindow) {
@@ -96,16 +99,13 @@ class RNMBXPointAnnotation(private val mContext: Context, private val mManager:
9699
}
97100

98101
override fun removeFromMap(mapView: RNMBXMapView, reason: RemovalReason): Boolean {
99-
val map = (if (mMapView != null) mMapView else mapView) ?: return true
100-
if (annotation != null) {
101-
map.pointAnnotationManager?.delete(annotation!!)
102-
}
103-
if (mChildView != null) {
104-
map.offscreenAnnotationViewContainer?.removeView(mChildView)
105-
}
106-
if (calloutView != null) {
107-
map.offscreenAnnotationViewContainer?.removeView(calloutView)
108-
}
102+
val map = mMapView ?: mapView
103+
104+
annotation?.let { map.pointAnnotations?.delete(it) }
105+
106+
mChildView?.let { map.offscreenAnnotationViewContainer?.removeView(it) }
107+
calloutView?.let { map.offscreenAnnotationViewContainer?.removeView(it)}
108+
109109
return super.removeFromMap(mapView, reason)
110110
}
111111

@@ -138,33 +138,36 @@ class RNMBXPointAnnotation(private val mContext: Context, private val mManager:
138138
val latLng: LatLng?
139139
get() = mCoordinate?.let { GeoJSONUtils.toLatLng(it) }
140140
val mapboxID: AnnotationID
141-
get() = if (annotation == null) INVALID_ANNOTATION_ID else annotation!!.id
141+
get() = annotation?.id ?: INVALID_ANNOTATION_ID
142+
143+
val calloutMapboxID: AnnotationID
144+
get() = mCalloutSymbol?.id ?: INVALID_ANNOTATION_ID
142145

143146
fun setCoordinate(point: Point) {
144147
mCoordinate = point
145148
annotation?.let {
146149
it.point = point
147-
mMapView?.pointAnnotationManager?.update(it)
150+
pointAnnotations?.update(it)
148151
}
149152
mCalloutSymbol?.let {
150153
it.point = point
151-
mMapView?.pointAnnotationManager?.update(it)
154+
pointAnnotations?.update(it)
152155
}
153156
}
154157

155158
fun setAnchor(x: Float, y: Float) {
156159
mAnchor = arrayOf(x, y)
157-
if (annotation != null) {
160+
annotation?.let { annotation ->
158161
updateAnchor()
159-
mMapView?.pointAnnotationManager?.update(annotation!!)
162+
pointAnnotations?.update(annotation)
160163
}
161164
}
162165

163166
fun setDraggable(draggable: Boolean) {
164167
mDraggable = draggable
165-
annotation?.let {
166-
it.isDraggable = draggable
167-
mMapView?.pointAnnotationManager?.update(it)
168+
annotation?.let { annotation ->
169+
annotation.isDraggable = draggable
170+
pointAnnotations?.update(annotation)
168171
}
169172
}
170173

@@ -179,8 +182,8 @@ class RNMBXPointAnnotation(private val mContext: Context, private val mManager:
179182

180183
fun doDeselect() {
181184
mManager.handleEvent(makeEvent(false))
182-
if (mCalloutSymbol != null) {
183-
mMapView?.pointAnnotationManager?.delete(mCalloutSymbol!!)
185+
mCalloutSymbol?.let { mCalloutSymbol ->
186+
pointAnnotations?.delete(mCalloutSymbol)
184187
}
185188
}
186189

@@ -207,19 +210,18 @@ class RNMBXPointAnnotation(private val mContext: Context, private val mManager:
207210
.withIconSize(1.0)
208211
.withSymbolSortKey(10.0)
209212
}
210-
mMapView?.pointAnnotationManager?.let { annotationManager ->
211-
options?.let {
212-
annotation = annotationManager.create(options)
213-
updateOptions()
214-
}
213+
annotation = null
214+
options?.let {
215+
annotation = pointAnnotations?.create(it)
216+
updateOptions()
215217
}
216218
}
217219

218220
private fun updateOptions() {
219-
if (annotation != null) {
221+
annotation?.let {
220222
updateIconImage()
221223
updateAnchor()
222-
mMapView?.pointAnnotationManager?.update(annotation!!)
224+
pointAnnotations?.update(it)
223225
}
224226
}
225227

@@ -268,7 +270,7 @@ class RNMBXPointAnnotation(private val mContext: Context, private val mManager:
268270
.withDraggable(false)
269271
}
270272
}
271-
val symbolManager = mMapView?.pointAnnotationManager
273+
val symbolManager = pointAnnotations
272274
if (symbolManager != null && options != null) {
273275
mCalloutSymbol = symbolManager.create(options)
274276
}
Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
package com.rnmapbox.rnmbx.components.annotation
2+
3+
import com.mapbox.maps.MapView
4+
import com.mapbox.maps.plugin.annotation.Annotation
5+
import com.mapbox.maps.plugin.annotation.AnnotationConfig
6+
import com.mapbox.maps.plugin.annotation.annotations
7+
import com.mapbox.maps.plugin.annotation.generated.OnPointAnnotationClickListener
8+
import com.mapbox.maps.plugin.annotation.generated.OnPointAnnotationDragListener
9+
import com.mapbox.maps.plugin.annotation.generated.PointAnnotation
10+
import com.mapbox.maps.plugin.annotation.generated.PointAnnotationManager
11+
import com.mapbox.maps.plugin.annotation.generated.PointAnnotationOptions
12+
import com.mapbox.maps.plugin.annotation.generated.createPointAnnotationManager
13+
import com.rnmapbox.rnmbx.components.annotation.RNMBXPointAnnotation
14+
import com.rnmapbox.rnmbx.utils.Logger
15+
16+
class RNMBXPointAnnotationCoordinator(val mapView: MapView) {
17+
val manager: PointAnnotationManager;
18+
var annotationClicked = false
19+
var annotationDragged = false
20+
21+
var selected: RNMBXPointAnnotation? = null
22+
23+
val annotations: MutableMap<String, RNMBXPointAnnotation> = hashMapOf()
24+
val callouts: MutableMap<String, RNMBXPointAnnotation> = hashMapOf()
25+
26+
init {
27+
manager = mapView.annotations.createPointAnnotationManager(AnnotationConfig(layerId = "RNMBX-mapview-annotations"))
28+
manager.addClickListener(OnPointAnnotationClickListener { pointAnnotation ->
29+
onAnnotationClick(pointAnnotation)
30+
false
31+
})
32+
}
33+
34+
fun getAndClearAnnotationClicked(): Boolean {
35+
if (annotationClicked) {
36+
annotationClicked = false
37+
return true
38+
}
39+
return false
40+
}
41+
42+
fun getAndClearAnnotationDragged(): Boolean {
43+
if (annotationDragged) {
44+
annotationDragged = false
45+
return true
46+
}
47+
return false
48+
}
49+
50+
fun lookupForClick(point: PointAnnotation): RNMBXPointAnnotation? {
51+
for (annotation in annotations.values) {
52+
if (point.id == annotation.mapboxID) {
53+
return annotation;
54+
}
55+
if (point.id == annotation.calloutMapboxID) {
56+
return null;
57+
}
58+
}
59+
Logger.e(LOG_TAG, "Failed to find RNMBXPointAnnotation for ${point.id}")
60+
return null;
61+
}
62+
63+
fun onAnnotationClick(pointAnnotation: RNMBXPointAnnotation) {
64+
var oldSelected: RNMBXPointAnnotation? = selected
65+
var newSelected: RNMBXPointAnnotation? = pointAnnotation
66+
67+
annotationClicked = true
68+
69+
if (newSelected == oldSelected) {
70+
newSelected = null
71+
}
72+
73+
manager.addDragListener(object : OnPointAnnotationDragListener {
74+
override fun onAnnotationDragStarted(_annotation: Annotation<*>) {
75+
annotationDragged = true;
76+
var reactAnnotation: RNMBXPointAnnotation? = null
77+
for (key in annotations.keys) {
78+
val annotation = annotations[key]
79+
val curMarkerID = annotation?.mapboxID
80+
if (_annotation.id == curMarkerID) {
81+
reactAnnotation = annotation
82+
}
83+
}
84+
reactAnnotation?.let { it.onDragStart() }
85+
}
86+
87+
override fun onAnnotationDrag(_annotation: Annotation<*>) {
88+
var reactAnnotation: RNMBXPointAnnotation? = null
89+
for (key in annotations.keys) {
90+
val annotation = annotations[key]
91+
val curMarkerID = annotation?.mapboxID
92+
if (_annotation.id == curMarkerID) {
93+
reactAnnotation = annotation
94+
}
95+
}
96+
reactAnnotation?.let { it.onDrag() }
97+
}
98+
99+
override fun onAnnotationDragFinished(_annotation: Annotation<*>) {
100+
annotationDragged = false;
101+
var reactAnnotation: RNMBXPointAnnotation? = null
102+
for (key in annotations.keys) {
103+
val annotation = annotations[key]
104+
val curMarkerID = annotation?.mapboxID
105+
if (_annotation.id == curMarkerID) {
106+
reactAnnotation = annotation
107+
}
108+
}
109+
reactAnnotation?.let { it.onDragEnd() }
110+
}
111+
})
112+
113+
oldSelected?.let { deselectAnnotation(it) }
114+
newSelected?.let { selectAnnotation(it) }
115+
116+
}
117+
118+
fun onAnnotationClick(point: PointAnnotation) {
119+
lookupForClick(point)?.let {
120+
onAnnotationClick(it)
121+
}
122+
}
123+
124+
fun deselectSelectedAnnotation(): Boolean {
125+
selected?.let {
126+
deselectAnnotation(it)
127+
return true
128+
}
129+
return false
130+
}
131+
132+
fun selectAnnotation(annotation: RNMBXPointAnnotation) {
133+
selected = annotation
134+
annotation.doSelect(true)
135+
}
136+
137+
fun deselectAnnotation(annotation: RNMBXPointAnnotation) {
138+
selected = null
139+
annotation.doDeselect()
140+
}
141+
142+
fun remove(annotation: RNMBXPointAnnotation) {
143+
if (annotation == selected) {
144+
selected = null
145+
}
146+
annotations.remove(annotation.iD)
147+
}
148+
149+
fun delete(annotation: PointAnnotation) {
150+
manager.delete(annotation)
151+
}
152+
153+
fun update(annotation: PointAnnotation) {
154+
manager.update(annotation)
155+
}
156+
157+
fun create(options: PointAnnotationOptions): PointAnnotation {
158+
return manager.create(options)
159+
}
160+
161+
fun add(annotation: RNMBXPointAnnotation) {
162+
annotations[annotation.iD!!] = annotation
163+
}
164+
165+
companion object {
166+
const val LOG_TAG = "RNMBXPointAnnotationCoordinator";
167+
}
168+
}

0 commit comments

Comments
 (0)