2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

お題は不問!Qiita Engineer Festa 2023で記事投稿!

【Android】緯度経度から2点の距離を出す【Kotlin】

Posted at

実践

Location.distanceBetweenを利用して2点の距離は出します。

private fun getDistanceBetween(
    latitude1: Double, longitude1: Double,
    latitude2: Double, longitude2: Double
): Float {
    val results = FloatArray(3)
    
    Location.distanceBetween(latitude1, longitude1, latitude2, longitude2, results)

    // 0を指定することで2点の距離を返す
    return results[0]
}

Location.distanceBetweenの引数を上から順に 第一地点の緯度、経度、第二地点の緯度、経度、2点間の距離を詰めるresultsの順で指定します。

resultsではそれぞれ数字を指定して欲しい情報を取得できます。

resultsで指定する数字 意味
0 距離(メートル)
1 始点から終点までの方位角
2 終点から始点までの方位角

参考

2
2
0

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
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?