Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sync Fork #3626

Closed
wants to merge 11 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -12,6 +13,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(
Expand Down Expand Up @@ -184,6 +186,70 @@ class NativeMapViewModule(context: ReactApplicationContext, val viewTagResolver:
)
}
}
override 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)
)
}
}

override fun getStyles(
viewRef: ViewRefTag?,
promise: Promise
) {
withMapViewOnUIThread(viewRef, promise) {

it.getStyles(
createCommandResponse(promise)
)
}
}

override fun setLayerProperties(
viewRef: ViewRefTag?,
layerId: String,
properties: ReadableMap,
promise: Promise
) {
withMapViewOnUIThread(viewRef, promise) {

it.setLayerProperties(
layerId,
properties,
createCommandResponse(promise)
)
}
}

override fun setLayerProperty(
viewRef: ViewRefTag?,
layerId: String,
property: String,
value: String,
promise: Promise
) {
withMapViewOnUIThread(viewRef, promise) {

it.setLayerProperty(
layerId,
property,
value,
createCommandResponse(promise)
)
}
}

companion object {
const val NAME = "RNMBXMapViewModule"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,15 @@ 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.*
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 <T> MutableList<T>.removeIf21(predicate: (T) -> Boolean): Boolean {
var removed = false
Expand All @@ -88,13 +90,52 @@ fun <T> MutableList<T>.removeIf21(predicate: (T) -> Boolean): Boolean {
}
return removed
}
fun convertReadableArrayToList(readableArray: ReadableArray): List<Value> {
val list = mutableListOf<Value>()
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<String, Value> {
val hashMap = HashMap<String, Value>()
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,
var margins: ReadableMap? =null,
var position: Int = -1
)

data class FeatureObject(
val id: String,
val feature: String
)

enum class MapGestureType {
Move,Scale,Rotate,Fling,Shove
}
Expand Down Expand Up @@ -1502,7 +1543,77 @@ open class RNMBXMapView(private val mContext: Context, var mManager: RNMBXMapVie
}
}

fun queryRenderedLayersInRect(rect: RectF?, filter: Expression?, layerIDs: List<String>?, 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<FeatureObject>()
for (i in features.value!!) {
val featureJson = try {
i.queriedFeature.feature.toJson()
} catch (e: Exception) {
Logger.e("queryRenderedFeaturesAtPoint", "Error converting feature to JSON", e)
continue
}
featuresList.add(FeatureObject(i.queriedFeature.sourceLayer ?: "unknown", featureJson))
}

response.success {
val gson = Gson();
val json = gson.toJson(featuresList);
it.putString("data", json)
}
} 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: ReadableMap, response: CommandResponse) {
val style = mMap!!.getStyle()
val propertiesValue = convertReadableMapToHashMap(properties)
try {
style?.setStyleLayerProperties(layerId, Value.valueOf(propertiesValue))
response.success {
it.putString("data", "Successfully set layer properties")
}
} catch (e: Exception) {
response.error("Error setting layer properties: ${e.message}")
}
}

fun setLayerProperty(layerId: String, property: String, value: String, response: CommandResponse) {
val style = mMap!!.getStyle()
try {
style?.setStyleLayerProperty(layerId,property,value as Value)
response.success {
it.putString("data", "Successfully set layer property")
}
} catch (e: Exception) {
response.error("Error setting layer property")
}
}
// endregion
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

/**
* This code was generated by [react-native-codegen](https://www.npmjs.com/package/react-native-codegen).
*
Expand All @@ -9,7 +8,6 @@
*
* @nolint
*/

package com.rnmapbox.rnmbx;

import com.facebook.proguard.annotations.DoNotStrip;
Expand All @@ -19,71 +17,91 @@
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;

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, ReadableMap 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, String value, Promise promise);

@ReactMethod
@DoNotStrip
public abstract void querySourceFeatures(@Nullable Integer viewRef, String sourceId, ReadableArray withFilter, ReadableArray withSourceLayerIDs, Promise promise);
}
1 change: 1 addition & 0 deletions example/rnmapbox.web 2.symlink
Loading
Loading