kotlinopenstreetmaposmdroid

OSMDroid get the radius of the map


I'm using OSMDroid (Open Street Map for Android), and I'm trying to get the radius of the visible map, to send it to my API and get a result scaled correctly.

Here is what I'm doing on iOS using MapKit to achieve this:

extension MKMapView {

    func topLeftCoordinate() -> CLLocationCoordinate2D {
        return self.convert(CGPoint(x: 0, y: 0), toCoordinateFrom: self)
    }

    func currentRadius() -> Double {
        let topLeftCoordinate = self.topLeftCoordinate()
        return sqrt(pow(centerCoordinate.latitude - topLeftCoordinate.latitude, 2) + pow(centerCoordinate.longitude - topLeftCoordinate.longitude, 2))
    }

}

OSMDroid is having a zoomLevel but I'm unable to find a way to convert it to what I need, keeping the correct scaling for API.

Thanks in advance for any help or lead.


Solution

  • I finally found a working solution. It's some kind of magic, but it seems to work correctly (constants were adapted a lot of times before getting the result)

    fun MapView.currentRadius(): Double {
        // This is magic
        return 400 / 2.toDouble().pow(zoomLevelDouble)
    }