Home > Software design >  How to convert geohash to Latitude and Longitude in Kotlin
How to convert geohash to Latitude and Longitude in Kotlin

Time:01-02

I have a String value as a geohash that I want to change it to latitude and longitude in Kotlin. I couldn't find any library for that. How can I convert geohash to latitude and longitude in Kotlin?

CodePudding user response:

Looks like geohash-java supports this:

An implementation of Geohashes in pure Java. The produced hashes, when using character precision (multiples of 5 bits) are compatible to the reference implementation geohash.org.

You can however also encode Geohashes down to the full available precision of a long i.e. 64 bits.

You can add the following to your pom.xml file if you are using Maven:

<dependency>
    <groupId>ch.hsr</groupId>
    <artifactId>geohash</artifactId>
    <version>1.4.0</version>
</dependency>

Or the following implementation line to your build.gradle file if you are using Gradle:

implementation("ch.hsr:geohash:1.4.0")

Example Usage:

import ch.hsr.geohash.GeoHash

fun main(args: Array<String>) {
    val geoHashString = "u4pruydqqvj" // Peninsula of Jutland, Denmark from en.wikipedia.org/wiki/Geohash
    println("geoHashString: $geoHashString")
    val geoHash = GeoHash.fromGeohashString(geoHashString)
    println("geoHash: $geoHash")
    val point = geoHash.originatingPoint
    println("point: $point")
    val lat = point.latitude
    println("lat: $lat")
    val lng = point.longitude
    println("lng: $lng")
}

Output:

geoHashString: u4pruydqqvj
geoHash: 1101000100101011011111010111100110010110101101101110001000000000 -> (57.64911130070686,10.407439023256302) -> (57.649109959602356,10.40744036436081) -> u4pruydqqvj
point: (57.64911063015461,10.407439693808556)
lat: 57.64911063015461
lng: 10.407439693808556
  • Related