Commit a1ccfbfa authored by Qiu Xiang's avatar Qiu Xiang

重构 Overlay 关系及管理

parent 3c201a52
......@@ -19,7 +19,7 @@ class AMap3DPackage : ReactPackage {
AMapViewManager(),
AMapMarkerManager(),
AMapInfoWindowManager(),
AMapOverlayManager(),
AMapMarkerIconManager(),
AMapPolylineManager(),
AMapPolygonManager(),
AMapCircleManager(),
......
......@@ -8,9 +8,8 @@ import com.amap.api.maps.model.Circle
import com.amap.api.maps.model.CircleOptions
import com.facebook.react.views.view.ReactViewGroup
class AMapCircle(context: Context) : ReactViewGroup(context) {
var circle: Circle? = null
private set
class AMapCircle(context: Context) : ReactViewGroup(context), AMapOverlay {
private var circle: Circle? = null
var center: LatLng? = null
set(value) {
......@@ -48,7 +47,7 @@ class AMapCircle(context: Context) : ReactViewGroup(context) {
circle?.zIndex = value
}
fun addToMap(map: AMap) {
override fun add(map: AMap) {
circle = map.addCircle(CircleOptions()
.center(center)
.radius(radius)
......@@ -57,4 +56,8 @@ class AMapCircle(context: Context) : ReactViewGroup(context) {
.fillColor(fillColor)
.zIndex(zIndex))
}
override fun remove() {
circle?.remove()
}
}
......@@ -9,22 +9,20 @@ import com.amap.api.maps.model.TileOverlayOptions
import com.facebook.react.bridge.ReadableArray
import com.facebook.react.views.view.ReactViewGroup
class AMapHeatMap(context: Context) : ReactViewGroup(context) {
var overlay: TileOverlay? = null
private set
class AMapHeatMap(context: Context) : ReactViewGroup(context), AMapOverlay {
private var overlay: TileOverlay? = null
private var coordinates: ArrayList<LatLng> = ArrayList()
var opacity: Double = 0.6
var radius: Int = 12
private var coordinates: ArrayList<LatLng> = ArrayList()
fun setCoordinates(coordinates: ReadableArray) {
this.coordinates = ArrayList((0 until coordinates.size())
.map { coordinates.getMap(it) }
.map { LatLng(it.getDouble("latitude"), it.getDouble("longitude")) })
}
fun addToMap(map: AMap) {
override fun add(map: AMap) {
overlay = map.addTileOverlay(TileOverlayOptions().tileProvider(
HeatmapTileProvider.Builder()
.data(coordinates)
......@@ -32,4 +30,8 @@ class AMapHeatMap(context: Context) : ReactViewGroup(context) {
.transparency(opacity)
.build()))
}
override fun remove() {
overlay?.remove()
}
}
\ No newline at end of file
......@@ -3,7 +3,6 @@ package cn.qiuxiang.react.amap3d.maps
import com.facebook.react.bridge.ReadableArray
import com.facebook.react.uimanager.SimpleViewManager
import com.facebook.react.uimanager.ThemedReactContext
import com.facebook.react.uimanager.ViewGroupManager
import com.facebook.react.uimanager.annotations.ReactProp
@Suppress("unused")
......
......@@ -7,7 +7,7 @@ import com.amap.api.maps.AMap
import com.amap.api.maps.model.*
import com.facebook.react.views.view.ReactViewGroup
class AMapMarker(context: Context) : ReactViewGroup(context) {
class AMapMarker(context: Context) : ReactViewGroup(context), AMapOverlay {
companion object {
private val COLORS = mapOf(
"AZURE" to BitmapDescriptorFactory.HUE_AZURE,
......@@ -92,7 +92,7 @@ class AMapMarker(context: Context) : ReactViewGroup(context) {
}
}
var customIcon: AMapOverlay? = null
var customIcon: AMapMarkerIcon? = null
set(value) {
field = value
value?.addOnLayoutChangeListener { _, _, _, _, _, _, _, _, _ -> updateCustomIcon() }
......@@ -100,7 +100,7 @@ class AMapMarker(context: Context) : ReactViewGroup(context) {
private var bitmapDescriptor: BitmapDescriptor? = null
fun addToMap(map: AMap) {
override fun add(map: AMap) {
marker = map.addMarker(MarkerOptions()
.setFlat(flat)
.icon(bitmapDescriptor)
......@@ -121,6 +121,10 @@ class AMapMarker(context: Context) : ReactViewGroup(context) {
marker?.isClickable = this.clickable_
}
override fun remove() {
marker?.destroy()
}
fun setIconColor(icon: String) {
bitmapDescriptor = COLORS[icon.toUpperCase()]?.let {
BitmapDescriptorFactory.defaultMarker(it)
......
package cn.qiuxiang.react.amap3d.maps
import android.content.Context
import com.facebook.react.views.view.ReactViewGroup
class AMapMarkerIcon(context: Context) : ReactViewGroup(context)
......@@ -3,12 +3,12 @@ package cn.qiuxiang.react.amap3d.maps
import com.facebook.react.uimanager.ThemedReactContext
import com.facebook.react.uimanager.ViewGroupManager
class AMapOverlayManager : ViewGroupManager<AMapOverlay>() {
class AMapMarkerIconManager : ViewGroupManager<AMapMarkerIcon>() {
override fun getName(): String {
return "AMapOverlay"
}
override fun createViewInstance(reactContext: ThemedReactContext): AMapOverlay {
return AMapOverlay(reactContext)
override fun createViewInstance(reactContext: ThemedReactContext): AMapMarkerIcon {
return AMapMarkerIcon(reactContext)
}
}
......@@ -21,7 +21,7 @@ internal class AMapMarkerManager : ViewGroupManager<AMapMarker>() {
override fun addView(marker: AMapMarker, view: View, index: Int) {
when (view) {
is AMapOverlay -> marker.customIcon = view
is AMapMarkerIcon -> marker.customIcon = view
is AMapInfoWindow -> marker.infoWindow = view
}
}
......
package cn.qiuxiang.react.amap3d.maps
import android.content.Context
import com.facebook.react.views.view.ReactViewGroup
import com.amap.api.maps.AMap
class AMapOverlay(context: Context) : ReactViewGroup(context) {
interface AMapOverlay {
fun add(map: AMap)
fun remove()
}
\ No newline at end of file
......@@ -9,9 +9,9 @@ import com.amap.api.maps.model.PolygonOptions
import com.facebook.react.bridge.ReadableArray
import com.facebook.react.views.view.ReactViewGroup
class AMapPolygon(context: Context) : ReactViewGroup(context) {
var polygon: Polygon? = null
private set
class AMapPolygon(context: Context) : ReactViewGroup(context), AMapOverlay {
private var polygon: Polygon? = null
private var coordinates: ArrayList<LatLng> = ArrayList()
var strokeWidth: Float = 1f
set(value) {
......@@ -37,8 +37,6 @@ class AMapPolygon(context: Context) : ReactViewGroup(context) {
polygon?.zIndex = value
}
private var coordinates: ArrayList<LatLng> = ArrayList()
fun setCoordinates(coordinates: ReadableArray) {
this.coordinates = ArrayList((0 until coordinates.size())
.map { coordinates.getMap(it) }
......@@ -47,7 +45,7 @@ class AMapPolygon(context: Context) : ReactViewGroup(context) {
polygon?.points = this.coordinates
}
fun addToMap(map: AMap) {
override fun add(map: AMap) {
polygon = map.addPolygon(PolygonOptions()
.addAll(coordinates)
.strokeColor(strokeColor)
......@@ -55,4 +53,8 @@ class AMapPolygon(context: Context) : ReactViewGroup(context) {
.fillColor(fillColor)
.zIndex(zIndex))
}
override fun remove() {
polygon?.remove()
}
}
......@@ -9,10 +9,13 @@ import com.amap.api.maps.model.PolylineOptions
import com.facebook.react.bridge.ReadableArray
import com.facebook.react.views.view.ReactViewGroup
class AMapPolyline(context: Context) : ReactViewGroup(context) {
class AMapPolyline(context: Context) : ReactViewGroup(context), AMapOverlay {
var polyline: Polyline? = null
private set
private var coordinates: ArrayList<LatLng> = ArrayList()
private var colors: ArrayList<Int> = ArrayList()
var width: Float = 1f
set(value) {
field = value
......@@ -45,9 +48,6 @@ class AMapPolyline(context: Context) : ReactViewGroup(context) {
var gradient: Boolean = false
private var coordinates: ArrayList<LatLng> = ArrayList()
private var colors: ArrayList<Int> = ArrayList()
fun setCoordinates(coordinates: ReadableArray) {
this.coordinates = ArrayList((0 until coordinates.size())
.map { coordinates.getMap(it) }
......@@ -57,10 +57,10 @@ class AMapPolyline(context: Context) : ReactViewGroup(context) {
}
fun setColors(colors: ReadableArray) {
this.colors = ArrayList((0..colors.size() - 1).map { colors.getInt(it) })
this.colors = ArrayList((0 until colors.size()).map { colors.getInt(it) })
}
fun addToMap(map: AMap) {
override fun add(map: AMap) {
polyline = map.addPolyline(PolylineOptions()
.addAll(coordinates)
.color(color)
......@@ -71,4 +71,8 @@ class AMapPolyline(context: Context) : ReactViewGroup(context) {
.setDottedLine(dashed)
.zIndex(zIndex))
}
override fun remove() {
polyline?.remove()
}
}
package cn.qiuxiang.react.amap3d.maps
import android.annotation.SuppressLint
import android.content.Context
import android.view.View
import com.amap.api.maps.AMap
......@@ -18,8 +17,6 @@ class AMapView(context: Context) : TextureMapView(context) {
private val eventEmitter: RCTEventEmitter = (context as ThemedReactContext).getJSModule(RCTEventEmitter::class.java)
private val markers = HashMap<String, AMapMarker>()
private val polylines = HashMap<String, AMapPolyline>()
private val polygons = HashMap<String, AMapPolygon>()
private val circles = HashMap<String, AMapCircle>()
private var locationType = MyLocationStyle.LOCATION_TYPE_FOLLOW_NO_CENTER
private val locationStyle by lazy {
val locationStyle = MyLocationStyle()
......@@ -79,7 +76,7 @@ class AMapView(context: Context) : TextureMapView(context) {
}
})
map.setOnCameraChangeListener(object: AMap.OnCameraChangeListener {
map.setOnCameraChangeListener(object : AMap.OnCameraChangeListener {
override fun onCameraChangeFinish(position: CameraPosition?) {
emitCameraChangeEvent("onStatusChangeComplete", position)
}
......@@ -118,59 +115,35 @@ class AMapView(context: Context) : TextureMapView(context) {
}
}
fun addMarker(marker: AMapMarker) {
marker.addToMap(map)
markers.put(marker.marker?.id!!, marker)
}
fun addPolyline(polyline: AMapPolyline) {
polyline.addToMap(map)
polylines.put(polyline.polyline?.id!!, polyline)
fun emit(id: Int?, name: String, data: WritableMap = Arguments.createMap()) {
id?.let { eventEmitter.receiveEvent(it, name, data) }
}
fun addPolygon(polygon: AMapPolygon) {
polygon.addToMap(map)
polygons.put(polygon.polygon?.id!!, polygon)
fun add(child: View) {
if (child is AMapOverlay) {
child.add(map)
if (child is AMapMarker) {
markers.put(child.marker?.id!!, child)
}
fun addCircle(circle: AMapCircle) {
circle.addToMap(map)
circles.put(circle.circle?.id!!, circle)
if (child is AMapPolyline) {
polylines.put(child.polyline?.id!!, child)
}
fun addHeatMap(heatMap: AMapHeatMap) {
heatMap.addToMap(map)
}
fun emit(id: Int?, name: String, data: WritableMap = Arguments.createMap()) {
id?.let { eventEmitter.receiveEvent(it, name, data) }
}
fun remove(child: View) {
when (child) {
is AMapMarker -> {
if (child is AMapOverlay) {
child.remove()
if (child is AMapMarker) {
markers.remove(child.marker?.id)
child.marker?.destroy()
}
is AMapPolyline -> {
if (child is AMapPolyline) {
polylines.remove(child.polyline?.id)
child.polyline?.remove()
}
is AMapPolygon -> {
polygons.remove(child.polygon?.id)
child.polygon?.remove()
}
is AMapCircle -> {
polygons.remove(child.circle?.id)
child.circle?.remove()
}
is AMapHeatMap -> {
child.overlay?.remove()
}
}
}
private val animateCallback = object: AMap.CancelableCallback {
private val animateCallback = object : AMap.CancelableCallback {
override fun onCancel() {
emit(id, "onAnimateCancel")
}
......
......@@ -4,7 +4,6 @@ import android.view.View
import com.amap.api.maps.AMap
import com.amap.api.maps.CameraUpdateFactory
import com.amap.api.maps.model.LatLng
import com.amap.api.maps.model.MyLocationStyle
import com.facebook.react.bridge.ReadableArray
import com.facebook.react.bridge.ReadableMap
import com.facebook.react.common.MapBuilder
......@@ -42,14 +41,8 @@ internal class AMapViewManager : ViewGroupManager<AMapView>() {
}
override fun addView(mapView: AMapView, child: View, index: Int) {
mapView.add(child)
super.addView(mapView, child, index)
when (child) {
is AMapMarker -> mapView.addMarker(child)
is AMapPolyline -> mapView.addPolyline(child)
is AMapPolygon -> mapView.addPolygon(child)
is AMapCircle -> mapView.addCircle(child)
is AMapHeatMap -> mapView.addHeatMap(child)
}
}
override fun removeViewAt(parent: AMapView, index: Int) {
......
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