0
0

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.

【Android】distanceBetweenで「results is null or has length < 1」が出る

Last updated at Posted at 2023-06-06

はじめに

distanceBetweenとは?

  • 2点間の距離・方角を出してくれるとても便利なメソッド。
  • A地点とB地点の距離を出したい場合は下記のような形で書く
MainActivity.kt
// 基本文法
Location.distanceBetween("A地点の緯度", "A地点の経度", "B地点の緯度", "B地点の経度", "結果格納先(FloatArray型)")

        // 例)渋谷駅から原宿駅の距離が知りたい場合
        val shibuyaLatitude = 35.65834264357642
        val shibuyaLongitude = 139.70157152612902
        val harajukuLatitude = 35.66998832846853
        val harajukuLongitude = 139.70252299715926
        val result = FloatArray(3)

        Location.distanceBetween(
            shibuyaLatitude,
            shibuyaLongitude,
            harajukuLatitude,
            harajukuLongitude,
            result
        )
        // 結果は [0]→距離、[1]→渋谷から原宿への方位角、[2]→原宿から渋谷への方位角]にそれぞれ格納される
        println(result[0]) // 1294.99

何が起こったか

何度実行しても「results is null or has length < 1」のエラーが出ました。
緯度経度の記述順は合っているはずだし、絶対に値が1以上になるような地点の計算だったのに、です。

原因

結果格納先をfloatArrayOf()で初期化していたのでFloatArray(3)にしたらあっさり直りました!!!

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?