Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# Environment
example/.env

# OSX
#
.DS_Store
Expand Down Expand Up @@ -69,6 +72,14 @@ android/keystores/debug.keystore
!.yarn/sdks
!.yarn/versions

# Nested workspace lockfiles: this is a single Yarn Berry workspace, only the
# root lockfile is used. A lockfile inside example/ makes Yarn treat it as its
# own project and breaks `yarn run` there (missing install-state on CI).
example/yarn.lock
example/.yarn/
bun.lock
example/bun.lock

# Expo
.expo/

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import com.facebook.react.viewmanagers.ClusteredYamapLiteViewManagerDelegate
import android.view.View
import android.graphics.Color
import com.yamaplite.components.ClusteredYamapLiteView
import com.yamaplite.utils.PointParser

class ClusteredYamapLiteViewManager : ViewGroupManager<ClusteredYamapLiteView>(), ClusteredYamapLiteViewManagerInterface<ClusteredYamapLiteView> {
private val mDelegate: ViewManagerDelegate<ClusteredYamapLiteView>
Expand All @@ -26,6 +27,11 @@ class ClusteredYamapLiteViewManager : ViewGroupManager<ClusteredYamapLiteView>()
return ClusteredYamapLiteView(context)
}

override fun onDropViewInstance(view: ClusteredYamapLiteView) {
view.onDropViewInstance()
super.onDropViewInstance(view)
}

override fun addView(parent: ClusteredYamapLiteView, child: View, index: Int) {
parent.addChild(child, index)
super.addView(parent, child, index)
Expand All @@ -40,8 +46,11 @@ class ClusteredYamapLiteViewManager : ViewGroupManager<ClusteredYamapLiteView>()
}

override fun removeViewAt(parent: ClusteredYamapLiteView, index: Int) {
parent.removeChild(index)
super.removeViewAt(parent, index)
val childCount = parent.getReactChildCount()
if (index >= 0 && index < childCount) {
parent.removeChild(index)
super.removeViewAt(parent, index)
}
}

override fun getName() = "ClusteredYamapLiteView"
Expand Down Expand Up @@ -157,7 +166,7 @@ class ClusteredYamapLiteViewManager : ViewGroupManager<ClusteredYamapLiteView>()

@ReactProp(name = "clusteredMarkers")
override fun setClusteredMarkers(view: ClusteredYamapLiteView, points: ReadableArray?) {
view.setClusteredMarkers(points?.toArrayList()?.filterNotNull() as ArrayList<Any>)
view.setClusteredMarkers(PointParser.parsePoints(points))
}

@ReactProp(name = "clusterColor")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,90 +17,74 @@ import com.yandex.runtime.image.ImageProvider
import kotlin.math.abs
import kotlin.math.sqrt

