Skip to content
Closed
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
61 changes: 56 additions & 5 deletions android/src/main/java/com/luggmaps/core/GoogleMapProvider.kt
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ class GoogleMapProvider(private val context: Context) :
private val liveMarkerViews = mutableSetOf<LuggMarkerView>()
private var activeNonBubbledMarker: Marker? = null
private var tapLocation: LatLng? = null
private var tapDownPoint: android.graphics.Point? = null
private var earlyFiredMarker: Marker? = null

// Initial camera settings
private var initialLatitude: Double = 0.0
Expand Down Expand Up @@ -211,8 +213,25 @@ class GoogleMapProvider(private val context: Context) :
map.setInfoWindowAdapter(this)

wrapperView?.touchEventHandler = { event ->
if (event.action == android.view.MotionEvent.ACTION_DOWN) {
tapLocation = map.projection.fromScreenLocation(android.graphics.Point(event.x.toInt(), event.y.toInt()))
when (event.action) {
android.view.MotionEvent.ACTION_DOWN -> {
val point = android.graphics.Point(event.x.toInt(), event.y.toInt())
tapLocation = map.projection.fromScreenLocation(point)
tapDownPoint = point
earlyFiredMarker = null
}
android.view.MotionEvent.ACTION_UP -> {
val downPoint = tapDownPoint
if (downPoint != null) {
val upX = event.x.toInt()
val upY = event.y.toInt()
val dx = upX - downPoint.x
val dy = upY - downPoint.y
if (dx * dx + dy * dy <= TAP_SLOP_PX * TAP_SLOP_PX) {
fireMarkerPressIfNearby(map, upX, upY)
}
}
}
}
}

Expand Down Expand Up @@ -303,8 +322,11 @@ class GoogleMapProvider(private val context: Context) :
dismissNonBubbledCallout()

markerToViewMap[marker]?.let { view ->
val point = googleMap?.projection?.toScreenLocation(marker.position)
view.emitPressEvent(point?.x?.toFloat() ?: 0f, point?.y?.toFloat() ?: 0f)
if (marker != earlyFiredMarker) {
val point = googleMap?.projection?.toScreenLocation(marker.position)
view.emitPressEvent(point?.x?.toFloat() ?: 0f, point?.y?.toFloat() ?: 0f)
}
earlyFiredMarker = null

val calloutView = view.calloutView
if (calloutView != null && !calloutView.bubbled && calloutView.hasCustomContent) {
Expand All @@ -316,7 +338,6 @@ class GoogleMapProvider(private val context: Context) :
}

if (!view.centerOnPress) {
marker.showInfoWindow()
return true
}
}
Expand Down Expand Up @@ -1322,7 +1343,37 @@ class GoogleMapProvider(private val context: Context) :

// endregion

private fun fireMarkerPressIfNearby(map: GoogleMap, x: Int, y: Int) {
val density = context.resources.displayMetrics.density
val hitRadiusPx = (HIT_RADIUS_DP * density).toInt()

var nearestMarker: Marker? = null
var nearestDistSq = hitRadiusPx * hitRadiusPx

for ((marker, view) in markerToViewMap) {
val anchorPoint = map.projection.toScreenLocation(marker.position)
// anchor is bottom-center by default; shift hit center upward by half the icon height
val iconHalfHeightPx = (view.scaledContentHeight / 2f).toInt()
val hitCenterY = anchorPoint.y - iconHalfHeightPx
val dx = anchorPoint.x - x
val dy = hitCenterY - y
val distSq = dx * dx + dy * dy
if (distSq < nearestDistSq) {
nearestDistSq = distSq
nearestMarker = marker
}
}

nearestMarker?.let { marker ->
val view = markerToViewMap[marker] ?: return
earlyFiredMarker = marker
view.emitPressEvent(x.toFloat(), y.toFloat())
}
}

companion object {
const val DEMO_MAP_ID = "DEMO_MAP_ID"
private const val TAP_SLOP_PX = 20
private const val HIT_RADIUS_DP = 44f
}
}
Loading