LoginSignup
2
1

More than 3 years have passed since last update.

Reverse Geocoder(逆ジオコーダー) android × kotlin

Posted at

Reverse Geocoder(逆ジオコーダー)

kotlinで書いている記事が少なかったので書き残して起きます。

逆ジオコーデイングする上で少し沼ってしまったので

誰かの参考になればいいかなと思っています。

*自分は実務経験がない開発者なので間違っている場合があると思いますが

そこら辺はご容赦ください。

環境

言語
kotlin
フレームワーク
Android Studio 4.1.2

やり方

事前準備

経度緯度を取得しておく

latitude(緯度)とlongitude(経度)

逆ジオコーダー

kotlin.MapActivity.kt
fun getAddress(latitude: Double, longitute: Double) {
        val strAddr = StringBuffer()
        val gcoder = Geocoder(this, Locale.getDefault())
        try {
            val lstAddrs = gcoder.getFromLocation(latitude, longitute, 1)
            for (addr in lstAddrs) {
                val idx = addr.maxAddressLineIndex
                for (i in 0..idx) {
                    strAddr.append(addr.getAddressLine(i))
                    Log.v("addr", addr.getAddressLine(i))
                }
            }
            val newStr = strAddr.toString()
            val names = newStr.split(" ".toRegex()).toTypedArray()
            //↓変更必要(geocoderText)
            geocoderText.setText(names[1])
            Toast.makeText(this, names[1], Toast.LENGTH_LONG).show()
        } catch (e: IOException) {
            e.printStackTrace()
        }
    }

後はこいつを呼び出せば使えます

kotlin.MapActivity.kt
getAddress(latitude,longitude)

latitude(緯度)とlongitude(経度)の部分は

最初に説明した通り、事前に準備が必要です。

2
1
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1