Yasin Beyazlı
Yasin Beyazlı

Reputation: 37

Expo Native View - How to add children and set api key

I have created a native view with an Expo module. I want this view to take related native views as children instead of props. The documentation only provides examples with props. How can I achieve this?

Additionally, I change the API key information of the package I use in the native Kotlin code in init, I want to change it via prop, how do I do this?

this is what i want to do:

export default function App() {
  return (
    <MapKitView
      style={{flex: 1}}
      region={{latitude: 41.0288128, longitude: 29.04227, zoom: 17}}
      apiKey="API_KEY">
      <Marker latitude={41.0288128} longitude={29.04227} />
    </MapKitView>
  )
}

The final version of the native code is as follows.:

class YandexMapKitView(context: Context, appContext: AppContext) : ExpoView(context, appContext) {
    init {
        MapKitFactory.setApiKey("API_KEY")
        MapKitFactory.initialize(context)
    }

    internal val mapKitView = MapView(context).also {
        it.layoutParams = LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)
        MapKitFactory.getInstance().onStart()
        it.onStart()
        addView(it)
    }

    fun setInitialRegion(region: InitialRegion) {
        mapKitView.mapWindow.map.move(
            CameraPosition(Point(region.latitude, region.longitude), region.zoom, 0.0f, 0.0f)
        )
    }
}

class YandexMapKitViewModule : Module() {
  override fun definition() = ModuleDefinition {
    Name("YandexMapKitView")

    View(YandexMapKitView::class) {
      Prop("region") { view: YandexMapKitView, region: InitialRegion ->
        view.setInitialRegion(region)
      }
    }
  }
}

Upvotes: 0

Views: 32

Answers (0)

Related Questions