unityでGPSを扱ったアプリを作成中です。緯度経度を取得し、それを利用した計算が正常に行われていないので行えるようにし、範囲内に入ったら反応するものを作成したいです。そこで、皆さんのお力を貸していただきたく投稿しました。
###発生している問題・エラーメッセージ
緯度経度は取得できているはずなのですが、計算に使用しても正常に扱えないので困っています。
書き方が間違っているなどがあると思うので、ぜひお願いします。
###該当のソースコード
C#を使用しています。
using UnityEngine;
using System.Collections;
public class GPSLoder : MonoBehaviour
{
private float intervalTime = 0.0f;
public double longitude;
public double latitude;
public static float kokudo;
void Start() { GetPos(); Math(); } void Update() { //毎フレーム読んでると処理が重くなるので、3秒毎に更新 intervalTime += Time.deltaTime; if (intervalTime >= 3.0f) { GetPos(); intervalTime = 0.0f; } } void GetPos() { //GPSで取得した緯度経度を変数に代入 StartCoroutine(GetGPS()); longitude = Input.location.lastData.longitude; latitude = Input.location.lastData.latitude; } void Math() { double lat1 = latitude; double lng1 = longitude; Debug.Log(lat1); Debug.Log(lng1); double lat3 = 36.484977; double lng3 = 136.568824; double dlat = lat1 - lat3; double dlng = lng1 - lng3; double fdlat = dlat * 100000; double fdlng = dlng * 100000; float f1 = (float)fdlat; float f2 = (float)fdlng; float kokudo = Mathf.Sqrt((f1 * f1) + (f2 * f2)); Debug.Log(kokudo); } void onGUI() { if (kokudo < 6) { GUI.Button(new Rect(150, 150, 150, 150),"範囲内です"); } else { GUI.Button(new Rect(150, 150, 150, 150), "範囲外です"); } } private IEnumerator GetGPS() { if (!Input.location.isEnabledByUser) { yield break; } Input.location.Start(); int maxWait = 120; while (Input.location.status == LocationServiceStatus.Initializing && maxWait > 0) { yield return new WaitForSeconds(1); maxWait--; } if (maxWait < 1) { print("Timed out"); yield break; } if (Input.location.status == LocationServiceStatus.Failed) { print("Unable to determine device location"); yield break; } else { print("Location: " + Input.location.lastData.latitude + " " + Input.location.lastData.longitude + " " + Input.location.lastData.altitude + " " + Input.location.lastData.horizontalAccuracy + " " + Input.location.lastData.timestamp); } Input.location.Stop(); }
}
###試したこと
書き方を変更したり、呼び出す部分を変更したりしました。
実機でテストもしてみましたが、反応しませんでした。
###補足情報(言語/FW/ツール等のバージョンなど)
ここを参考にしました。
URL:https://docs.unity3d.com/ScriptReference/LocationService.Start.html
URL:http://hro.hatenablog.jp/entry/2015/08/08/005930
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。