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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ Explore the example applications to see OpenMapView in action:
- [Example11MapTypes](examples/Example11MapTypes) - Switching between 5 map types (Normal, Terrain, Humanitarian, Cycle, None)
- [Example12Toolbar](examples/Example12Toolbar) - Open location in external map apps via geo: URI with OpenStreetMap browser fallback

![Example05Camera Demo](examples/Example05Camera/screenshot.gif)
![Example01Pan Demo](examples/Example01Pan/screenshot.gif)

## Documentation

Expand Down
44 changes: 22 additions & 22 deletions openmapview/src/main/kotlin/de/afarber/openmapview/MapController.kt
Original file line number Diff line number Diff line change
Expand Up @@ -1305,8 +1305,8 @@ class MapController(

private fun drawPolylines(
canvas: Canvas,
centerPixelX: Double,
centerPixelY: Double,
centerPixelX: Float,
centerPixelY: Float,
) {
val paint = Paint()
paint.style = Paint.Style.STROKE
Expand Down Expand Up @@ -1343,8 +1343,8 @@ class MapController(

private fun drawPolygons(
canvas: Canvas,
centerPixelX: Double,
centerPixelY: Double,
centerPixelX: Float,
centerPixelY: Float,
) {
val fillPaint = Paint()
fillPaint.style = Paint.Style.FILL
Expand Down Expand Up @@ -1410,8 +1410,8 @@ class MapController(

private fun drawCircles(
canvas: Canvas,
centerPixelX: Double,
centerPixelY: Double,
centerPixelX: Float,
centerPixelY: Float,
) {
val fillPaint = Paint()
fillPaint.style = Paint.Style.FILL
Expand Down Expand Up @@ -1465,8 +1465,8 @@ class MapController(

private fun drawMarkers(
canvas: Canvas,
centerPixelX: Double,
centerPixelY: Double,
centerPixelX: Float,
centerPixelY: Float,
) {
for (marker in markers) {
// Skip invisible markers
Expand Down Expand Up @@ -1503,8 +1503,8 @@ class MapController(

private fun drawGroundOverlay(
canvas: Canvas,
centerPixelX: Double,
centerPixelY: Double,
centerPixelX: Float,
centerPixelY: Float,
groundOverlay: GroundOverlay,
) {
val bitmap = loadBitmap(groundOverlay.image)
Expand Down Expand Up @@ -1583,8 +1583,8 @@ class MapController(

private fun drawPolygonsByZIndex(
canvas: Canvas,
centerPixelX: Double,
centerPixelY: Double,
centerPixelX: Float,
centerPixelY: Float,
zIndex: Float,
sortedPolygons: List<Polygon>,
) {
Expand Down Expand Up @@ -1649,8 +1649,8 @@ class MapController(

private fun drawPolylinesByZIndex(
canvas: Canvas,
centerPixelX: Double,
centerPixelY: Double,
centerPixelX: Float,
centerPixelY: Float,
zIndex: Float,
sortedPolylines: List<Polyline>,
) {
Expand Down Expand Up @@ -1689,8 +1689,8 @@ class MapController(

private fun drawCirclesByZIndex(
canvas: Canvas,
centerPixelX: Double,
centerPixelY: Double,
centerPixelX: Float,
centerPixelY: Float,
zIndex: Float,
sortedCircles: List<Circle>,
) {
Expand Down Expand Up @@ -1725,8 +1725,8 @@ class MapController(

private fun drawMarkersByZIndex(
canvas: Canvas,
centerPixelX: Double,
centerPixelY: Double,
centerPixelX: Float,
centerPixelY: Float,
zIndex: Float,
sortedMarkers: List<Marker>,
) {
Expand Down Expand Up @@ -1754,8 +1754,8 @@ class MapController(

private fun drawInfoWindows(
canvas: Canvas,
centerPixelX: Double,
centerPixelY: Double,
centerPixelX: Float,
centerPixelY: Float,
) {
val backgroundPaint =
Paint().apply {
Expand Down Expand Up @@ -1888,8 +1888,8 @@ class MapController(
private fun drawTileOverlay(
canvas: Canvas,
visibleTiles: List<TileCoordinate>,
centerPixelX: Double,
centerPixelY: Double,
centerPixelX: Float,
centerPixelY: Float,
overlay: TileOverlay,
) {
val cache = overlayTileCaches[overlay.id] ?: return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ class Projection
val adjustedScreenX = screenX - paddingOffsetX
val adjustedScreenY = screenY - paddingOffsetY

val pixelX = (centerPixelX + (adjustedScreenX - viewWidth / 2 + panOffsetX).toDouble()).toInt()
val pixelY = (centerPixelY + (adjustedScreenY - viewHeight / 2 + panOffsetY).toDouble()).toInt()
val pixelX = (centerPixelX + adjustedScreenX - viewWidth / 2 + panOffsetX).toInt()
val pixelY = (centerPixelY + adjustedScreenY - viewHeight / 2 + panOffsetY).toInt()

return ProjectionUtils.pixelToLatLng(pixelX, pixelY, zoom.toInt())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,12 @@ internal object ProjectionUtils {
fun latLngToPixel(
latLng: LatLng,
zoom: Int,
): Pair<Double, Double> {
): Pair<Float, Float> {
val n = 2.0.pow(zoom)
val xPixel = (latLng.longitude + 180.0) / 360.0 * n * TILE_SIZE
val latRad = latLng.latitude * PI / 180.0
val yPixel = (1.0 - ln(tan(latRad) + 1.0 / cos(latRad)) / PI) / 2.0 * n * TILE_SIZE
return Pair(xPixel, yPixel)
return Pair(xPixel.toFloat(), yPixel.toFloat())
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ data class TileOverlay(
val fadeIn: Boolean = false,
val tag: Any? = null,
) {
internal val id: String = "tileoverlay_${System.nanoTime()}_${System.identityHashCode(this)}"
internal val id: String = "tileOverlay_${System.nanoTime()}_${System.identityHashCode(this)}"

init {
require(transparency in 0f..1f) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,34 +11,35 @@ import org.junit.Assert.assertEquals
import org.junit.Test

class ProjectionTest {
private val epsilon = 0.0001
private val epsilonFloat = 0.0001f
private val epsilonDouble = 0.0001

@Test
fun testLatLngToPixel_Equator() {
// At zoom 0, equator center should be at (128, 128)
val (x, y) = ProjectionUtils.latLngToPixel(LatLng(0.0, 0.0), 0)
assertEquals(128.0, x, epsilon)
assertEquals(128.0, y, epsilon)
assertEquals(128.0f, x, epsilonFloat)
assertEquals(128.0f, y, epsilonFloat)
}

@Test
fun testLatLngToPixel_NullIsland() {
// Null Island (0,0) at different zooms
val zoom1 = ProjectionUtils.latLngToPixel(LatLng(0.0, 0.0), 1)
assertEquals(256.0, zoom1.first, epsilon)
assertEquals(256.0, zoom1.second, epsilon)
assertEquals(256.0f, zoom1.first, epsilonFloat)
assertEquals(256.0f, zoom1.second, epsilonFloat)

val zoom2 = ProjectionUtils.latLngToPixel(LatLng(0.0, 0.0), 2)
assertEquals(512.0, zoom2.first, epsilon)
assertEquals(512.0, zoom2.second, epsilon)
assertEquals(512.0f, zoom2.first, epsilonFloat)
assertEquals(512.0f, zoom2.second, epsilonFloat)
}

@Test
fun testPixelToLatLng_Equator() {
// At zoom 0, pixel (128, 128) should be equator center
val latLng = ProjectionUtils.pixelToLatLng(128, 128, 0)
assertEquals(0.0, latLng.latitude, epsilon)
assertEquals(0.0, latLng.longitude, epsilon)
assertEquals(0.0, latLng.latitude, epsilonDouble)
assertEquals(0.0, latLng.longitude, epsilonDouble)
}

@Test
Expand All @@ -50,8 +51,8 @@ class ProjectionTest {
val (x, y) = ProjectionUtils.latLngToPixel(original, zoom)
val result = ProjectionUtils.pixelToLatLng(x.toInt(), y.toInt(), zoom)

assertEquals(original.latitude, result.latitude, epsilon)
assertEquals(original.longitude, result.longitude, epsilon)
assertEquals(original.latitude, result.latitude, epsilonDouble)
assertEquals(original.longitude, result.longitude, epsilonDouble)
}

@Test
Expand Down Expand Up @@ -91,8 +92,8 @@ class ProjectionTest {

// +180° should be at right edge (512), -180° at left edge (0)
// They represent the same meridian but wrap around
assertEquals(512.0, positive.first, epsilon)
assertEquals(0.0, negative.first, epsilon)
assertEquals(512.0f, positive.first, epsilonFloat)
assertEquals(0.0f, negative.first, epsilonFloat)
}

@Test
Expand Down