Commit 7e497853 authored by 7c00's avatar 7c00

优化 kotlin 代码

parent 6eb2f429
......@@ -2,7 +2,7 @@ apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'
buildscript {
ext.kotlin_version = '1.2.21'
ext.kotlin_version = '1.2.41'
repositories {
jcenter()
......
package cn.qiuxiang.react.amap3d
import android.content.res.Resources
import com.amap.api.maps.model.LatLng
import com.amap.api.maps.model.LatLngBounds
import com.facebook.react.bridge.Arguments
import com.facebook.react.bridge.ReadableArray
import com.facebook.react.bridge.ReadableMap
import com.facebook.react.bridge.WritableMap
val Float.toPx: Int
get() = (this * Resources.getSystem().displayMetrics.density).toInt()
fun Float.toPx(): Int {
return (this * Resources.getSystem().displayMetrics.density).toInt()
}
fun ReadableMap.toLatLng(): LatLng {
return LatLng(this.getDouble("latitude"), this.getDouble("longitude"))
}
fun ReadableArray.toLatLngList(): ArrayList<LatLng> {
return ArrayList((0..(this.size() - 1)).map { this.getMap(it).toLatLng() })
}
fun LatLng.toWritableMap(): WritableMap {
val map = Arguments.createMap()
map.putDouble("latitude", this.latitude)
map.putDouble("longitude", this.longitude)
return map
}
fun ReadableMap.toLatLngBounds(): LatLngBounds {
val latitude = this.getDouble("latitude")
val longitude = this.getDouble("longitude")
val latitudeDelta = this.getDouble("latitudeDelta")
val longitudeDelta = this.getDouble("longitudeDelta")
return LatLngBounds(
LatLng(latitude - latitudeDelta / 2, longitude - longitudeDelta / 2),
LatLng(latitude + latitudeDelta / 2, longitude + longitudeDelta / 2)
)
}
\ No newline at end of file
package cn.qiuxiang.react.amap3d.maps
import cn.qiuxiang.react.amap3d.toLatLng
import cn.qiuxiang.react.amap3d.toPx
import com.amap.api.maps.model.LatLng
import com.facebook.react.bridge.ReadableMap
import com.facebook.react.uimanager.SimpleViewManager
import com.facebook.react.uimanager.ThemedReactContext
......@@ -19,9 +19,7 @@ internal class AMapCircleManager : SimpleViewManager<AMapCircle>() {
@ReactProp(name = "coordinate")
fun setCoordinate(circle: AMapCircle, coordinate: ReadableMap) {
circle.center = LatLng(
coordinate.getDouble("latitude"),
coordinate.getDouble("longitude"))
circle.center = coordinate.toLatLng()
}
@ReactProp(name = "radius")
......@@ -41,11 +39,11 @@ internal class AMapCircleManager : SimpleViewManager<AMapCircle>() {
@ReactProp(name = "strokeWidth")
fun setStrokeWidth(circle: AMapCircle, strokeWidth: Float) {
circle.strokeWidth = strokeWidth.toPx.toFloat()
circle.strokeWidth = strokeWidth.toPx().toFloat()
}
@ReactProp(name = "zIndex")
fun setZIndex_(circle: AMapCircle, zIndex: Float) {
fun setZIndez(circle: AMapCircle, zIndex: Float) {
circle.zIndex = zIndex
}
}
package cn.qiuxiang.react.amap3d.maps
import android.content.Context
import cn.qiuxiang.react.amap3d.toLatLngList
import com.amap.api.maps.AMap
import com.amap.api.maps.model.HeatmapTileProvider
import com.amap.api.maps.model.LatLng
......@@ -17,9 +18,7 @@ class AMapHeatMap(context: Context) : ReactViewGroup(context), AMapOverlay {
var radius: Int = 12
fun setCoordinates(coordinates: ReadableArray) {
this.coordinates = ArrayList((0 until coordinates.size())
.map { coordinates.getMap(it) }
.map { LatLng(it.getDouble("latitude"), it.getDouble("longitude")) })
this.coordinates = coordinates.toLatLngList()
}
override fun add(map: AMap) {
......
package cn.qiuxiang.react.amap3d.maps
import com.facebook.react.uimanager.LayoutShadowNode
import com.facebook.react.uimanager.ThemedReactContext
import com.facebook.react.uimanager.ViewGroupManager
......@@ -12,8 +11,4 @@ class AMapInfoWindowManager : ViewGroupManager<AMapInfoWindow>() {
override fun createViewInstance(reactContext: ThemedReactContext): AMapInfoWindow {
return AMapInfoWindow(reactContext)
}
override fun createShadowNodeInstance(): LayoutShadowNode {
return super.createShadowNodeInstance()
}
}
\ No newline at end of file
......@@ -159,8 +159,8 @@ class AMapMarker(context: Context) : ReactViewGroup(context), AMapOverlay {
fun lockToScreen(args: ReadableArray?) {
if (args != null) {
val x = args.getDouble(0).toFloat().toPx
val y = args.getDouble(1).toFloat().toPx
val x = args.getDouble(0).toFloat().toPx()
val y = args.getDouble(1).toFloat().toPx()
marker?.setPositionByPixels(x, y)
}
}
......
package cn.qiuxiang.react.amap3d.maps
import android.view.View
import cn.qiuxiang.react.amap3d.toLatLng
import com.amap.api.maps.model.LatLng
import com.facebook.react.bridge.ReadableArray
import com.facebook.react.bridge.ReadableMap
......@@ -70,9 +71,7 @@ internal class AMapMarkerManager : ViewGroupManager<AMapMarker>() {
@ReactProp(name = "coordinate")
fun setCoordinate(view: AMapMarker, coordinate: ReadableMap) {
view.position = LatLng(
coordinate.getDouble("latitude"),
coordinate.getDouble("longitude"))
view.position = coordinate.toLatLng()
}
@ReactProp(name = "flat")
......@@ -116,14 +115,12 @@ internal class AMapMarkerManager : ViewGroupManager<AMapMarker>() {
}
@ReactProp(name = "zIndex")
fun setZInex(marker: AMapMarker, zIndex: Float) {
fun setZIndez(marker: AMapMarker, zIndex: Float) {
marker.zIndex = zIndex
}
@ReactProp(name = "anchor")
fun setAnchor(view: AMapMarker, coordinate: ReadableMap) {
view.setAnchor(
coordinate.getDouble("x"),
coordinate.getDouble("y"))
view.setAnchor(coordinate.getDouble("x"), coordinate.getDouble("y"))
}
}
package cn.qiuxiang.react.amap3d.maps
import android.content.Context
import cn.qiuxiang.react.amap3d.toLatLng
import com.amap.api.maps.AMap
import com.amap.api.maps.model.*
import com.facebook.react.bridge.ReadableArray
......@@ -15,9 +16,7 @@ class AMapMultiPoint(context: Context) : ReactViewGroup(context), AMapOverlay {
items = ArrayList((0 until points.size())
.map {
val data = points.getMap(it)
val item = MultiPointItem(LatLng(
data.getDouble("latitude"),
data.getDouble("longitude")))
val item = MultiPointItem(data.toLatLng())
if (data.hasKey("title")) {
item.title = data.getString("title")
}
......
......@@ -2,6 +2,7 @@ package cn.qiuxiang.react.amap3d.maps
import android.content.Context
import android.graphics.Color
import cn.qiuxiang.react.amap3d.toLatLngList
import com.amap.api.maps.AMap
import com.amap.api.maps.model.LatLng
import com.amap.api.maps.model.Polygon
......@@ -38,10 +39,7 @@ class AMapPolygon(context: Context) : ReactViewGroup(context), AMapOverlay {
}
fun setCoordinates(coordinates: ReadableArray) {
this.coordinates = ArrayList((0 until coordinates.size())
.map { coordinates.getMap(it) }
.map { LatLng(it.getDouble("latitude"), it.getDouble("longitude")) })
this.coordinates = coordinates.toLatLngList()
polygon?.points = this.coordinates
}
......
......@@ -33,7 +33,7 @@ internal class AMapPolygonManager : SimpleViewManager<AMapPolygon>() {
@ReactProp(name = "strokeWidth")
fun setStrokeWidth(polygon: AMapPolygon, strokeWidth: Float) {
polygon.strokeWidth = strokeWidth.toPx.toFloat()
polygon.strokeWidth = strokeWidth.toPx().toFloat()
}
@ReactProp(name = "zIndex")
......
......@@ -2,6 +2,7 @@ package cn.qiuxiang.react.amap3d.maps
import android.content.Context
import android.graphics.Color
import cn.qiuxiang.react.amap3d.toLatLngList
import com.amap.api.maps.AMap
import com.amap.api.maps.model.LatLng
import com.amap.api.maps.model.Polyline
......@@ -49,10 +50,7 @@ class AMapPolyline(context: Context) : ReactViewGroup(context), AMapOverlay {
var gradient: Boolean = false
fun setCoordinates(coordinates: ReadableArray) {
this.coordinates = ArrayList((0 until coordinates.size())
.map { coordinates.getMap(it) }
.map { LatLng(it.getDouble("latitude"), it.getDouble("longitude")) })
this.coordinates = coordinates.toLatLngList()
polyline?.points = this.coordinates
}
......
......@@ -37,7 +37,7 @@ internal class AMapPolylineManager : SimpleViewManager<AMapPolyline>() {
@ReactProp(name = "width")
fun setWidth(polyline: AMapPolyline, width: Float) {
polyline.width = width.toPx.toFloat()
polyline.width = width.toPx().toFloat()
}
@ReactProp(name = "zIndex")
......
......@@ -2,10 +2,16 @@ package cn.qiuxiang.react.amap3d.maps
import android.content.Context
import android.view.View
import cn.qiuxiang.react.amap3d.toLatLng
import cn.qiuxiang.react.amap3d.toLatLngBounds
import cn.qiuxiang.react.amap3d.toWritableMap
import com.amap.api.maps.AMap
import com.amap.api.maps.CameraUpdateFactory
import com.amap.api.maps.TextureMapView
import com.amap.api.maps.model.*
import com.amap.api.maps.model.BitmapDescriptorFactory
import com.amap.api.maps.model.CameraPosition
import com.amap.api.maps.model.Marker
import com.amap.api.maps.model.MyLocationStyle
import com.facebook.react.bridge.Arguments
import com.facebook.react.bridge.ReadableArray
import com.facebook.react.bridge.ReadableMap
......@@ -31,17 +37,11 @@ class AMapView(context: Context) : TextureMapView(context) {
marker.active = false
}
val event = Arguments.createMap()
event.putDouble("latitude", latLng.latitude)
event.putDouble("longitude", latLng.longitude)
emit(id, "onPress", event)
emit(id, "onPress", latLng.toWritableMap())
}
map.setOnMapLongClickListener { latLng ->
val event = Arguments.createMap()
event.putDouble("latitude", latLng.latitude)
event.putDouble("longitude", latLng.longitude)
emit(id, "onLongPress", event)
emit(id, "onLongPress", latLng.toWritableMap())
}
map.setOnMyLocationChangeListener { location ->
......@@ -73,11 +73,7 @@ class AMapView(context: Context) : TextureMapView(context) {
}
override fun onMarkerDragEnd(marker: Marker) {
val position = marker.position
val data = Arguments.createMap()
data.putDouble("latitude", position.latitude)
data.putDouble("longitude", position.longitude)
emit(markers[marker.id]?.id, "onDragEnd", data)
emit(markers[marker.id]?.id, "onDragEnd", marker.position.toWritableMap())
}
})
......@@ -112,12 +108,10 @@ class AMapView(context: Context) : TextureMapView(context) {
fun emitCameraChangeEvent(event: String, position: CameraPosition?) {
position?.let {
val data = Arguments.createMap()
val data = it.target.toWritableMap()
data.putDouble("zoomLevel", it.zoom.toDouble())
data.putDouble("tilt", it.tilt.toDouble())
data.putDouble("rotation", it.bearing.toDouble())
data.putDouble("latitude", it.target.latitude)
data.putDouble("longitude", it.target.longitude)
if (event == "onStatusChangeComplete") {
val southwest = map.projection.visibleRegion.latLngBounds.southwest
val northeast = map.projection.visibleRegion.latLngBounds.northeast
......@@ -136,10 +130,10 @@ class AMapView(context: Context) : TextureMapView(context) {
if (child is AMapOverlay) {
child.add(map)
if (child is AMapMarker) {
markers.put(child.marker?.id!!, child)
markers[child.marker?.id!!] = child
}
if (child is AMapPolyline) {
lines.put(child.polyline?.id!!, child)
lines[child.polyline?.id!!] = child
}
}
}
......@@ -177,8 +171,7 @@ class AMapView(context: Context) : TextureMapView(context) {
var rotation = currentCameraPosition.bearing
if (target.hasKey("coordinate")) {
val json = target.getMap("coordinate")
coordinate = LatLng(json.getDouble("latitude"), json.getDouble("longitude"))
coordinate = target.getMap("coordinate").toLatLng()
}
if (target.hasKey("zoomLevel")) {
......@@ -199,11 +192,11 @@ class AMapView(context: Context) : TextureMapView(context) {
}
fun setRegion(region: ReadableMap) {
map.moveCamera(CameraUpdateFactory.newLatLngBounds(latLngBoundsFromReadableMap(region), 0))
map.moveCamera(CameraUpdateFactory.newLatLngBounds(region.toLatLngBounds(), 0))
}
fun setLimitRegion(region: ReadableMap) {
map.setMapStatusLimits(latLngBoundsFromReadableMap(region))
map.setMapStatusLimits(region.toLatLngBounds())
}
fun setLocationEnabled(enabled: Boolean) {
......@@ -215,17 +208,6 @@ class AMapView(context: Context) : TextureMapView(context) {
locationStyle.interval(interval)
}
private fun latLngBoundsFromReadableMap(region: ReadableMap): LatLngBounds {
val latitude = region.getDouble("latitude")
val longitude = region.getDouble("longitude")
val latitudeDelta = region.getDouble("latitudeDelta")
val longitudeDelta = region.getDouble("longitudeDelta")
return LatLngBounds(
LatLng(latitude - latitudeDelta / 2, longitude - longitudeDelta / 2),
LatLng(latitude + latitudeDelta / 2, longitude + longitudeDelta / 2)
)
}
fun setLocationStyle(style: ReadableMap) {
if (style.hasKey("fillColor")) {
locationStyle.radiusFillColor(style.getInt("fillColor"))
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment