Skip to content

Commit

Permalink
fix: add deselectAnnotationOnTap to deselect and hide pointannotation…
Browse files Browse the repository at this point in the history
… callout on press, refactor point annotation manager or android
  • Loading branch information
mfazekas committed Dec 9, 2023
1 parent 50ee9f9 commit 28c9c0e
Show file tree
Hide file tree
Showing 15 changed files with 273 additions and 160 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,9 @@ import com.rnmapbox.rnmbx.components.AbstractMapFeature
import com.mapbox.maps.plugin.annotation.generated.PointAnnotation
import com.mapbox.maps.MapboxMap
import com.rnmapbox.rnmbx.components.mapview.RNMBXMapView
import com.rnmapbox.rnmbx.components.annotation.RNMBXCallout
import com.rnmapbox.rnmbx.utils.GeoJSONUtils
import com.rnmapbox.rnmbx.events.constants.EventTypes
import com.mapbox.maps.plugin.annotation.generated.PointAnnotationOptions
import com.mapbox.maps.plugin.annotation.generated.PointAnnotationManager
import com.rnmapbox.rnmbx.components.annotation.RNMBXPointAnnotation
import com.mapbox.maps.extension.style.layers.properties.generated.IconAnchor
import com.rnmapbox.rnmbx.events.PointAnnotationClickEvent
import android.graphics.PointF
Expand All @@ -28,7 +25,7 @@ import java.util.*
import com.rnmapbox.rnmbx.v11compat.annotation.*;

class RNMBXPointAnnotation(private val mContext: Context, private val mManager: RNMBXPointAnnotationManager) : AbstractMapFeature(mContext), View.OnLayoutChangeListener {
var marker: PointAnnotation? = null
var annotation: PointAnnotation? = null
private set
private var mMap: MapboxMap? = null
private val mHasChildren = false
Expand Down Expand Up @@ -100,8 +97,8 @@ 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 (marker != null) {
map.pointAnnotationManager?.delete(marker!!)
if (annotation != null) {
map.pointAnnotationManager?.delete(annotation!!)
}
if (mChildView != null) {
map.offscreenAnnotationViewContainer?.removeView(mChildView)
Expand Down Expand Up @@ -141,33 +138,33 @@ class RNMBXPointAnnotation(private val mContext: Context, private val mManager:
val latLng: LatLng?
get() = mCoordinate?.let { GeoJSONUtils.toLatLng(it) }
val mapboxID: AnnotationID
get() = if (marker == null) INVALID_ANNOTATION_ID else marker!!.id
get() = if (annotation == null) INVALID_ANNOTATION_ID else annotation!!.id

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

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

fun setDraggable(draggable: Boolean) {
mDraggable = draggable
if (marker != null) {
marker!!.isDraggable = draggable
mMapView?.pointAnnotationManager?.update(marker!!)
annotation?.let {
it.isDraggable = draggable
mMapView?.pointAnnotationManager?.update(it)
}
}

Expand All @@ -188,17 +185,17 @@ class RNMBXPointAnnotation(private val mContext: Context, private val mManager:
}

fun onDragStart() {
mCoordinate = marker!!.point
mCoordinate = annotation!!.point
mManager.handleEvent(makeDragEvent(EventTypes.ANNOTATION_DRAG_START))
}

fun onDrag() {
mCoordinate = marker!!.point
mCoordinate = annotation!!.point
mManager.handleEvent(makeDragEvent(EventTypes.ANNOTATION_DRAG))
}

fun onDragEnd() {
mCoordinate = marker!!.point
mCoordinate = annotation!!.point
mManager.handleEvent(makeDragEvent(EventTypes.ANNOTATION_DRAG_END))
}

Expand All @@ -210,41 +207,42 @@ class RNMBXPointAnnotation(private val mContext: Context, private val mManager:
.withIconSize(1.0)
.withSymbolSortKey(10.0)
}
val symbolManager = mMapView?.pointAnnotationManager
if (symbolManager != null && options != null) {
marker = symbolManager.create(options)
updateOptions()
mMapView?.pointAnnotationManager?.let { annotationManager ->
options?.let {
annotation = annotationManager.create(options)
updateOptions()
}
}
}

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

private fun updateIconImage() {
if (mChildView != null) {
if (mChildBitmapId != null) {
marker?.iconImage = mChildBitmapId
annotation?.iconImage = mChildBitmapId
}
} else {
marker?.iconImage = MARKER_IMAGE_ID
marker?.iconAnchor = IconAnchor.BOTTOM
annotation?.iconImage = MARKER_IMAGE_ID
annotation?.iconAnchor = IconAnchor.BOTTOM
}
}

private fun updateAnchor() {
if (mAnchor != null && mChildView != null && mChildBitmap != null && marker != null) {
if (mAnchor != null && mChildView != null && mChildBitmap != null && annotation != null) {
var w = mChildBitmap!!.width
var h = mChildBitmap!!.height
val scale = resources.displayMetrics.density
w = (w / scale).toInt()
h = (h / scale).toInt()
marker?.iconAnchor = IconAnchor.TOP_LEFT
marker?.iconOffset = Arrays.asList(w.toDouble() * mAnchor!![0] * -1.0, h.toDouble() * mAnchor!![1] * -1.0)
annotation?.iconAnchor = IconAnchor.TOP_LEFT
annotation?.iconOffset = Arrays.asList(w.toDouble() * mAnchor!![0] * -1.0, h.toDouble() * mAnchor!![1] * -1.0)
}
}

Expand Down
Loading

0 comments on commit 28c9c0e

Please sign in to comment.