From 9eb232596ff140107790ccbe61748d8b8284df56 Mon Sep 17 00:00:00 2001 From: Masashi Katsumata Date: Fri, 13 Nov 2026 02:27:09 -0800 Subject: [PATCH] Find tapped markers when the projection rounds to whole pixels MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The marker hit test measures the screen scale round the tap from points 1e-4° north and east of it. ArcGIS's 3D SceneView returns whole-pixel screen points, and zoomed out 1e-4° is under a pixel: both points landed on the tap, the scale came out 0, no search area was made, and a tap on a marker did nothing (the marker-animation sample on ArcGIS: tapping Bounce did not bounce it). The step now widens tenfold until both points are at least 4 px away, up to 1°. --- .../mapconductor/core/marker/MarkerHitPick.kt | 28 +++++++++++++++---- .../core/marker/MarkerHitTestTest.kt | 22 +++++++++++++++ 2 files changed, 45 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/mapconductor/core/marker/MarkerHitPick.kt b/src/main/java/com/mapconductor/core/marker/MarkerHitPick.kt index 7a05eb5..e1216aa 100644 --- a/src/main/java/com/mapconductor/core/marker/MarkerHitPick.kt +++ b/src/main/java/com/mapconductor/core/marker/MarkerHitPick.kt @@ -81,11 +81,23 @@ object MarkerHitPick { project: (GeoPointInterface) -> Offset?, density: Double = ResourceProvider.getDensity().toDouble(), ): GeoRectBounds? { - val epsilon = 1e-4 - val north = project(GeoPoint(position.latitude + epsilon, position.longitude)) ?: return null - val east = project(GeoPoint(position.latitude, position.longitude + epsilon)) ?: return null - val pxPerDegLat = hypot((north.x - touchScreen.x).toDouble(), (north.y - touchScreen.y).toDouble()) / epsilon - val pxPerDegLon = hypot((east.x - touchScreen.x).toDouble(), (east.y - touchScreen.y).toDouble()) / epsilon + // The scale round the tap, from points a little north and east of it. Some + // projections round to whole pixels (ArcGIS's 3D SceneView): zoomed out, 1e-4° + // is under a pixel and both points land on the tap, so widen the step until + // they move far enough to measure. + var epsilon = 1e-4 + var pxPerDegLat: Double + var pxPerDegLon: Double + while (true) { + val north = project(GeoPoint(position.latitude + epsilon, position.longitude)) ?: return null + val east = project(GeoPoint(position.latitude, position.longitude + epsilon)) ?: return null + val dLat = hypot((north.x - touchScreen.x).toDouble(), (north.y - touchScreen.y).toDouble()) + val dLon = hypot((east.x - touchScreen.x).toDouble(), (east.y - touchScreen.y).toDouble()) + pxPerDegLat = dLat / epsilon + pxPerDegLon = dLon / epsilon + if ((dLat >= MIN_MEASURED_PX && dLon >= MIN_MEASURED_PX) || epsilon >= MAX_EPSILON_DEG) break + epsilon *= 10 + } if (pxPerDegLat <= 0.0 || pxPerDegLon <= 0.0) return null val radiusPx = SEARCH_RADIUS_DP * density val halfLat = radiusPx / pxPerDegLat @@ -142,4 +154,10 @@ object MarkerHitPick { * 候補を探す半径(dp)。ラベルの長さの上限の目安で、これより遠くまで伸びたラベルの端は拾えない。 */ const val SEARCH_RADIUS_DP: Double = 256.0 + + /** The scale is measured over at least this many pixels (see [searchBounds]). */ + private const val MIN_MEASURED_PX = 4.0 + + /** The widest step tried when measuring the scale: a 1° step covers any zoom that shows markers. */ + private const val MAX_EPSILON_DEG = 1.0 } diff --git a/src/test/java/com/mapconductor/core/marker/MarkerHitTestTest.kt b/src/test/java/com/mapconductor/core/marker/MarkerHitTestTest.kt index 3df704b..e104f6d 100644 --- a/src/test/java/com/mapconductor/core/marker/MarkerHitTestTest.kt +++ b/src/test/java/com/mapconductor/core/marker/MarkerHitTestTest.kt @@ -139,4 +139,26 @@ class MarkerHitTestPickTest { assertTrue(MarkerHitPick.pickIndex(touch, listOf(far, near), 40.0) == 1) assertTrue(MarkerHitPick.pickIndex(Offset(400f, 400f), listOf(far, near), tolerance) == null) } + + /** + * A projection that rounds to whole pixels (ArcGIS's 3D SceneView does): zoomed out, + * 1e-4° is under a pixel, and the scale must still be measured. Without that the + * search area could not be made and no marker tap was recognised. + */ + @Test + fun searchBoundsWorksWithAProjectionRoundedToWholePixels() { + val worldPx = 512.0 * Math.pow(2.0, 9.0) // zoom 9 + fun project(p: com.mapconductor.core.features.GeoPointInterface): Offset { + val x = (p.longitude + 180.0) / 360.0 * worldPx + val s = Math.sin(Math.toRadians(p.latitude)) + val y = (0.5 - Math.log((1 + s) / (1 - s)) / (4 * Math.PI)) * worldPx + return Offset(Math.round(x).toFloat(), Math.round(y).toFloat()) + } + val tap = com.mapconductor.core.features.GeoPoint(21.31, -157.86) + val bounds = MarkerHitPick.searchBounds(tap, project(tap), ::project, density = 2.0) + assertTrue("bounds were not made", bounds != null) + // 256 dp at density 2 is 512 px; at zoom 9 (728 px a degree) that is about 0.70° each way. + val halfLon = (bounds!!.northEast!!.longitude - bounds.southWest!!.longitude) / 2 + assertTrue("half width $halfLon", halfLon in 0.63..0.77) + } }