Commit a1ccfbfa authored by Qiu Xiang's avatar Qiu Xiang

重构 Overlay 关系及管理

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