前提
UnityでiPhone用の位置情報アプリを作成しています。
後述のコルーチンで3秒ごとの移動距離を取得したく色々試したのですが、うまくいきません。
実現したいこと
・コルーチンで3秒ごとに端末緯度経度を用いて移動距離を算出したい。
・while内で新しく位置情報を取得した際にoldLocationが上書きされないようにしたい。
発生している問題・エラーメッセージ
・恐らくwhile内下部でoldLocationを更新した後、whileループの最初に戻ってから3秒後に現在の端末位置を更新した際に、計算処理を行う前にoldLocationも上書きされてしまっているのだと思います。
計算処理を行なっても移動距離が0mになってしまっています。
該当のソースコード
c#
1 2//目標距離までの残り距離 3public static int remainingDistance = 1000; 4//前回while処理時の位置情報 5private Location oldLocation; 6 7〜中略〜 8 9private IEnumerator GetDistance() 10 { 11 var cachedWait = new WaitForSeconds(3); 12 13 LocationInfo locationInfo = Input.location.lastData; 14 15 oldLocation = new Location(locationInfo.latitude, locationInfo.longitude); 16 //実機テスト用デバッグテキスト 17 debug1.text = string.Format("3秒前緯度{0},経度{1}", oldLocation.Latitude, oldLocation.Longitude); 18 19 while (remainingDistance >= 0) 20 { 21 yield return cachedWait; 22 23 LocationInfo locationInfo1 = Input.location.lastData; 24 //現在の端末の緯度経度 25 Location deviceLocation = new Location(locationInfo1.latitude, locationInfo1.longitude); 26 27 debug2.text = string.Format("現在緯度{0},経度{1}", deviceLocation.Latitude, deviceLocation.Longitude); 28 29 //移動距離(メートル) 二点間の緯度経度から距離を計算する処理 30 int distance = (int)NaviMath.LatlngDistance(deviceLocation, oldLocation) * 1000; 31 32 text2.text = string.Format("{0}", distance); 33 34 remainingDistance -= distance; 35 36 text.text = string.Format("残り{0}m", remainingDistance); 37 //更新 38 oldLocation = deviceLocation; 39 40 } 41 42 //ここは無視してください 43 text2.text = string.Format(""); 44 arrivalpanel.SetActive(true); 45 yield break; 46 47 }
試したこと
試しにstartメソッド内でInput.location.lastDataを使ってoldLocationに端末位置を代入したら、処理一回で移動距離に1400万mほど加算されてしまいました。こちらの原因は分かりませんでした。
実在地の緯度経度で試算を行った際にはおおよそ正確な値が出たので計算処理は間違っていないはずです。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2022/11/23 01:36
2022/11/23 01:41
2022/11/23 02:22