我有两个文件定义如下
//googlemap.kt
@file:JsModule("@react-google-maps/api")
package tz.co.asoft.ui.react.maps.google
import react.Component
import react.RProps
import react.RState
external interface LatLng {
var lat: Double
var lng: Double
}
external class GoogleMap : Component<GoogleMap.Props, RState> {
interface Props : RProps {
var id: String
var center: LatLng
var zoom: Int
var mapContainerStyle: dynamic
}
override fun render(): dynamic
}
external class Marker : Component<Marker.Props, RState> {
interface Props : RProps {
var position: LatLng
}
override fun render(): dynamic
}
external class InfoWindow : Component<InfoWindow.Props, RState> {
interface Props : RProps {
var position: LatLng
}
override fun render(): dynamic
}
external class LoadScript : Component<LoadScript.Props, RState> {
interface Props : RProps {
var id: String
var googleMapsApiKey: String
}
override fun render(): dynamic
}
external class Polyline : Component<Polyline.Props, RState> {
interface Props : RProps {
var path: Array<LatLng>
var options: Options
var onLoad: (polyline:dynamic)->Unit
}
interface Options {
var strokeColor: String
var strokeOpacity: Double
var strokeWeight: Int
var fillColor: String
var fillOpacity: Double
var clickable: Boolean
var draggable: Boolean
var editable: Boolean
var visible: Boolean
var radius: Int
var paths: Array<LatLng>
var zIndex: Int
}
override fun render(): dynamic
}
我还有一个文件可以使它的 dsl 易于使用
//dsl.kt
package tz.co.asoft.ui.react.maps.google
import kotlinext.js.js
import kotlinext.js.jsObject
import react.RBuilder
import react.RHandler
private fun RBuilder.loadScript(handler: RHandler<LoadScript.Props> = {}) = child(LoadScript::class) {
attrs {
id = "google-maps-script"
googleMapsApiKey = "<your api key>"
}
handler()
}
private fun RBuilder.googleMap(handler: RHandler<GoogleMap.Props> = {}) = child(GoogleMap::class) {
attrs {
center = jsObject { lat = 0.0; lng = 0.0 }
zoom = 13
mapContainerStyle = js {
height = "90%"
width = "100%"
}
}
handler()
}
fun RBuilder.polyLine(paths: Array<LatLng>, handler: RHandler<Polyline.Props> = {}) = child(Polyline::class) {
attrs {
options = jsObject {
strokeColor = "#FF0000"
strokeOpacity = 0.8
strokeWeight = 2
fillColor = "#FF0000"
fillOpacity = 0.35
clickable = false
draggable = false
editable = false
visible = true
radius = 30000
zIndex = 1
}
options.paths = paths
}
attrs.path = paths
handler()
}
fun RBuilder.marker(handler: RHandler<Marker.Props> = {}) = child(Marker::class) {
attrs { position = jsObject { lat = 0.0; lng = 0.0 } }
handler()
}
fun RBuilder.infoWindow(handler: RHandler<InfoWindow.Props> = {}) = child(InfoWindow::class) {
attrs { position = jsObject { lat = 0.0; lng = 0.0 } }
handler()
}
fun RBuilder.map(handler: RHandler<GoogleMap.Props> = {}) = loadScript {
googleMap {
handler()
}
}
fun Array<Double>.toLatLng() = jsObject<LatLng> {
lat = get(0)
lng = get(1)
}
现在我可以在我的反应项目中使用 lib 作为
div{
map {
marker {
attrs { position = jsObject { lat = 0.0; lng = 0.0 } }
}
}
}
希望这可以帮助