From 3ee9dcfd6f7f82fe15ea6b272cf816c092c848c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mikl=C3=B3s=20Fazekas?= Date: Sun, 3 Sep 2023 14:32:58 +0200 Subject: [PATCH] fix(android): fix queryRenderedFeaturesInRect in case rect is not conventional format --- .../rctmgl/components/mapview/RCTMGLMapView.kt | 4 ++-- .../components/mapview/RCTMGLMapViewManager.kt | 3 ++- .../rctmgl/utils/extensions/ReadableArray.kt | 15 +++++++++++++++ 3 files changed, 19 insertions(+), 3 deletions(-) diff --git a/android/src/main/java-v10/com/mapbox/rctmgl/components/mapview/RCTMGLMapView.kt b/android/src/main/java-v10/com/mapbox/rctmgl/components/mapview/RCTMGLMapView.kt index dd7aacdd8..e5f63e2a1 100644 --- a/android/src/main/java-v10/com/mapbox/rctmgl/components/mapview/RCTMGLMapView.kt +++ b/android/src/main/java-v10/com/mapbox/rctmgl/components/mapview/RCTMGLMapView.kt @@ -964,9 +964,9 @@ open class RCTMGLMapView(private val mContext: Context, var mManager: RCTMGLMapV } } - fun queryRenderedFeaturesInRect(rect: RectF, filter: Expression?, layerIDs: List?, response: CommandResponse) { + fun queryRenderedFeaturesInRect(rect: RectF?, filter: Expression?, layerIDs: List?, response: CommandResponse) { val size = mMap!!.getMapOptions().size - val screenBox = if (rect.isEmpty()) ScreenBox(ScreenCoordinate(0.0, 0.0), ScreenCoordinate(size?.width!!.toDouble(), size?.height!!.toDouble())) else ScreenBox( + val screenBox = if (rect == null) ScreenBox(ScreenCoordinate(0.0, 0.0), ScreenCoordinate(size?.width!!.toDouble(), size?.height!!.toDouble())) else ScreenBox( ScreenCoordinate(rect.right.toDouble(), rect.bottom.toDouble() ), ScreenCoordinate(rect.left.toDouble(), rect.top.toDouble()), ) diff --git a/android/src/main/java-v10/com/mapbox/rctmgl/components/mapview/RCTMGLMapViewManager.kt b/android/src/main/java-v10/com/mapbox/rctmgl/components/mapview/RCTMGLMapViewManager.kt index 51321fd21..9490f984b 100644 --- a/android/src/main/java-v10/com/mapbox/rctmgl/components/mapview/RCTMGLMapViewManager.kt +++ b/android/src/main/java-v10/com/mapbox/rctmgl/components/mapview/RCTMGLMapViewManager.kt @@ -19,6 +19,7 @@ import com.mapbox.rctmgl.utils.ConvertUtils import com.mapbox.rctmgl.utils.ExpressionParser import com.mapbox.rctmgl.utils.Logger import com.mapbox.rctmgl.utils.extensions.toCoordinate +import com.mapbox.rctmgl.utils.extensions.toRectF import com.mapbox.rctmgl.utils.extensions.toScreenCoordinate import java.lang.Exception import java.util.HashMap @@ -325,7 +326,7 @@ open class RCTMGLMapViewManager(context: ReactApplicationContext) : "queryRenderedFeaturesInRect" -> { val layerIds = ConvertUtils.toStringList(args!!.getArray(3)) mapView.queryRenderedFeaturesInRect( - ConvertUtils.toRectF(args.getArray(1)), + if ((args.getArray(1)?.size() ?: 0) == 0) null else args.getArray(1).toRectF(), ExpressionParser.from(args!!.getArray(2)), if (layerIds.size == 0) null else layerIds, response diff --git a/android/src/main/java-v10/com/mapbox/rctmgl/utils/extensions/ReadableArray.kt b/android/src/main/java-v10/com/mapbox/rctmgl/utils/extensions/ReadableArray.kt index 5226dc150..dd204ddc4 100644 --- a/android/src/main/java-v10/com/mapbox/rctmgl/utils/extensions/ReadableArray.kt +++ b/android/src/main/java-v10/com/mapbox/rctmgl/utils/extensions/ReadableArray.kt @@ -1,9 +1,12 @@ package com.mapbox.rctmgl.utils.extensions +import android.graphics.RectF import com.facebook.react.bridge.ReadableArray import com.mapbox.geojson.Point import com.mapbox.maps.ScreenCoordinate import com.mapbox.rctmgl.utils.Logger +import java.lang.Float.min +import java.lang.Float.max fun ReadableArray.toCoordinate() : Point { if (this.size() != 2) { @@ -21,3 +24,15 @@ fun ReadableArray.toScreenCoordinate() : ScreenCoordinate { } return ScreenCoordinate(getDouble(0), getDouble(1)) } + +fun ReadableArray.toRectF() : RectF? { + if (size() != 4) { + return null; + } + return RectF( + min(getDouble(3).toFloat(), getDouble(1).toFloat()), + min(getDouble(0).toFloat(), getDouble(2).toFloat()), + max(getDouble(3).toFloat(), getDouble(1).toFloat()), + max(getDouble(0).toFloat(), getDouble(2).toFloat()) + ) +}