From 56eaf3e0c5c11e4f6cf1773f8dbe69c0621fd6bc Mon Sep 17 00:00:00 2001 From: Hitesh Modi Date: Wed, 10 Jul 2024 15:32:43 +0530 Subject: [PATCH 01/11] feat: getStyles, setLayerProperties, setLayerProperty --- ios/RNMBX/RNMBXMapViewManager.swift | 97 +++++++++++++++++++++++++++++ ios/RNMBX/RNMBXMapViewModule.mm | 30 +++++++++ package.json | 9 ++- src/components/MapView.tsx | 54 ++++++++++++++++ 4 files changed, 185 insertions(+), 5 deletions(-) diff --git a/ios/RNMBX/RNMBXMapViewManager.swift b/ios/RNMBX/RNMBXMapViewManager.swift index 53834dfdb..d6e0f7697 100644 --- a/ios/RNMBX/RNMBXMapViewManager.swift +++ b/ios/RNMBX/RNMBXMapViewManager.swift @@ -224,4 +224,101 @@ extension RNMBXMapViewManager { } } } + + @objc public static func queryRenderedLayersInRect( + _ map: RNMBXMapView, + withBBox bbox: [NSNumber], + withFilter filter: [Any]?, + withLayerIDs layerIDs: [String]?, + resolver: @escaping RCTPromiseResolveBlock, + rejecter: @escaping RCTPromiseRejectBlock) -> Void { + let top = bbox.isEmpty ? 0.0 : CGFloat(bbox[0].floatValue) + let right = bbox.isEmpty ? 0.0 : CGFloat(bbox[1].floatValue) + let bottom = bbox.isEmpty ? 0.0 : CGFloat(bbox[2].floatValue) + let left = bbox.isEmpty ? 0.0 : CGFloat(bbox[3].floatValue) + let rect = bbox.isEmpty ? CGRect(x: 0.0, y: 0.0, width: map.bounds.size.width, height: map.bounds.size.height) : CGRect(x: [left,right].min()!, y: [top,bottom].min()!, width: abs(right-left), height: abs(bottom-top)) + logged("queryRenderedLayersInRect.option", rejecter: rejecter) { + let options = try RenderedQueryOptions(layerIds: layerIDs?.isEmpty ?? true ? nil : layerIDs, filter: filter?.asExpression()) + map.mapboxMap.queryRenderedFeatures(with: rect, options: options) { result in + switch result { + case .success(let features): + resolver(features.compactMap { queriedFeature in + logged("queryRenderedLayersInRect.queriedfeature.map") { + return ["id":queriedFeature.sourceLayer, "feature": try queriedFeature.feature.toJSON()] } + }) + case .failure(let error): + rejecter("queryRenderedLayersInRect","failed to query features", error) + } + } + } + } + @objc public static func getStyles( + _ map: RNMBXMapView, + resolver: @escaping RCTPromiseResolveBlock, + rejecter: @escaping RCTPromiseRejectBlock) -> Void { + logged("getStyles.option", rejecter: rejecter) { + resolver(map.mapboxMap.style.JSON) + } + } + + @objc public static func setLayerProperties( + _ map: RNMBXMapView, + withLayerID layerID: String, + withProperties properties: [String: Any], + resolver: @escaping RCTPromiseResolveBlock, + rejecter: @escaping RCTPromiseRejectBlock) -> Void { + do{ + try map.mapboxMap.style.setLayerProperties(for: layerID, properties: properties) + resolver(nil) + } catch let error { + rejecter("failed_to_set_properties", "An error occurred while setting layer properties: \(error.localizedDescription)", error) + } + } + + @objc public static func setLayerProperty( + _ map: RNMBXMapView, + withLayerID layerID: String, + withProperty property: String, + withValue value: Any, + resolver: @escaping RCTPromiseResolveBlock, + rejecter: @escaping RCTPromiseRejectBlock) -> Void { + do{ + try map.mapboxMap.style.setLayerProperty(for: layerID, property: property, value: value) + resolver(nil) + } catch let error { + rejecter("failed_to_set_property", "An error occurred while setting layer property: \(error.localizedDescription)", error) + } + } + + @objc public static func setLayerFilter( + _ map: RNMBXMapView, + withLayerID layerID: String, + withFilter filter: [Any], + resolver: @escaping RCTPromiseResolveBlock, + rejecter: @escaping RCTPromiseRejectBlock) -> Void { + + do { + var layer:Layer = try map.mapboxMap.style.layer(withId: layerID) + if let specificLayer = layer as? FillLayer { + try map.mapboxMap.style.updateLayer(withId: layerID, type: FillLayer.self) { (updatedLayer: inout FillLayer) in + updatedLayer.filter = try filter.asExpression() + } + } else if let specificLayer = layer as? LineLayer { + try map.mapboxMap.style.updateLayer(withId: layerID, type: LineLayer.self) { (updatedLayer: inout LineLayer) in + updatedLayer.filter = try filter.asExpression() + } + } else if let specificLayer = layer as? CircleLayer { + try map.mapboxMap.style.updateLayer(withId: layerID, type: CircleLayer.self) { (updatedLayer: inout CircleLayer) in + updatedLayer.filter = try filter.asExpression() + } + } else { + // Handle other layer types as needed + throw NSError(domain: "", code: 0, userInfo: [NSLocalizedDescriptionKey: "Unsupported layer type"]) + } + + resolver(nil) + } catch let error { + rejecter("failed_to_set_filter", "Set Layer Filter: \(error.localizedDescription)", error) + } + } } diff --git a/ios/RNMBX/RNMBXMapViewModule.mm b/ios/RNMBX/RNMBXMapViewModule.mm index 21cb8f1ad..cc374ef6f 100644 --- a/ios/RNMBX/RNMBXMapViewModule.mm +++ b/ios/RNMBX/RNMBXMapViewModule.mm @@ -139,6 +139,36 @@ - (void)withMapView:(nonnull NSNumber*)viewRef block:(void (^)(RNMBXMapView *))b } reject:reject methodName:@"querySourceFeatures"]; } +RCT_EXPORT_METHOD(queryRenderedLayersInRect:(nonnull NSNumber*)viewRef withBBox:(NSArray *)withBBox withFilter:(NSArray *)withFilter withLayerIDs:(NSArray *)withLayerIDs resolve:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject) { + [self withMapView:viewRef block:^(RNMBXMapView *view) { + [RNMBXMapViewManager queryRenderedLayersInRect:view withBBox:withBBox withFilter:withFilter withLayerIDs:withLayerIDs resolver:resolve rejecter:reject]; + } reject:reject methodName:@"queryRenderedLayersInRect"]; +} + +RCT_EXPORT_METHOD(getStyles:(nonnull NSNumber*)viewRef resolve:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject) { + [self withMapView:viewRef block:^(RNMBXMapView *view) { + [RNMBXMapViewManager getStyles:view resolver:resolve rejecter:reject]; + } reject:reject methodName:@"getStyles"]; +} + +RCT_EXPORT_METHOD(setLayerProperties: (nonnull NSNumber *)viewRef withLayerID:(NSString *)withLayerID withProperties: (NSDictionary *)withProperties resolve:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject){ + [self withMapView:viewRef block:^(RNMBXMapView *view) { + [RNMBXMapViewManager setLayerProperties:view withLayerID:withLayerID withProperties:withProperties resolver:resolve rejecter:reject]; + } reject:reject methodName:@"setLayerProperties"]; +} + +RCT_EXPORT_METHOD(setLayerProperty : (nonnull NSNumber *)viewRef withLayerID: (nonnull NSString *)withLayerID withProperty: (nonnull NSString *)withProperty withValue: (id)withValue resolve: (RCTPromiseResolveBlock)resolve reject: (RCTPromiseRejectBlock)reject){ + [self withMapView:viewRef block:^(RNMBXMapView *view) { + [RNMBXMapViewManager setLayerProperty:view withLayerID:withLayerID withProperty:withProperty withValue:withValue resolver:resolve rejecter:reject]; + } reject:reject methodName:@"setLayerProperty"]; +} + +RCT_EXPORT_METHOD(setLayerFilter: (nonnull NSNumber *)viewRef withLayerID: (NSString *)withLayerID withFilter: (NSArray *)withFilter resolve: (RCTPromiseResolveBlock)resolve rejecte: (RCTPromiseRejectBlock)reject){ + [self withMapView:viewRef block:^(RNMBXMapView *view) { + [RNMBXMapViewManager setLayerFilter:view withLayerID:withLayerID withFilter:withFilter resolver:resolve rejecter:reject]; + } reject:reject methodName:@"setLayerFilter"]; +} + // Thanks to this guard, we won't compile this code when we build for the old architecture. #ifdef RCT_NEW_ARCH_ENABLED - (std::shared_ptr)getTurboModule: diff --git a/package.json b/package.json index 996deed17..a6beb5993 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { - "name": "@rnmapbox/maps", - "description": "A Mapbox react native module for creating custom maps", - "version": "10.1.27", + "name": "rnmapbox-ditch", + "description": "A customized Mapbox React Native module designed to create tailored maps, specifically enhanced for the unique requirements of Ditch Navigation.", + "version": "1.0.6", "publishConfig": { "access": "public" }, @@ -13,10 +13,9 @@ "mapbox", "react-native" ], - "license": "MIT", "repository": { "type": "git", - "url": "https://github.com/rnmapbox/maps" + "url": "https://github.com/extentdevices/ditch-rnmapbox" }, "main": "lib/commonjs/index", "module": "lib/module/index", diff --git a/src/components/MapView.tsx b/src/components/MapView.tsx index 13042336f..4986eb901 100644 --- a/src/components/MapView.tsx +++ b/src/components/MapView.tsx @@ -470,6 +470,11 @@ type CallbablePropKeysWithoutOn = CallbablePropKeys extends `on${infer C}` type Debounced = F & { clear(): void; flush(): void }; +type LayerProperties = { + // eslint-disable-next-line @typescript-eslint/no-explicit-any + [key: string]: any; +}; + /** * MapView backed by Mapbox Native GL */ @@ -775,6 +780,55 @@ class MapView extends NativeBridgeComponent( } } + async queryRenderedLayersInRect( + bbox: BBox | [], + filter: FilterExpression | [] = [], + layerIDs: string[] | null = null, + ) { + if ( + bbox != null && + (bbox.length === 4 || (RNMBXModule.MapboxV10 && bbox.length === 0)) + ) { + const res = await this._runNative('queryRenderedLayersInRect', [ + bbox, + getFilter(filter), + layerIDs, + ]); + + if (isAndroid()) { + return JSON.parse(res as unknown as string); + } + + return res; + } else { + throw new Error( + 'Must pass in a valid bounding box: [top, right, bottom, left]. An empty array [] is also acceptable in v10.', + ); + } + } + async getStyles() { + return JSON.parse(await this._runNative('getStyles')); + } + + async setLayerProperties(layerID: string, properities: LayerProperties) { + return await this._runNative('setLayerProperties', [layerID, properities]); + } + + // eslint-disable-next-line @typescript-eslint/no-explicit-any + async setLayerProperty(layerID: string, property: string, value: any) { + return await this._runNative('setLayerProperty', [ + layerID, + property, + value, + ]); + } + + async setLayerFilter(layerID: string, filter: string[]) { + return await this._runNative('setLayerFilter', [ + layerID, + getFilter(filter), + ]); + } /** * Returns an array of GeoJSON Feature objects representing features within the specified vector tile or GeoJSON source that satisfy the query parameters. * From 7685d3a04ea54e0d99b7199f1ba07c9d9b76ac81 Mon Sep 17 00:00:00 2001 From: Hitesh Modi Date: Fri, 19 Jul 2024 13:23:36 +0530 Subject: [PATCH 02/11] feat: add functionalities to android --- .../components/mapview/NativeMapViewModule.kt | 65 +++++++++++++++++ .../rnmbx/components/mapview/RNMBXMapView.kt | 72 +++++++++++++++++++ ios/RNMBX/RNMBXMapViewManager.swift | 58 +++++++-------- ios/RNMBX/RNMBXMapViewModule.mm | 6 -- package.json | 2 +- 5 files changed, 167 insertions(+), 36 deletions(-) diff --git a/android/src/main/java/com/rnmapbox/rnmbx/components/mapview/NativeMapViewModule.kt b/android/src/main/java/com/rnmapbox/rnmbx/components/mapview/NativeMapViewModule.kt index ef529ef14..b1ef8df0c 100644 --- a/android/src/main/java/com/rnmapbox/rnmbx/components/mapview/NativeMapViewModule.kt +++ b/android/src/main/java/com/rnmapbox/rnmbx/components/mapview/NativeMapViewModule.kt @@ -12,6 +12,7 @@ import com.rnmapbox.rnmbx.utils.ViewRefTag import com.rnmapbox.rnmbx.utils.ViewTagResolver import com.rnmapbox.rnmbx.utils.extensions.toCoordinate import com.rnmapbox.rnmbx.utils.extensions.toScreenCoordinate +import com.mapbox.bindgen.Value class NativeMapViewModule(context: ReactApplicationContext, val viewTagResolver: ViewTagResolver) : NativeMapViewModuleSpec(context) { private fun withMapViewOnUIThread( @@ -184,6 +185,70 @@ class NativeMapViewModule(context: ReactApplicationContext, val viewTagResolver: ) } } + fun queryRenderedLayersInRect( + viewRef: ViewRefTag?, + withBBox: ReadableArray, + withFilter: ReadableArray, + withLayerIDs: ReadableArray?, + promise: Promise + ) { + withMapViewOnUIThread(viewRef, promise) { + val layerIds = ConvertUtils.toStringList(withLayerIDs) + + it.queryRenderedLayersInRect( + ConvertUtils.toRectF(withBBox), + ExpressionParser.from(withFilter), + if (layerIds.size == 0) null else layerIds, + createCommandResponse(promise) + ) + } + } + + fun getStyles( + viewRef: ViewRefTag?, + promise: Promise + ) { + withMapViewOnUIThread(viewRef, promise) { + + it.getStyles( + createCommandResponse(promise) + ) + } + } + + fun setLayerProperties( + viewRef: ViewRefTag?, + layerId: String, + properties: Value, + promise: Promise + ) { + withMapViewOnUIThread(viewRef, promise) { + + it.setLayerProperties( + layerId, + properties, + createCommandResponse(promise) + ) + } + } + + fun setLayerProperty( + viewRef: ViewRefTag?, + layerId: String, + property: String, + value: Value, + promise: Promise + ) { + withMapViewOnUIThread(viewRef, promise) { + + it.setLayerProperty( + layerId, + property, + value, + createCommandResponse(promise) + ) + } + } companion object { const val NAME = "RNMBXMapViewModule" diff --git a/android/src/main/java/com/rnmapbox/rnmbx/components/mapview/RNMBXMapView.kt b/android/src/main/java/com/rnmapbox/rnmbx/components/mapview/RNMBXMapView.kt index 95398fb6c..0b2f841ad 100644 --- a/android/src/main/java/com/rnmapbox/rnmbx/components/mapview/RNMBXMapView.kt +++ b/android/src/main/java/com/rnmapbox/rnmbx/components/mapview/RNMBXMapView.kt @@ -95,6 +95,11 @@ data class OrnamentSettings( var position: Int = -1 ) +data class FeatureObject( + val id: String, + val feature: String +) + enum class MapGestureType { Move,Scale,Rotate,Fling,Shove } @@ -1502,7 +1507,74 @@ open class RNMBXMapView(private val mContext: Context, var mManager: RNMBXMapVie } } + fun queryRenderedLayersInRect(rect: RectF?, filter: Expression?, layerIDs: List?, response: CommandResponse) { + val size = mMap!!.getMapOptions().size + 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()), + ) + mMap.queryRenderedFeatures( + RenderedQueryGeometry(screenBox), + RenderedQueryOptions(layerIDs, filter) + ) { features -> + if (features.isValue) { + val featuresList = ArrayList() + for (i in features.value!!) { + val featureJson = try { + i.feature.toJson() + } catch (e: Exception) { + Logger.e("queryRenderedFeaturesAtPoint", "Error converting feature to JSON", e) + continue + } + featuresList.add(FeatureObject(i.sourceLayer ?: "unknown", featureJson)) + } + + response.success { + it.putString("data", featuresList.toString()) + } + } else { + response.error(features.error ?: "n/a") + } + } + } + + fun getStyles(response: CommandResponse) { + val styleJSON = try { + val style = mMap!!.getStyle() + val json = style?.styleJSON ?: throw Exception("Style JSON is null") + json + } catch (e: Exception) { + response.error("Error converting styles to JSON: ${e.message}") + return + } + response.success { + it.putString("data", styleJSON) + } + } + + fun setLayerProperties(layerId: String, properties: Value, response: CommandResponse) { + val style = mMap!!.getStyle() + try { + style?.setStyleLayerProperties(layerId,properties) + response.success { + it.putString("data", "Successfully set layer properties") + } + } catch (e: Exception) { + response.error("Error setting layer properties") + } + } + fun setLayerProperty(layerId: String, property: String, value: Value, response: CommandResponse) { + val style = mMap!!.getStyle() + try { + style?.setStyleLayerProperty(layerId,property,value) + response.success { + it.putString("data", "Successfully set layer property") + } + } catch (e: Exception) { + response.error("Error setting layer property") + } + } // endregion } diff --git a/ios/RNMBX/RNMBXMapViewManager.swift b/ios/RNMBX/RNMBXMapViewManager.swift index d6e0f7697..43c97a525 100644 --- a/ios/RNMBX/RNMBXMapViewManager.swift +++ b/ios/RNMBX/RNMBXMapViewManager.swift @@ -290,35 +290,35 @@ extension RNMBXMapViewManager { } } - @objc public static func setLayerFilter( - _ map: RNMBXMapView, - withLayerID layerID: String, - withFilter filter: [Any], - resolver: @escaping RCTPromiseResolveBlock, - rejecter: @escaping RCTPromiseRejectBlock) -> Void { + // @objc public static func setLayerFilter( + // _ map: RNMBXMapView, + // withLayerID layerID: String, + // withFilter filter: [Any], + // resolver: @escaping RCTPromiseResolveBlock, + // rejecter: @escaping RCTPromiseRejectBlock) -> Void { - do { - var layer:Layer = try map.mapboxMap.style.layer(withId: layerID) - if let specificLayer = layer as? FillLayer { - try map.mapboxMap.style.updateLayer(withId: layerID, type: FillLayer.self) { (updatedLayer: inout FillLayer) in - updatedLayer.filter = try filter.asExpression() - } - } else if let specificLayer = layer as? LineLayer { - try map.mapboxMap.style.updateLayer(withId: layerID, type: LineLayer.self) { (updatedLayer: inout LineLayer) in - updatedLayer.filter = try filter.asExpression() - } - } else if let specificLayer = layer as? CircleLayer { - try map.mapboxMap.style.updateLayer(withId: layerID, type: CircleLayer.self) { (updatedLayer: inout CircleLayer) in - updatedLayer.filter = try filter.asExpression() - } - } else { - // Handle other layer types as needed - throw NSError(domain: "", code: 0, userInfo: [NSLocalizedDescriptionKey: "Unsupported layer type"]) - } + // do { + // var layer:Layer = try map.mapboxMap.style.layer(withId: layerID) + // if let specificLayer = layer as? FillLayer { + // try map.mapboxMap.style.updateLayer(withId: layerID, type: FillLayer.self) { (updatedLayer: inout FillLayer) in + // updatedLayer.filter = try filter.asExpression() + // } + // } else if let specificLayer = layer as? LineLayer { + // try map.mapboxMap.style.updateLayer(withId: layerID, type: LineLayer.self) { (updatedLayer: inout LineLayer) in + // updatedLayer.filter = try filter.asExpression() + // } + // } else if let specificLayer = layer as? CircleLayer { + // try map.mapboxMap.style.updateLayer(withId: layerID, type: CircleLayer.self) { (updatedLayer: inout CircleLayer) in + // updatedLayer.filter = try filter.asExpression() + // } + // } else { + // // Handle other layer types as needed + // throw NSError(domain: "", code: 0, userInfo: [NSLocalizedDescriptionKey: "Unsupported layer type"]) + // } - resolver(nil) - } catch let error { - rejecter("failed_to_set_filter", "Set Layer Filter: \(error.localizedDescription)", error) - } - } + // resolver(nil) + // } catch let error { + // rejecter("failed_to_set_filter", "Set Layer Filter: \(error.localizedDescription)", error) + // } + // } } diff --git a/ios/RNMBX/RNMBXMapViewModule.mm b/ios/RNMBX/RNMBXMapViewModule.mm index cc374ef6f..dca7fdc36 100644 --- a/ios/RNMBX/RNMBXMapViewModule.mm +++ b/ios/RNMBX/RNMBXMapViewModule.mm @@ -163,12 +163,6 @@ - (void)withMapView:(nonnull NSNumber*)viewRef block:(void (^)(RNMBXMapView *))b } reject:reject methodName:@"setLayerProperty"]; } -RCT_EXPORT_METHOD(setLayerFilter: (nonnull NSNumber *)viewRef withLayerID: (NSString *)withLayerID withFilter: (NSArray *)withFilter resolve: (RCTPromiseResolveBlock)resolve rejecte: (RCTPromiseRejectBlock)reject){ - [self withMapView:viewRef block:^(RNMBXMapView *view) { - [RNMBXMapViewManager setLayerFilter:view withLayerID:withLayerID withFilter:withFilter resolver:resolve rejecter:reject]; - } reject:reject methodName:@"setLayerFilter"]; -} - // Thanks to this guard, we won't compile this code when we build for the old architecture. #ifdef RCT_NEW_ARCH_ENABLED - (std::shared_ptr)getTurboModule: diff --git a/package.json b/package.json index a6beb5993..d935747fc 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "rnmapbox-ditch", "description": "A customized Mapbox React Native module designed to create tailored maps, specifically enhanced for the unique requirements of Ditch Navigation.", - "version": "1.0.6", + "version": "1.0.7", "publishConfig": { "access": "public" }, From 0722287ec70be3de4d617f2341b688dca87c51e2 Mon Sep 17 00:00:00 2001 From: Hitesh Modi Date: Fri, 19 Jul 2024 14:33:04 +0530 Subject: [PATCH 03/11] fix: issues --- .../components/mapview/NativeMapViewModule.kt | 8 +- .../rnmbx/NativeMapViewModuleSpec.java | 116 ++++++++++-------- ios/RNMBX/RNMBXMapViewManager.swift | 4 +- src/components/MapView.tsx | 22 ++-- 4 files changed, 81 insertions(+), 69 deletions(-) diff --git a/android/src/main/java/com/rnmapbox/rnmbx/components/mapview/NativeMapViewModule.kt b/android/src/main/java/com/rnmapbox/rnmbx/components/mapview/NativeMapViewModule.kt index b1ef8df0c..4fe7280ee 100644 --- a/android/src/main/java/com/rnmapbox/rnmbx/components/mapview/NativeMapViewModule.kt +++ b/android/src/main/java/com/rnmapbox/rnmbx/components/mapview/NativeMapViewModule.kt @@ -185,7 +185,7 @@ class NativeMapViewModule(context: ReactApplicationContext, val viewTagResolver: ) } } - fun queryRenderedLayersInRect( + override fun queryRenderedLayersInRect( viewRef: ViewRefTag?, withBBox: ReadableArray, withFilter: ReadableArray, @@ -204,7 +204,7 @@ class NativeMapViewModule(context: ReactApplicationContext, val viewTagResolver: } } - fun getStyles( + override fun getStyles( viewRef: ViewRefTag?, promise: Promise ) { @@ -216,7 +216,7 @@ class NativeMapViewModule(context: ReactApplicationContext, val viewTagResolver: } } - fun setLayerProperties( + override fun setLayerProperties( viewRef: ViewRefTag?, layerId: String, properties: Value, @@ -232,7 +232,7 @@ class NativeMapViewModule(context: ReactApplicationContext, val viewTagResolver: } } - fun setLayerProperty( + override fun setLayerProperty( viewRef: ViewRefTag?, layerId: String, property: String, diff --git a/android/src/main/old-arch/com/rnmapbox/rnmbx/NativeMapViewModuleSpec.java b/android/src/main/old-arch/com/rnmapbox/rnmbx/NativeMapViewModuleSpec.java index 3081071d0..fe3dbd41b 100644 --- a/android/src/main/old-arch/com/rnmapbox/rnmbx/NativeMapViewModuleSpec.java +++ b/android/src/main/old-arch/com/rnmapbox/rnmbx/NativeMapViewModuleSpec.java @@ -1,4 +1,3 @@ - /** * This code was generated by [react-native-codegen](https://www.npmjs.com/package/react-native-codegen). * @@ -9,7 +8,6 @@ * * @nolint */ - package com.rnmapbox.rnmbx; import com.facebook.proguard.annotations.DoNotStrip; @@ -22,68 +20,88 @@ import com.facebook.react.turbomodule.core.interfaces.TurboModule; import javax.annotation.Nonnull; import javax.annotation.Nullable; +import com.mapbox.bindgen.Value; public abstract class NativeMapViewModuleSpec extends ReactContextBaseJavaModule implements ReactModuleWithSpec, TurboModule { - public static final String NAME = "RNMBXMapViewModule"; - public NativeMapViewModuleSpec(ReactApplicationContext reactContext) { - super(reactContext); - } + public static final String NAME = "RNMBXMapViewModule"; + + public NativeMapViewModuleSpec(ReactApplicationContext reactContext) { + super(reactContext); + } + + @Override + public @Nonnull + String getName() { + return NAME; + } + + @ReactMethod + @DoNotStrip + public abstract void takeSnap(@Nullable Integer viewRef, boolean writeToDisk, Promise promise); + + @ReactMethod + @DoNotStrip + public abstract void queryTerrainElevation(@Nullable Integer viewRef, ReadableArray coordinates, Promise promise); + + @ReactMethod + @DoNotStrip + public abstract void setSourceVisibility(@Nullable Integer viewRef, boolean visible, String sourceId, String sourceLayerId, Promise promise); + + @ReactMethod + @DoNotStrip + public abstract void getCenter(@Nullable Integer viewRef, Promise promise); - @Override - public @Nonnull String getName() { - return NAME; - } + @ReactMethod + @DoNotStrip + public abstract void getCoordinateFromView(@Nullable Integer viewRef, ReadableArray atPoint, Promise promise); - @ReactMethod - @DoNotStrip - public abstract void takeSnap(@Nullable Integer viewRef, boolean writeToDisk, Promise promise); + @ReactMethod + @DoNotStrip + public abstract void getPointInView(@Nullable Integer viewRef, ReadableArray atCoordinate, Promise promise); - @ReactMethod - @DoNotStrip - public abstract void queryTerrainElevation(@Nullable Integer viewRef, ReadableArray coordinates, Promise promise); + @ReactMethod + @DoNotStrip + public abstract void getZoom(@Nullable Integer viewRef, Promise promise); - @ReactMethod - @DoNotStrip - public abstract void setSourceVisibility(@Nullable Integer viewRef, boolean visible, String sourceId, String sourceLayerId, Promise promise); + @ReactMethod + @DoNotStrip + public abstract void getVisibleBounds(@Nullable Integer viewRef, Promise promise); - @ReactMethod - @DoNotStrip - public abstract void getCenter(@Nullable Integer viewRef, Promise promise); + @ReactMethod + @DoNotStrip + public abstract void queryRenderedFeaturesAtPoint(@Nullable Integer viewRef, ReadableArray atPoint, ReadableArray withFilter, ReadableArray withLayerIDs, Promise promise); - @ReactMethod - @DoNotStrip - public abstract void getCoordinateFromView(@Nullable Integer viewRef, ReadableArray atPoint, Promise promise); + @ReactMethod + @DoNotStrip + public abstract void queryRenderedFeaturesInRect(@Nullable Integer viewRef, ReadableArray withBBox, ReadableArray withFilter, ReadableArray withLayerIDs, Promise promise); - @ReactMethod - @DoNotStrip - public abstract void getPointInView(@Nullable Integer viewRef, ReadableArray atCoordinate, Promise promise); + @ReactMethod + @DoNotStrip + public abstract void setHandledMapChangedEvents(@Nullable Integer viewRef, ReadableArray events, Promise promise); - @ReactMethod - @DoNotStrip - public abstract void getZoom(@Nullable Integer viewRef, Promise promise); + @ReactMethod + @DoNotStrip + public abstract void clearData(@Nullable Integer viewRef, Promise promise); - @ReactMethod - @DoNotStrip - public abstract void getVisibleBounds(@Nullable Integer viewRef, Promise promise); + @ReactMethod + @DoNotStrip + public abstract void querySourceFeatures(@Nullable Integer viewRef, String sourceId, ReadableArray withFilter, ReadableArray withSourceLayerIDs, Promise promise); - @ReactMethod - @DoNotStrip - public abstract void queryRenderedFeaturesAtPoint(@Nullable Integer viewRef, ReadableArray atPoint, ReadableArray withFilter, ReadableArray withLayerIDs, Promise promise); + @ReactMethod + @DoNotStrip + public abstract void queryRenderedLayersInRect(@Nullable Integer viewRef, ReadableArray withBBox, ReadableArray withFilter, ReadableArray withLayerIDs, Promise promise); - @ReactMethod - @DoNotStrip - public abstract void queryRenderedFeaturesInRect(@Nullable Integer viewRef, ReadableArray withBBox, ReadableArray withFilter, ReadableArray withLayerIDs, Promise promise); + @ReactMethod + @DoNotStrip + public abstract void getStyles(@Nullable Integer viewRef, Promise promise); - @ReactMethod - @DoNotStrip - public abstract void setHandledMapChangedEvents(@Nullable Integer viewRef, ReadableArray events, Promise promise); + @ReactMethod + @DoNotStrip + public abstract void setLayerProperties(@Nullable Integer viewRef, String layerId, Value properties, Promise promise); - @ReactMethod - @DoNotStrip - public abstract void clearData(@Nullable Integer viewRef, Promise promise); + @ReactMethod + @DoNotStrip + public abstract void setLayerProperty(@Nullable Integer viewRef, String layerId, String property, Value value, Promise promise); - @ReactMethod - @DoNotStrip - public abstract void querySourceFeatures(@Nullable Integer viewRef, String sourceId, ReadableArray withFilter, ReadableArray withSourceLayerIDs, Promise promise); } diff --git a/ios/RNMBX/RNMBXMapViewManager.swift b/ios/RNMBX/RNMBXMapViewManager.swift index 43c97a525..bbc261507 100644 --- a/ios/RNMBX/RNMBXMapViewManager.swift +++ b/ios/RNMBX/RNMBXMapViewManager.swift @@ -244,7 +244,7 @@ extension RNMBXMapViewManager { case .success(let features): resolver(features.compactMap { queriedFeature in logged("queryRenderedLayersInRect.queriedfeature.map") { - return ["id":queriedFeature.sourceLayer, "feature": try queriedFeature.feature.toJSON()] } + return ["data": ["id": queriedFeature.sourceLayer, "feature": try queriedFeature.feature.toJSON()]] } }) case .failure(let error): rejecter("queryRenderedLayersInRect","failed to query features", error) @@ -257,7 +257,7 @@ extension RNMBXMapViewManager { resolver: @escaping RCTPromiseResolveBlock, rejecter: @escaping RCTPromiseRejectBlock) -> Void { logged("getStyles.option", rejecter: rejecter) { - resolver(map.mapboxMap.style.JSON) + resolver(["data": map.mapboxMap.style.JSON]) } } diff --git a/src/components/MapView.tsx b/src/components/MapView.tsx index 4986eb901..b49b70563 100644 --- a/src/components/MapView.tsx +++ b/src/components/MapView.tsx @@ -789,17 +789,16 @@ class MapView extends NativeBridgeComponent( bbox != null && (bbox.length === 4 || (RNMBXModule.MapboxV10 && bbox.length === 0)) ) { - const res = await this._runNative('queryRenderedLayersInRect', [ - bbox, - getFilter(filter), - layerIDs, - ]); + const res = await this._runNative<{ data: GeoJSON.Feature }>( + 'queryRenderedLayersInRect', + [bbox, getFilter(filter), layerIDs], + ); if (isAndroid()) { - return JSON.parse(res as unknown as string); + return JSON.parse(res.data as unknown as string); } - return res; + return res.data; } else { throw new Error( 'Must pass in a valid bounding box: [top, right, bottom, left]. An empty array [] is also acceptable in v10.', @@ -807,7 +806,8 @@ class MapView extends NativeBridgeComponent( } } async getStyles() { - return JSON.parse(await this._runNative('getStyles')); + const res = await this._runNative<{ data: string }>('getStyles'); + return JSON.parse(res.data); } async setLayerProperties(layerID: string, properities: LayerProperties) { @@ -823,12 +823,6 @@ class MapView extends NativeBridgeComponent( ]); } - async setLayerFilter(layerID: string, filter: string[]) { - return await this._runNative('setLayerFilter', [ - layerID, - getFilter(filter), - ]); - } /** * Returns an array of GeoJSON Feature objects representing features within the specified vector tile or GeoJSON source that satisfy the query parameters. * From ce4d2232c5b51b4f5fe20c3a4a45b01960b85d49 Mon Sep 17 00:00:00 2001 From: Hitesh Modi Date: Mon, 22 Jul 2024 21:52:52 +0530 Subject: [PATCH 04/11] fix: queryRenderedLayers --- .../components/mapview/NativeMapViewModule.kt | 5 +- .../rnmbx/components/mapview/RNMBXMapView.kt | 51 ++++++++++++++++--- .../rnmbx/NativeMapViewModuleSpec.java | 6 +-- ios/RNMBX/RNMBXMapViewManager.swift | 8 +-- ios/RNMBX/RNMBXMapViewModule.mm | 2 +- package.json | 2 +- src/components/MapView.tsx | 25 +++++---- 7 files changed, 73 insertions(+), 26 deletions(-) diff --git a/android/src/main/java/com/rnmapbox/rnmbx/components/mapview/NativeMapViewModule.kt b/android/src/main/java/com/rnmapbox/rnmbx/components/mapview/NativeMapViewModule.kt index 4fe7280ee..35dddca95 100644 --- a/android/src/main/java/com/rnmapbox/rnmbx/components/mapview/NativeMapViewModule.kt +++ b/android/src/main/java/com/rnmapbox/rnmbx/components/mapview/NativeMapViewModule.kt @@ -3,6 +3,7 @@ package com.rnmapbox.rnmbx.components.mapview import com.facebook.react.bridge.Promise import com.facebook.react.bridge.ReactApplicationContext import com.facebook.react.bridge.ReadableArray +import com.facebook.react.bridge.ReadableMap import com.facebook.react.bridge.WritableMap import com.facebook.react.bridge.WritableNativeMap import com.rnmapbox.rnmbx.NativeMapViewModuleSpec @@ -219,7 +220,7 @@ class NativeMapViewModule(context: ReactApplicationContext, val viewTagResolver: override fun setLayerProperties( viewRef: ViewRefTag?, layerId: String, - properties: Value, + properties: ReadableMap, promise: Promise ) { withMapViewOnUIThread(viewRef, promise) { @@ -236,7 +237,7 @@ class NativeMapViewModule(context: ReactApplicationContext, val viewTagResolver: viewRef: ViewRefTag?, layerId: String, property: String, - value: Value, + value: String, promise: Promise ) { withMapViewOnUIThread(viewRef, promise) { diff --git a/android/src/main/java/com/rnmapbox/rnmbx/components/mapview/RNMBXMapView.kt b/android/src/main/java/com/rnmapbox/rnmbx/components/mapview/RNMBXMapView.kt index 0b2f841ad..624319ef6 100644 --- a/android/src/main/java/com/rnmapbox/rnmbx/components/mapview/RNMBXMapView.kt +++ b/android/src/main/java/com/rnmapbox/rnmbx/components/mapview/RNMBXMapView.kt @@ -68,6 +68,7 @@ import java.util.* import com.rnmapbox.rnmbx.components.annotation.RNMBXPointAnnotationCoordinator import com.rnmapbox.rnmbx.components.images.ImageManager +import com.google.gson.Gson; import com.rnmapbox.rnmbx.v11compat.event.* import com.rnmapbox.rnmbx.v11compat.feature.* @@ -75,6 +76,7 @@ import com.rnmapbox.rnmbx.v11compat.mapboxmap.* import com.rnmapbox.rnmbx.v11compat.ornamentsettings.* import org.json.JSONException import org.json.JSONObject +import com.facebook.react.bridge.ReadableMapKeySetIterator; fun MutableList.removeIf21(predicate: (T) -> Boolean): Boolean { var removed = false @@ -88,6 +90,40 @@ fun MutableList.removeIf21(predicate: (T) -> Boolean): Boolean { } return removed } +fun convertReadableArrayToList(readableArray: ReadableArray): List { + val list = mutableListOf() + for (i in 0 until readableArray.size()) { + val value = when (readableArray.getType(i)) { + ReadableType.Boolean -> Value.valueOf(readableArray.getBoolean(i)) + ReadableType.Number -> Value.valueOf(readableArray.getDouble(i)) + ReadableType.String -> Value.valueOf(readableArray.getString(i) ?: "") + ReadableType.Map -> Value.valueOf(convertReadableMapToHashMap(readableArray.getMap(i)!!)) + ReadableType.Array -> Value.valueOf(convertReadableArrayToList(readableArray.getArray(i)!!)) + else -> throw IllegalArgumentException("Unsupported type in array") + } + list.add(value) + } + return list +} +fun convertReadableMapToHashMap(readableMap: ReadableMap): HashMap { + val hashMap = HashMap() + val iterator: ReadableMapKeySetIterator = readableMap.keySetIterator() + + while (iterator.hasNextKey()) { + val key = iterator.nextKey() + val value = when (readableMap.getType(key)) { + ReadableType.Boolean -> Value.valueOf(readableMap.getBoolean(key)) + ReadableType.Number -> Value.valueOf(readableMap.getDouble(key)) + ReadableType.String -> Value.valueOf(readableMap.getString(key)!!) + ReadableType.Map -> Value.valueOf(convertReadableMapToHashMap(readableMap.getMap(key)!!)) + ReadableType.Array -> Value.valueOf(convertReadableArrayToList(readableMap.getArray(key)!!)) + else -> throw IllegalArgumentException("Unsupported type") + } + hashMap[key] = value + } + + return hashMap +} data class OrnamentSettings( var enabled : Boolean? = false, @@ -1530,7 +1566,9 @@ open class RNMBXMapView(private val mContext: Context, var mManager: RNMBXMapVie } response.success { - it.putString("data", featuresList.toString()) + val gson = Gson(); + val json = gson.toJson(featuresList); + it.putString("data", json) } } else { response.error(features.error ?: "n/a") @@ -1552,22 +1590,23 @@ open class RNMBXMapView(private val mContext: Context, var mManager: RNMBXMapVie } } - fun setLayerProperties(layerId: String, properties: Value, response: CommandResponse) { + fun setLayerProperties(layerId: String, properties: ReadableMap, response: CommandResponse) { val style = mMap!!.getStyle() + val propertiesValue = convertReadableMapToHashMap(properties) try { - style?.setStyleLayerProperties(layerId,properties) + style?.setStyleLayerProperties(layerId, Value.valueOf(propertiesValue)) response.success { it.putString("data", "Successfully set layer properties") } } catch (e: Exception) { - response.error("Error setting layer properties") + response.error("Error setting layer properties: ${e.message}") } } - fun setLayerProperty(layerId: String, property: String, value: Value, response: CommandResponse) { + fun setLayerProperty(layerId: String, property: String, value: String, response: CommandResponse) { val style = mMap!!.getStyle() try { - style?.setStyleLayerProperty(layerId,property,value) + style?.setStyleLayerProperty(layerId,property,value as Value) response.success { it.putString("data", "Successfully set layer property") } diff --git a/android/src/main/old-arch/com/rnmapbox/rnmbx/NativeMapViewModuleSpec.java b/android/src/main/old-arch/com/rnmapbox/rnmbx/NativeMapViewModuleSpec.java index fe3dbd41b..7a60eabf1 100644 --- a/android/src/main/old-arch/com/rnmapbox/rnmbx/NativeMapViewModuleSpec.java +++ b/android/src/main/old-arch/com/rnmapbox/rnmbx/NativeMapViewModuleSpec.java @@ -17,10 +17,10 @@ import com.facebook.react.bridge.ReactMethod; import com.facebook.react.bridge.ReactModuleWithSpec; import com.facebook.react.bridge.ReadableArray; +import com.facebook.react.bridge.ReadableMap; import com.facebook.react.turbomodule.core.interfaces.TurboModule; import javax.annotation.Nonnull; import javax.annotation.Nullable; -import com.mapbox.bindgen.Value; public abstract class NativeMapViewModuleSpec extends ReactContextBaseJavaModule implements ReactModuleWithSpec, TurboModule { @@ -98,10 +98,10 @@ String getName() { @ReactMethod @DoNotStrip - public abstract void setLayerProperties(@Nullable Integer viewRef, String layerId, Value properties, Promise promise); + public abstract void setLayerProperties(@Nullable Integer viewRef, String layerId, ReadableMap properties, Promise promise); @ReactMethod @DoNotStrip - public abstract void setLayerProperty(@Nullable Integer viewRef, String layerId, String property, Value value, Promise promise); + public abstract void setLayerProperty(@Nullable Integer viewRef, String layerId, String property, String value, Promise promise); } diff --git a/ios/RNMBX/RNMBXMapViewManager.swift b/ios/RNMBX/RNMBXMapViewManager.swift index bbc261507..f2fb91dfe 100644 --- a/ios/RNMBX/RNMBXMapViewManager.swift +++ b/ios/RNMBX/RNMBXMapViewManager.swift @@ -242,10 +242,10 @@ extension RNMBXMapViewManager { map.mapboxMap.queryRenderedFeatures(with: rect, options: options) { result in switch result { case .success(let features): - resolver(features.compactMap { queriedFeature in + resolver(["data": features.compactMap { queriedFeature in logged("queryRenderedLayersInRect.queriedfeature.map") { - return ["data": ["id": queriedFeature.sourceLayer, "feature": try queriedFeature.feature.toJSON()]] } - }) + return ["id": queriedFeature.sourceLayer, "feature": try queriedFeature.feature.toJSON()] } + }]) case .failure(let error): rejecter("queryRenderedLayersInRect","failed to query features", error) } @@ -279,7 +279,7 @@ extension RNMBXMapViewManager { _ map: RNMBXMapView, withLayerID layerID: String, withProperty property: String, - withValue value: Any, + withValue value: String, resolver: @escaping RCTPromiseResolveBlock, rejecter: @escaping RCTPromiseRejectBlock) -> Void { do{ diff --git a/ios/RNMBX/RNMBXMapViewModule.mm b/ios/RNMBX/RNMBXMapViewModule.mm index dca7fdc36..9ad171370 100644 --- a/ios/RNMBX/RNMBXMapViewModule.mm +++ b/ios/RNMBX/RNMBXMapViewModule.mm @@ -157,7 +157,7 @@ - (void)withMapView:(nonnull NSNumber*)viewRef block:(void (^)(RNMBXMapView *))b } reject:reject methodName:@"setLayerProperties"]; } -RCT_EXPORT_METHOD(setLayerProperty : (nonnull NSNumber *)viewRef withLayerID: (nonnull NSString *)withLayerID withProperty: (nonnull NSString *)withProperty withValue: (id)withValue resolve: (RCTPromiseResolveBlock)resolve reject: (RCTPromiseRejectBlock)reject){ +RCT_EXPORT_METHOD(setLayerProperty : (nonnull NSNumber *)viewRef withLayerID: (nonnull NSString *)withLayerID withProperty: (nonnull NSString *)withProperty withValue: (nonnull NSString *)withValue resolve: (RCTPromiseResolveBlock)resolve reject: (RCTPromiseRejectBlock)reject){ [self withMapView:viewRef block:^(RNMBXMapView *view) { [RNMBXMapViewManager setLayerProperty:view withLayerID:withLayerID withProperty:withProperty withValue:withValue resolver:resolve rejecter:reject]; } reject:reject methodName:@"setLayerProperty"]; diff --git a/package.json b/package.json index d935747fc..9f252aa31 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "rnmapbox-ditch", "description": "A customized Mapbox React Native module designed to create tailored maps, specifically enhanced for the unique requirements of Ditch Navigation.", - "version": "1.0.7", + "version": "1.1.2", "publishConfig": { "access": "public" }, diff --git a/src/components/MapView.tsx b/src/components/MapView.tsx index b49b70563..dde4ed50b 100644 --- a/src/components/MapView.tsx +++ b/src/components/MapView.tsx @@ -470,9 +470,14 @@ type CallbablePropKeysWithoutOn = CallbablePropKeys extends `on${infer C}` type Debounced = F & { clear(): void; flush(): void }; +type LayerProperty = Array | string | number | boolean; + type LayerProperties = { - // eslint-disable-next-line @typescript-eslint/no-explicit-any - [key: string]: any; + [key: string]: LayerProperty; +}; +type FeatureLayer = { + id: string; + feature: GeoJSON.Feature | string; }; /** @@ -789,13 +794,16 @@ class MapView extends NativeBridgeComponent( bbox != null && (bbox.length === 4 || (RNMBXModule.MapboxV10 && bbox.length === 0)) ) { - const res = await this._runNative<{ data: GeoJSON.Feature }>( - 'queryRenderedLayersInRect', - [bbox, getFilter(filter), layerIDs], - ); + const res = await this._runNative<{ + data: FeatureLayer[] | string; + }>('queryRenderedLayersInRect', [bbox, getFilter(filter), layerIDs]); if (isAndroid()) { - return JSON.parse(res.data as unknown as string); + const data = JSON.parse(res.data as unknown as string); + + return data.map((layer: FeatureLayer) => { + return { ...res, feature: JSON.parse(layer.feature) }; + }); } return res.data; @@ -814,8 +822,7 @@ class MapView extends NativeBridgeComponent( return await this._runNative('setLayerProperties', [layerID, properities]); } - // eslint-disable-next-line @typescript-eslint/no-explicit-any - async setLayerProperty(layerID: string, property: string, value: any) { + async setLayerProperty(layerID: string, property: string, value: string) { return await this._runNative('setLayerProperty', [ layerID, property, From c0dffb4898c03c8f5610b70493d95badae040c17 Mon Sep 17 00:00:00 2001 From: Hitesh Modi Date: Fri, 16 Aug 2024 13:34:52 +0530 Subject: [PATCH 05/11] fix: ios queryRenderedLayersInRect --- ios/RNMBX/RNMBXMapViewManager.swift | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ios/RNMBX/RNMBXMapViewManager.swift b/ios/RNMBX/RNMBXMapViewManager.swift index f2fb91dfe..5c49bd709 100644 --- a/ios/RNMBX/RNMBXMapViewManager.swift +++ b/ios/RNMBX/RNMBXMapViewManager.swift @@ -242,9 +242,9 @@ extension RNMBXMapViewManager { map.mapboxMap.queryRenderedFeatures(with: rect, options: options) { result in switch result { case .success(let features): - resolver(["data": features.compactMap { queriedFeature in + resolver(["data": features.compactMap { feature in logged("queryRenderedLayersInRect.queriedfeature.map") { - return ["id": queriedFeature.sourceLayer, "feature": try queriedFeature.feature.toJSON()] } + return ["id": feature.queriedFeature.sourceLayer, "feature": try feature.queriedFeature.feature.toJSON()] } }]) case .failure(let error): rejecter("queryRenderedLayersInRect","failed to query features", error) From bd4f415366a00ed53136b586404573ac2d16c3e1 Mon Sep 17 00:00:00 2001 From: Hitesh Modi Date: Fri, 16 Aug 2024 13:36:53 +0530 Subject: [PATCH 06/11] fix: bugs --- package.json | 2 +- src/components/MapView.tsx | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 9f252aa31..b441d0876 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "rnmapbox-ditch", "description": "A customized Mapbox React Native module designed to create tailored maps, specifically enhanced for the unique requirements of Ditch Navigation.", - "version": "1.1.2", + "version": "1.1.3", "publishConfig": { "access": "public" }, diff --git a/src/components/MapView.tsx b/src/components/MapView.tsx index dde4ed50b..f30ed98b2 100644 --- a/src/components/MapView.tsx +++ b/src/components/MapView.tsx @@ -802,7 +802,7 @@ class MapView extends NativeBridgeComponent( const data = JSON.parse(res.data as unknown as string); return data.map((layer: FeatureLayer) => { - return { ...res, feature: JSON.parse(layer.feature) }; + return { ...res, feature: JSON.parse(layer.feature as string) }; }); } From 1b05f4cb11e8d4572e4cf664f98ad16dcd7e3595 Mon Sep 17 00:00:00 2001 From: Hitesh Modi Date: Fri, 16 Aug 2024 14:50:55 +0530 Subject: [PATCH 07/11] fix: queryRenderedFeatures android fix --- .../com/rnmapbox/rnmbx/components/mapview/RNMBXMapView.kt | 4 ++-- package.json | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/android/src/main/java/com/rnmapbox/rnmbx/components/mapview/RNMBXMapView.kt b/android/src/main/java/com/rnmapbox/rnmbx/components/mapview/RNMBXMapView.kt index 624319ef6..e14385b07 100644 --- a/android/src/main/java/com/rnmapbox/rnmbx/components/mapview/RNMBXMapView.kt +++ b/android/src/main/java/com/rnmapbox/rnmbx/components/mapview/RNMBXMapView.kt @@ -1557,12 +1557,12 @@ open class RNMBXMapView(private val mContext: Context, var mManager: RNMBXMapVie val featuresList = ArrayList() for (i in features.value!!) { val featureJson = try { - i.feature.toJson() + i.queriedFeature.feature.toJson() } catch (e: Exception) { Logger.e("queryRenderedFeaturesAtPoint", "Error converting feature to JSON", e) continue } - featuresList.add(FeatureObject(i.sourceLayer ?: "unknown", featureJson)) + featuresList.add(FeatureObject(i.queriedFeature.sourceLayer ?: "unknown", featureJson)) } response.success { diff --git a/package.json b/package.json index b441d0876..8cc5d3702 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "rnmapbox-ditch", "description": "A customized Mapbox React Native module designed to create tailored maps, specifically enhanced for the unique requirements of Ditch Navigation.", - "version": "1.1.3", + "version": "1.1.4", "publishConfig": { "access": "public" }, From bbc9fabe7c7bb35a64b44cd639ace621e12d4717 Mon Sep 17 00:00:00 2001 From: Hitesh Modi Date: Mon, 2 Sep 2024 21:10:13 +0530 Subject: [PATCH 08/11] feat: bearing line --- package.json | 2 +- src/components/MapView.tsx | 2 +- src/components/UserLocation.tsx | 90 ++++++++++++++++++++++++++++----- 3 files changed, 79 insertions(+), 15 deletions(-) diff --git a/package.json b/package.json index 8cc5d3702..dbabacab9 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "rnmapbox-ditch", "description": "A customized Mapbox React Native module designed to create tailored maps, specifically enhanced for the unique requirements of Ditch Navigation.", - "version": "1.1.4", + "version": "1.1.5", "publishConfig": { "access": "public" }, diff --git a/src/components/MapView.tsx b/src/components/MapView.tsx index f30ed98b2..24cf27787 100644 --- a/src/components/MapView.tsx +++ b/src/components/MapView.tsx @@ -802,7 +802,7 @@ class MapView extends NativeBridgeComponent( const data = JSON.parse(res.data as unknown as string); return data.map((layer: FeatureLayer) => { - return { ...res, feature: JSON.parse(layer.feature as string) }; + return { ...layer, feature: JSON.parse(layer.feature as string) }; }); } diff --git a/src/components/UserLocation.tsx b/src/components/UserLocation.tsx index d45fb5778..1e6d56e9e 100644 --- a/src/components/UserLocation.tsx +++ b/src/components/UserLocation.tsx @@ -2,7 +2,7 @@ import React, { ReactElement } from 'react'; import locationManager from '../modules/location/locationManager'; import { type Location } from '../modules/location/locationManager'; -import { CircleLayerStyle } from '../Mapbox'; +import { CircleLayerStyle, LineLayer, ShapeSource } from '../Mapbox'; import Annotation from './Annotation'; import CircleLayer from './CircleLayer'; @@ -32,6 +32,40 @@ const layerStyles: Record<'normal', Record> = { }, }; +function calculateDestination( + coordinates: number[], + distance: number, + bearing: number | null, +) { + if (!bearing) return coordinates; + const R = 6371; // Radius of the Earth in km + const [lng, lat] = coordinates; + const latRad = (lat * Math.PI) / 180; + const lngRad = (lng * Math.PI) / 180; + const bearingRad = (bearing * Math.PI) / 180; + const distRad = distance / R; + + const destLatRad = Math.asin( + Math.sin(latRad) * Math.cos(distRad) + + Math.cos(latRad) * Math.sin(distRad) * Math.cos(bearingRad), + ); + + const destLngRad = + lngRad + + Math.atan2( + Math.sin(bearingRad) * Math.sin(distRad) * Math.cos(latRad), + Math.cos(distRad) - Math.sin(latRad) * Math.sin(destLatRad), + ); + + const destLat = (destLatRad * 180) / Math.PI; + const destLng = (destLngRad * 180) / Math.PI; + const dest: [number, number] = [destLng, destLat]; + return dest; +} +const noseLineStyle = { + lineColor: 'red', + lineWidth: 2, +}; const normalIcon = ( showsUserHeadingIndicator?: boolean, heading?: number | null, @@ -237,7 +271,7 @@ class UserLocation extends React.Component { if (location && location.coords) { const { longitude, latitude } = location.coords; - ({ heading } = location.coords); + heading = location.coords.course ?? location.coords.heading; coordinates = [longitude, latitude]; } @@ -279,17 +313,47 @@ class UserLocation extends React.Component { } return ( - - {children || normalIcon(showsUserHeadingIndicator, heading)} - + <> + + {children || normalIcon(false, heading)} + + {showsUserHeadingIndicator && heading && ( + + + + )} + ); } } From 930bcce3762889e6dbda38c955545d65253270ac Mon Sep 17 00:00:00 2001 From: Hitesh Modi Date: Mon, 2 Sep 2024 21:11:01 +0530 Subject: [PATCH 09/11] fix: version --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index dbabacab9..6d00fa1ef 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "rnmapbox-ditch", "description": "A customized Mapbox React Native module designed to create tailored maps, specifically enhanced for the unique requirements of Ditch Navigation.", - "version": "1.1.5", + "version": "1.1.6", "publishConfig": { "access": "public" }, From cf2f2a461f98229fa1a7fa008028420e1fe1f152 Mon Sep 17 00:00:00 2001 From: Hitesh Modi Date: Thu, 5 Sep 2024 23:33:48 +0530 Subject: [PATCH 10/11] remove noseline --- package.json | 2 +- src/components/UserLocation.tsx | 88 +++++---------------------------- 2 files changed, 13 insertions(+), 77 deletions(-) diff --git a/package.json b/package.json index 6d00fa1ef..b7b50acdc 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "rnmapbox-ditch", "description": "A customized Mapbox React Native module designed to create tailored maps, specifically enhanced for the unique requirements of Ditch Navigation.", - "version": "1.1.6", + "version": "1.1.8", "publishConfig": { "access": "public" }, diff --git a/src/components/UserLocation.tsx b/src/components/UserLocation.tsx index 1e6d56e9e..10c62b982 100644 --- a/src/components/UserLocation.tsx +++ b/src/components/UserLocation.tsx @@ -2,7 +2,7 @@ import React, { ReactElement } from 'react'; import locationManager from '../modules/location/locationManager'; import { type Location } from '../modules/location/locationManager'; -import { CircleLayerStyle, LineLayer, ShapeSource } from '../Mapbox'; +import { CircleLayerStyle } from '../Mapbox'; import Annotation from './Annotation'; import CircleLayer from './CircleLayer'; @@ -32,40 +32,6 @@ const layerStyles: Record<'normal', Record> = { }, }; -function calculateDestination( - coordinates: number[], - distance: number, - bearing: number | null, -) { - if (!bearing) return coordinates; - const R = 6371; // Radius of the Earth in km - const [lng, lat] = coordinates; - const latRad = (lat * Math.PI) / 180; - const lngRad = (lng * Math.PI) / 180; - const bearingRad = (bearing * Math.PI) / 180; - const distRad = distance / R; - - const destLatRad = Math.asin( - Math.sin(latRad) * Math.cos(distRad) + - Math.cos(latRad) * Math.sin(distRad) * Math.cos(bearingRad), - ); - - const destLngRad = - lngRad + - Math.atan2( - Math.sin(bearingRad) * Math.sin(distRad) * Math.cos(latRad), - Math.cos(distRad) - Math.sin(latRad) * Math.sin(destLatRad), - ); - - const destLat = (destLatRad * 180) / Math.PI; - const destLng = (destLngRad * 180) / Math.PI; - const dest: [number, number] = [destLng, destLat]; - return dest; -} -const noseLineStyle = { - lineColor: 'red', - lineWidth: 2, -}; const normalIcon = ( showsUserHeadingIndicator?: boolean, heading?: number | null, @@ -313,47 +279,17 @@ class UserLocation extends React.Component { } return ( - <> - - {children || normalIcon(false, heading)} - - {showsUserHeadingIndicator && heading && ( - - - - )} - + + {children || normalIcon(showsUserHeadingIndicator, heading)} + ); } } From 796b1dc34d6bc9a34d1dd889077bef7bcb557549 Mon Sep 17 00:00:00 2001 From: Hitesh Modi Date: Sun, 15 Sep 2024 15:02:51 +0530 Subject: [PATCH 11/11] fix: annotation click event --- example/rnmapbox.web 2.symlink | 1 + package.json | 16 ++++++++-------- src/components/Annotation.tsx | 2 +- src/components/UserLocation.tsx | 3 ++- 4 files changed, 12 insertions(+), 10 deletions(-) create mode 120000 example/rnmapbox.web 2.symlink diff --git a/example/rnmapbox.web 2.symlink b/example/rnmapbox.web 2.symlink new file mode 120000 index 000000000..8fb16c871 --- /dev/null +++ b/example/rnmapbox.web 2.symlink @@ -0,0 +1 @@ +../javascript/web \ No newline at end of file diff --git a/package.json b/package.json index b7b50acdc..c10bd6a54 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "rnmapbox-ditch", "description": "A customized Mapbox React Native module designed to create tailored maps, specifically enhanced for the unique requirements of Ditch Navigation.", - "version": "1.1.8", + "version": "1.2.0", "publishConfig": { "access": "public" }, @@ -56,10 +56,10 @@ }, "peerDependencies": { "expo": ">=47.0.0", - "mapbox-gl": "^2.9.0", + "mapbox-gl": "^3.1.2", "react": ">=16.6.1", - "react-native": ">=0.59.9", - "react-dom": ">= 17.0.0" + "react-dom": ">= 17.0.0", + "react-native": ">=0.59.9" }, "peerDependenciesMeta": { "expo": { @@ -86,6 +86,7 @@ "@babel/eslint-parser": "^7.19.1", "@babel/plugin-proposal-class-properties": "7.18.6", "@babel/runtime": "7.19.0", + "@mdx-js/mdx": "^3.0.0", "@react-native/eslint-config": "^0.72.2", "@sinonjs/fake-timers": "^8.0.1", "@testing-library/react-native": "^12.4.0", @@ -108,18 +109,17 @@ "jest": "29.7.0", "jest-cli": "29.7.0", "lint-staged": "^12.1.2", - "mapbox-gl": "^2.9.0", + "mapbox-gl": "^3.1.2", "metro-react-native-babel-preset": "0.71.1", "node-dir": "0.1.17", "prettier": "2.7.1", "react": "18.2.0", "react-docgen": "rnmapbox/react-docgen#rnmapbox-dist-react-docgen-v6", - "react-native": "0.73.0-rc.4", + "react-native": "^0.75.2", "react-native-builder-bob": "^0.23.1", "react-test-renderer": "18.2.0", "ts-node": "10.9.1", - "typescript": "5.1.3", - "@mdx-js/mdx": "^3.0.0" + "typescript": "5.1.3" }, "codegenConfig": { "name": "rnmapbox_maps_specs", diff --git a/src/components/Annotation.tsx b/src/components/Annotation.tsx index b0925228a..a4d56b275 100644 --- a/src/components/Annotation.tsx +++ b/src/components/Annotation.tsx @@ -129,7 +129,7 @@ class Annotation extends React.Component { return ( } > {children} diff --git a/src/components/UserLocation.tsx b/src/components/UserLocation.tsx index 10c62b982..a655a1a61 100644 --- a/src/components/UserLocation.tsx +++ b/src/components/UserLocation.tsx @@ -3,6 +3,7 @@ import React, { ReactElement } from 'react'; import locationManager from '../modules/location/locationManager'; import { type Location } from '../modules/location/locationManager'; import { CircleLayerStyle } from '../Mapbox'; +import { OnPressEvent } from '../types/OnPressEvent'; import Annotation from './Annotation'; import CircleLayer from './CircleLayer'; @@ -92,7 +93,7 @@ type Props = { /** * Callback that is triggered on location icon press */ - onPress?: () => void; + onPress?: (event: OnPressEvent) => void; /** * Callback that is triggered on location update