From 6caed761011801718b356c7cebae3f9117c0ac9a Mon Sep 17 00:00:00 2001 From: Tobias Zwick Date: Mon, 27 Jan 2020 15:36:19 +0100 Subject: [PATCH] gps range also precise when tilting the map (#1684) --- .../map/LocationAwareMapFragment.kt | 22 ++++++------------- .../util/SphericalEarthMath.java | 2 ++ 2 files changed, 9 insertions(+), 15 deletions(-) diff --git a/app/src/main/java/de/westnordost/streetcomplete/map/LocationAwareMapFragment.kt b/app/src/main/java/de/westnordost/streetcomplete/map/LocationAwareMapFragment.kt index 897642fa46..549da69aa3 100644 --- a/app/src/main/java/de/westnordost/streetcomplete/map/LocationAwareMapFragment.kt +++ b/app/src/main/java/de/westnordost/streetcomplete/map/LocationAwareMapFragment.kt @@ -187,25 +187,17 @@ open class LocationAwareMapFragment : MapFragment() { val location = displayedLocation ?: return if (accuracyMarker?.isVisible != true) return - val pos = OsmLatLon(location.latitude, location.longitude) - val size = meters2Pixels(pos, location.accuracy) + val zoom = controller!!.cameraPosition.zoom + val size = location.accuracy * pixelsPerMeter(location.latitude, zoom) accuracyMarker?.setStylingFromString( - "{ style: 'points', color: 'white', size: [${size}px, ${size}px], order: 2000, flat: true, collide: false }" + "{ style: 'points', color: 'white', size: ${size}px, order: 2000, flat: true, collide: false }" ) } - private fun meters2Pixels(at: LatLon, meters: Float): Float { - val pos0 = at - val pos1 = SphericalEarthMath.translate(pos0, meters.toDouble(), 0.0) - val screenPos0 = controller!!.latLonToScreenPosition(pos0) - val screenPos1 = controller!!.latLonToScreenPosition(pos1) - val camera = controller!!.cameraPosition - val tiltFactor = (sin(camera.tilt / 2.0) * cos(camera.rotation)).toFloat() - return (1f / (1f - abs(tiltFactor)) * - sqrt( - (screenPos1.y - screenPos0.y).pow(2f) + - (screenPos1.x - screenPos0.x).pow(2f) - )) + private fun pixelsPerMeter(latitude: Double, zoom: Float): Double { + val numberOfTiles = (2.0).pow(zoom.toDouble()) + val metersPerTile = cos(latitude * PI / 180.0) * SphericalEarthMath.EARTH_CIRCUMFERENCE / numberOfTiles + return 256 / metersPerTile } /* --------------------------------- Position tracking -------------------------------------- */ diff --git a/app/src/main/java/de/westnordost/streetcomplete/util/SphericalEarthMath.java b/app/src/main/java/de/westnordost/streetcomplete/util/SphericalEarthMath.java index 337db7a6e3..ef955e17d8 100644 --- a/app/src/main/java/de/westnordost/streetcomplete/util/SphericalEarthMath.java +++ b/app/src/main/java/de/westnordost/streetcomplete/util/SphericalEarthMath.java @@ -19,6 +19,8 @@ public class SphericalEarthMath */ public static final double EARTH_RADIUS = 6371000; + public static final double EARTH_CIRCUMFERENCE = 40000000; + /** * Calculate a bounding box that contains the given circle. In other words, it is a square * centered at the given position and with a side length of radius*2.