p-lr / MapComposeMP

Kotlin Multiplatform port of MapCompose
Apache License 2.0
36 stars 5 forks source link

I cant use lat & long for scroolto point? #12

Closed toffeantyri closed 4 days ago

toffeantyri commented 5 days ago

method scroll have params x,y in 0..1 ? I cant use latitude and longitude? Or how i can to convert it value?
I use osm Osm source (by OsmDemo)

toffeantyri commented 5 days ago

Standart algorithm for normalisation in 0..1 - take out wrong result

p-lr commented 4 days ago

https://github.com/p-lr/MapCompose/issues/111

toffeantyri commented 4 days ago

Its good example


import java.io.IOException

fun main() {
    val (X, Y) = doProjection(48.86, 2.35)!!  // Paris
    println("projected: X=$X , Y=$Y")

    val x = normalize(X, min = X0, max = -X0)
    val y = normalize(Y, min = -X0, max = X0)
    println("normalized: x=$x , y=$y")
}

fun doProjection(latitude: Double, longitude: Double): Pair<Double, Double>? {
    if (abs(latitude) > 90 || abs(longitude) > 180) {
        return null
    }
    val num = longitude * 0.017453292519943295 // 2*pi / 360
    val X = 6378137.0 * num
    val a = latitude * 0.017453292519943295
    val Y = 3189068.5 * ln((1.0 + sin(a)) / (1.0 - sin(a)))

    return Pair(X, Y)
}

fun normalize(t: Double, min: Double, max: Double): Double {
    return (t - min) / (max - min)
}

private const val X0 = -20037508.3427892476```