Skip to content

Commit

Permalink
gps range also precise when tilting the map (#1684)
Browse files Browse the repository at this point in the history
  • Loading branch information
westnordost committed Jan 27, 2020
1 parent 0141d87 commit 6caed76
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 -------------------------------------- */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 6caed76

Please sign in to comment.