Skip to content

Commit

Permalink
fix(andorid): fix error when clicking on a point annotation's callout (
Browse files Browse the repository at this point in the history
…#3307)

* fix(andorid): fix error when clicking on a point annotation's callout

* chore(ios): update Podfile
  • Loading branch information
mfazekas authored Jan 9, 2024
1 parent f308f35 commit 90f90d2
Show file tree
Hide file tree
Showing 4 changed files with 682 additions and 647 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import java.util.*
import com.rnmapbox.rnmbx.v11compat.annotation.*;

class RNMBXPointAnnotation(private val mContext: Context, private val mManager: RNMBXPointAnnotationManager) : AbstractMapFeature(mContext), View.OnLayoutChangeListener {

var pointAnnotations: RNMBXPointAnnotationCoordinator? = null
var annotation: PointAnnotation? = null
private set
private var mMap: MapboxMap? = null
Expand Down Expand Up @@ -79,6 +81,7 @@ class RNMBXPointAnnotation(private val mContext: Context, private val mManager:
override fun addToMap(mapView: RNMBXMapView) {
super.addToMap(mapView)
mMap = mapView.getMapboxMap()
pointAnnotations = mapView.pointAnnotations
makeMarker()
if (mChildView != null) {
if (!mChildView!!.isAttachedToWindow) {
Expand All @@ -96,16 +99,13 @@ class RNMBXPointAnnotation(private val mContext: Context, private val mManager:
}

override fun removeFromMap(mapView: RNMBXMapView, reason: RemovalReason): Boolean {
val map = (if (mMapView != null) mMapView else mapView) ?: return true
if (annotation != null) {
map.pointAnnotationManager?.delete(annotation!!)
}
if (mChildView != null) {
map.offscreenAnnotationViewContainer?.removeView(mChildView)
}
if (calloutView != null) {
map.offscreenAnnotationViewContainer?.removeView(calloutView)
}
val map = mMapView ?: mapView

annotation?.let { map.pointAnnotations?.delete(it) }

mChildView?.let { map.offscreenAnnotationViewContainer?.removeView(it) }
calloutView?.let { map.offscreenAnnotationViewContainer?.removeView(it)}

return super.removeFromMap(mapView, reason)
}

Expand Down Expand Up @@ -138,33 +138,36 @@ class RNMBXPointAnnotation(private val mContext: Context, private val mManager:
val latLng: LatLng?
get() = mCoordinate?.let { GeoJSONUtils.toLatLng(it) }
val mapboxID: AnnotationID
get() = if (annotation == null) INVALID_ANNOTATION_ID else annotation!!.id
get() = annotation?.id ?: INVALID_ANNOTATION_ID

val calloutMapboxID: AnnotationID
get() = mCalloutSymbol?.id ?: INVALID_ANNOTATION_ID

fun setCoordinate(point: Point) {
mCoordinate = point
annotation?.let {
it.point = point
mMapView?.pointAnnotationManager?.update(it)
pointAnnotations?.update(it)
}
mCalloutSymbol?.let {
it.point = point
mMapView?.pointAnnotationManager?.update(it)
pointAnnotations?.update(it)
}
}

fun setAnchor(x: Float, y: Float) {
mAnchor = arrayOf(x, y)
if (annotation != null) {
annotation?.let { annotation ->
updateAnchor()
mMapView?.pointAnnotationManager?.update(annotation!!)
pointAnnotations?.update(annotation)
}
}

fun setDraggable(draggable: Boolean) {
mDraggable = draggable
annotation?.let {
it.isDraggable = draggable
mMapView?.pointAnnotationManager?.update(it)
annotation?.let { annotation ->
annotation.isDraggable = draggable
pointAnnotations?.update(annotation)
}
}

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

fun doDeselect() {
mManager.handleEvent(makeEvent(false))
if (mCalloutSymbol != null) {
mMapView?.pointAnnotationManager?.delete(mCalloutSymbol!!)
mCalloutSymbol?.let { mCalloutSymbol ->
pointAnnotations?.delete(mCalloutSymbol)
}
}

Expand All @@ -207,19 +210,18 @@ class RNMBXPointAnnotation(private val mContext: Context, private val mManager:
.withIconSize(1.0)
.withSymbolSortKey(10.0)
}
mMapView?.pointAnnotationManager?.let { annotationManager ->
options?.let {
annotation = annotationManager.create(options)
updateOptions()
}
annotation = null
options?.let {
annotation = pointAnnotations?.create(it)
updateOptions()
}
}

private fun updateOptions() {
if (annotation != null) {
annotation?.let {
updateIconImage()
updateAnchor()
mMapView?.pointAnnotationManager?.update(annotation!!)
pointAnnotations?.update(it)
}
}

Expand Down Expand Up @@ -268,7 +270,7 @@ class RNMBXPointAnnotation(private val mContext: Context, private val mManager:
.withDraggable(false)
}
}
val symbolManager = mMapView?.pointAnnotationManager
val symbolManager = pointAnnotations
if (symbolManager != null && options != null) {
mCalloutSymbol = symbolManager.create(options)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
package com.rnmapbox.rnmbx.components.annotation

import com.mapbox.maps.MapView
import com.mapbox.maps.plugin.annotation.Annotation
import com.mapbox.maps.plugin.annotation.AnnotationConfig
import com.mapbox.maps.plugin.annotation.annotations
import com.mapbox.maps.plugin.annotation.generated.OnPointAnnotationClickListener
import com.mapbox.maps.plugin.annotation.generated.OnPointAnnotationDragListener
import com.mapbox.maps.plugin.annotation.generated.PointAnnotation
import com.mapbox.maps.plugin.annotation.generated.PointAnnotationManager
import com.mapbox.maps.plugin.annotation.generated.PointAnnotationOptions
import com.mapbox.maps.plugin.annotation.generated.createPointAnnotationManager
import com.rnmapbox.rnmbx.components.annotation.RNMBXPointAnnotation
import com.rnmapbox.rnmbx.utils.Logger

class RNMBXPointAnnotationCoordinator(val mapView: MapView) {
val manager: PointAnnotationManager;
var annotationClicked = false
var annotationDragged = false

var selected: RNMBXPointAnnotation? = null

val annotations: MutableMap<String, RNMBXPointAnnotation> = hashMapOf()
val callouts: MutableMap<String, RNMBXPointAnnotation> = hashMapOf()

init {
manager = mapView.annotations.createPointAnnotationManager(AnnotationConfig(layerId = "RNMBX-mapview-annotations"))
manager.addClickListener(OnPointAnnotationClickListener { pointAnnotation ->
onAnnotationClick(pointAnnotation)
false
})
}

fun getAndClearAnnotationClicked(): Boolean {
if (annotationClicked) {
annotationClicked = false
return true
}
return false
}

fun getAndClearAnnotationDragged(): Boolean {
if (annotationDragged) {
annotationDragged = false
return true
}
return false
}

fun lookupForClick(point: PointAnnotation): RNMBXPointAnnotation? {
for (annotation in annotations.values) {
if (point.id == annotation.mapboxID) {
return annotation;
}
if (point.id == annotation.calloutMapboxID) {
return null;
}
}
Logger.e(LOG_TAG, "Failed to find RNMBXPointAnnotation for ${point.id}")
return null;
}

fun onAnnotationClick(pointAnnotation: RNMBXPointAnnotation) {
var oldSelected: RNMBXPointAnnotation? = selected
var newSelected: RNMBXPointAnnotation? = pointAnnotation

annotationClicked = true

if (newSelected == oldSelected) {
newSelected = null
}

manager.addDragListener(object : OnPointAnnotationDragListener {
override fun onAnnotationDragStarted(_annotation: Annotation<*>) {
annotationDragged = true;
var reactAnnotation: RNMBXPointAnnotation? = null
for (key in annotations.keys) {
val annotation = annotations[key]
val curMarkerID = annotation?.mapboxID
if (_annotation.id == curMarkerID) {
reactAnnotation = annotation
}
}
reactAnnotation?.let { it.onDragStart() }
}

override fun onAnnotationDrag(_annotation: Annotation<*>) {
var reactAnnotation: RNMBXPointAnnotation? = null
for (key in annotations.keys) {
val annotation = annotations[key]
val curMarkerID = annotation?.mapboxID
if (_annotation.id == curMarkerID) {
reactAnnotation = annotation
}
}
reactAnnotation?.let { it.onDrag() }
}

override fun onAnnotationDragFinished(_annotation: Annotation<*>) {
annotationDragged = false;
var reactAnnotation: RNMBXPointAnnotation? = null
for (key in annotations.keys) {
val annotation = annotations[key]
val curMarkerID = annotation?.mapboxID
if (_annotation.id == curMarkerID) {
reactAnnotation = annotation
}
}
reactAnnotation?.let { it.onDragEnd() }
}
})

oldSelected?.let { deselectAnnotation(it) }
newSelected?.let { selectAnnotation(it) }

}

fun onAnnotationClick(point: PointAnnotation) {
lookupForClick(point)?.let {
onAnnotationClick(it)
}
}

fun deselectSelectedAnnotation(): Boolean {
selected?.let {
deselectAnnotation(it)
return true
}
return false
}

fun selectAnnotation(annotation: RNMBXPointAnnotation) {
selected = annotation
annotation.doSelect(true)
}

fun deselectAnnotation(annotation: RNMBXPointAnnotation) {
selected = null
annotation.doDeselect()
}

fun remove(annotation: RNMBXPointAnnotation) {
if (annotation == selected) {
selected = null
}
annotations.remove(annotation.iD)
}

fun delete(annotation: PointAnnotation) {
manager.delete(annotation)
}

fun update(annotation: PointAnnotation) {
manager.update(annotation)
}

fun create(options: PointAnnotationOptions): PointAnnotation {
return manager.create(options)
}

fun add(annotation: RNMBXPointAnnotation) {
annotations[annotation.iD!!] = annotation
}

companion object {
const val LOG_TAG = "RNMBXPointAnnotationCoordinator";
}
}
Loading

0 comments on commit 90f90d2

Please sign in to comment.