class ClusteredYamapLiteView(context: Context) : YamapLiteView(context), ClusterListener,
ClusterTapListener {
class ClusteredYamapLiteView(context: Context) :
YamapLiteView(context), ClusterListener, ClusterTapListener {
private var clusterCollection: ClusterizedPlacemarkCollection
private var clusterColor = Color.RED
private val placemarksMap: HashMap<String?, PlacemarkMapObject?> = HashMap<String?, PlacemarkMapObject?>()
private val placemarksMap: HashMap<String?, PlacemarkMapObject?> =
HashMap<String?, PlacemarkMapObject?>()
private var pointsList = ArrayList<Point>()

init {
clusterCollection = mapView.mapWindow.map.mapObjects.addClusterizedPlacemarkCollection(this)
}

fun setClusteredMarkers(points: ArrayList<Any>) {
clusterCollection.clear()
placemarksMap.clear()
val pt = ArrayList<Point>()
for (i in points.indices) {
@Suppress("UNCHECKED_CAST")
val point = points[i] as HashMap<String, Double>
val lat = point["lat"] as Double
val lon = point["lon"] as Double
pt.add(Point(lat, lon))
}
val placemarks = clusterCollection.addPlacemarks(pt, TextImageProvider(""), IconStyle())
pointsList = pt
val childCount = super.getReactChildCount()
for (i in placemarks.indices) {
val placemark = placemarks[i]
val geometry = placemark.geometry
placemarksMap["${geometry.latitude}${geometry.longitude}"] = placemark
if (i < childCount) {
val child: Any? = super.getReactChildAt(i)
if (child != null && child is YamapLiteMarkerView) {
child.setMarkerMapObject(placemark)
}
}
}
clusterCollection.clusterPlacemarks(50.0, 12)
fun setClusteredMarkers(points: List<Point>) {
pointsList = ArrayList(points)
rebuildClusterPlacemarks()
}

fun setClusterColor(color: Int) {
clusterColor = color
updateUserMarkersColor()
rebuildClusterPlacemarks()
}

private fun updateUserMarkersColor() {
private fun rebuildClusterPlacemarks() {
clusterCollection.clear()
val placemarks = clusterCollection.addPlacemarks(
pointsList,
TextImageProvider(pointsList.size.toString()),
IconStyle()
)
placemarksMap.clear()
val placemarks = clusterCollection.addPlacemarks(pointsList, TextImageProvider(""), IconStyle())
val childCount = super.getReactChildCount()
for (i in placemarks.indices) {
val placemark = placemarks[i]
val geometry = placemark.geometry
placemarksMap["${geometry.latitude}${geometry.longitude}"] = placemark
placemarksMap[geometryKey(geometry.latitude, geometry.longitude)] = placemark
if (i < childCount) {
val child: Any? = super.getReactChildAt(i)
if (child != null && child is YamapLiteMarkerView) {
val child = super.getReactChildAt(i)
if (child is YamapLiteMarkerView) {
child.setMarkerMapObject(placemark)
}
}
}
clusterCollection.clusterPlacemarks(50.0, 12)
}

private fun geometryKey(lat: Double, lon: Double): String = "$lat,$lon"

fun addChild(child: View, index: Int) {
super.addReactChild(child, index)
if (child is YamapLiteMarkerView) {
val placemark = placemarksMap["" + child.latitude + child.longitude]
addReactChildToList(child, index)
val placemark = placemarksMap[geometryKey(child.latitude, child.longitude)]
if (placemark != null) {
child.setMarkerMapObject(placemark)
}
} else {
super.addReactChild(child, index)
}
}

fun removeChild(index: Int) {
val child = getChildAt(index)
if (index < 0 || index >= super.getReactChildCount()) {
return
}
val child = super.getReactChildAt(index)
if (child is YamapLiteMarkerView) {
val mapObject = child.placemark
if (mapObject != null && mapObject.isValid) {
clusterCollection.remove(mapObject)
placemarksMap.remove("" + child.latitude + child.longitude)
val clusterPlacemark = placemarksMap.remove(geometryKey(child.latitude, child.longitude))
if (clusterPlacemark != null && clusterPlacemark.isValid) {
clusterCollection.remove(clusterPlacemark)
}
removeReactChildFromListAt(index)
} else {
super.removeReactChildAt(index)
}
}

Expand All @@ -112,7 +96,7 @@ ClusterTapListener {
override fun onClusterTap(cluster: Cluster): Boolean {
val points = ArrayList<Point?>()
for (placemark in cluster.placemarks) {
points.add(placemark.geometry)
points.add(placemark.geometry)
}
super.fitMarkers(points)
return true
Expand All @@ -129,7 +113,7 @@ ClusterTapListener {
val scaledFontSize = Companion.FONT_SIZE * density
val scaledMarginSize = Companion.MARGIN_SIZE * density
val scaledStrokeSize = Companion.STROKE_SIZE * density

val textPaint = Paint()
textPaint.textSize = scaledFontSize
textPaint.textAlign = Paint.Align.CENTER
Expand All @@ -152,25 +136,25 @@ ClusterTapListener {
backgroundPaint.isAntiAlias = true
backgroundPaint.color = clusterColor
canvas.drawCircle(
(width / 2).toFloat(),
(width / 2).toFloat(),
externalRadius,
backgroundPaint
(width / 2).toFloat(),
(width / 2).toFloat(),
externalRadius,
backgroundPaint
)

backgroundPaint.color = Color.WHITE
canvas.drawCircle(
(width / 2).toFloat(),
(width / 2).toFloat(),
internalRadius,
backgroundPaint
(width / 2).toFloat(),
(width / 2).toFloat(),
internalRadius,
backgroundPaint
)

canvas.drawText(
text,
(width / 2).toFloat(),
width / 2 - (textMetrics.ascent + textMetrics.descent) / 2,
textPaint
text,
(width / 2).toFloat(),
width / 2 - (textMetrics.ascent + textMetrics.descent) / 2,
textPaint
)

return bitmap
Expand All @@ -181,6 +165,5 @@ ClusterTapListener {
private const val FONT_SIZE = 45f
private const val MARGIN_SIZE = 9f
private const val STROKE_SIZE = 9f
private val CLUSTER_COLOR = Color.parseColor("#000000")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,15 @@ package com.yamaplite.components
import android.content.Context
import android.graphics.Color
import android.view.ViewGroup
import com.facebook.react.bridge.Arguments
import com.facebook.react.bridge.ReactContext
import com.facebook.react.bridge.WritableMap
import com.facebook.react.uimanager.UIManagerHelper
import com.yamaplite.events.PressEvent
import com.yandex.mapkit.geometry.Circle
import com.yandex.mapkit.geometry.Point
import com.yandex.mapkit.map.CircleMapObject
import com.yandex.mapkit.map.MapObject
import com.yandex.mapkit.map.MapObjectTapListener
import com.yandex.mapkit.mapview.MapView
import com.facebook.react.uimanager.UIManagerHelper
import com.facebook.react.uimanager.events.Event

class YamapCircle(context: Context?) : ViewGroup(context), MapObjectTapListener {
var circle: Circle
Expand All @@ -32,8 +30,7 @@ class YamapCircle(context: Context?) : ViewGroup(context), MapObjectTapListener
circle = Circle(center, radius)
}

override fun onLayout(changed: Boolean, l: Int, t: Int, r: Int, b: Int) {
}
override fun onLayout(changed: Boolean, l: Int, t: Int, r: Int, b: Int) {}

// PROPS
fun setCenter(point: Point) {
Expand All @@ -49,7 +46,11 @@ class YamapCircle(context: Context?) : ViewGroup(context), MapObjectTapListener
fun setRadius(_radius: Float) {
radius = _radius
updateGeometry()
if (rnMapObject == null && mapView != null && radius > 0 && (center.latitude != 0.0 || center.longitude != 0.0)) {
if (rnMapObject == null &&
mapView != null &&
radius > 0 &&
(center.latitude != 0.0 || center.longitude != 0.0)
) {
addToMap(mapView!!)
} else {
updateCircle()
Expand Down Expand Up @@ -103,49 +104,42 @@ class YamapCircle(context: Context?) : ViewGroup(context), MapObjectTapListener
fun addToMap(mapView: MapView) {
this.mapView = mapView
updateGeometry()
if (!isGeometryValid()) {
return
}
val circleObject = mapView.getMapWindow().map.mapObjects.addCircle(circle)
rnMapObject = circleObject
circleObject.addTapListener(this)
updateCircle()
}

private fun isGeometryValid(): Boolean {
return radius > 0f && center.latitude.isFinite() && center.longitude.isFinite()
}

fun removeFromMap(mapView: MapView) {
rnMapObject?.let {
mapView.getMapWindow().map.mapObjects.remove(it)
rnMapObject = null
if (it.isValid) {
mapView.getMapWindow().map.mapObjects.remove(it)
}
}
rnMapObject = null
}

override fun onMapObjectTap(mapObject: MapObject, point: Point): Boolean {
val reactContext = context as? ReactContext
if (reactContext == null) {
return false
}

val viewId = getId()
val surfaceId = UIManagerHelper.getSurfaceId(reactContext)
val eventDispatcher = UIManagerHelper.getEventDispatcherForReactTag(reactContext, viewId)

if (eventDispatcher != null) {
val eventData = Arguments.createMap().apply {
putDouble("lat", point.latitude)
putDouble("lon", point.longitude)
}
val event = PressEvent(surfaceId, viewId, eventData)
eventDispatcher.dispatchEvent(event as Event<*>)
}
return true
}

private class PressEvent(surfaceId: Int, viewTag: Int, private val eventData: WritableMap?) : Event<PressEvent>(surfaceId, viewTag) {
override fun getEventName(): String {
return "onCirclePress"
}
override fun getEventData(): WritableMap? {
return eventData
}
override fun getCoalescingKey(): Short {
return 0
if (eventDispatcher != null) {
val event = PressEvent(surfaceId, viewId, "onCirclePress", point)
eventDispatcher.dispatchEvent(event)
}
return handled
}
}
}
Loading
Loading