Added GPS

This commit is contained in:
2025-11-23 19:42:34 +01:00
parent 39b42e1e90
commit 655d378dbb
3 changed files with 139 additions and 13 deletions

View File

@@ -0,0 +1,72 @@
using System.Collections;
using UnityEngine;
public class GPSManager : MonoBehaviour
{
[Header("GPS settings")]
public float Accuracy = 10f;
public float UpdateDistance = 5f;
public int MaxWait = 20;
[Header("GPS coordinates")]
private double[] LastCoords = new double[2];
private double[] FailsafeCoords = new double[] { 50.7727878, 15.0718625 };
private double? LastTime;
void Start()
{
}
void Update()
{
StartCoroutine(UpdateGPS());
}
public double[] GetLastCoords()
{
if (LastCoords[0] == 0 && LastCoords[1] == 0) { return FailsafeCoords; }
return LastCoords;
}
IEnumerator UpdateGPS()
{
if (!Input.location.isEnabledByUser)
{
Debug.Log("GPS not enabled by user");
LastCoords = FailsafeCoords;
LastTime = null;
yield break;
}
Input.location.Start(Accuracy, UpdateDistance);
while (Input.location.status == LocationServiceStatus.Initializing && MaxWait > 0)
{
yield return new WaitForSeconds(1);
MaxWait--;
}
if (MaxWait < 1)
{
Debug.Log("GPS timed out");
LastCoords = FailsafeCoords;
LastTime = null;
yield break;
}
if (Input.location.status == LocationServiceStatus.Failed)
{
Debug.Log("GPS failed to determine device location");
LastCoords = FailsafeCoords;
LastTime = null;
yield break;
}
else
{
LastCoords[0] = Input.location.lastData.latitude;
LastCoords[1] = Input.location.lastData.longitude;
LastTime = Input.location.lastData.timestamp;
Debug.Log("GPS location: " + LastCoords[0] + ", " + LastCoords[1] + " (time: " + LastTime + ")");
}
}
}

View File

@@ -15,6 +15,7 @@ public class MapRenderer : MonoBehaviour
public float queryRadiusMeters = 200f; // radius around lat/lon to query
[Header("Location (lat, lon)")]
public GPSManager gpsManager;
private double latitude = 50.7727878;
private double longitude = 15.0718625;
@@ -26,30 +27,31 @@ public class MapRenderer : MonoBehaviour
[Header("Road settings")]
public Material roadMaterial;
public float defaultRoadWidth = 4.0f; // meters
public float motorwayWidth = 10.0f;
public float primaryWidth = 8.0f;
public float secondaryWidth = 6.0f;
public float tertiaryWidth = 5.0f;
[Header("Misc")]
public float metersPerUnit = 1f; // scale: 1 unit = 1 meter
public bool autoStart = true;
[Header("Storage")]
Dictionary<long, Vector2> nodes = new Dictionary<long, Vector2>(); // id -> latlon
List<Way> parsedWays = new List<Way>();
void Start()
{
if (autoStart) { StartCoroutine(RenderMap()); }
}
public void StartGenerating()
{
StartCoroutine(RenderMap());
}
IEnumerator RenderMap()
{
ClearChildren();
//TODO: GPS update
double[] GPS = gpsManager.GetLastCoords();
latitude = GPS[0];
longitude = GPS[1];
string q = $"[out:xml][timeout:90];(way[\"building\"](around:{queryRadiusMeters.ToString().Replace(",", ".")},{latitude.ToString().Replace(",", ".")},{longitude.ToString().Replace(",", ".")});way[\"highway\"](around:{queryRadiusMeters.ToString().Replace(",", ".")},{latitude.ToString().Replace(",", ".")},{longitude.ToString().Replace(",", ".")}););(._;>;);out body;";
@@ -337,10 +339,10 @@ public class MapRenderer : MonoBehaviour
{
// simple heuristic
string h = w.tags["highway"];
if (h == "motorway") width = 10f;
else if (h == "primary") width = 8f;
else if (h == "secondary") width = 6f;
else if (h == "tertiary") width = 5f;
if (h == "motorway") width = motorwayWidth;
else if (h == "primary") width = primaryWidth;
else if (h == "secondary") width = secondaryWidth;
else if (h == "tertiary") width = tertiaryWidth;
else width = defaultRoadWidth;
}