consolidate all repos to one for archive
This commit is contained in:
103
projektna_naloga/ar_map/Assets/Scripts/TrackUser.cs
Normal file
103
projektna_naloga/ar_map/Assets/Scripts/TrackUser.cs
Normal file
@@ -0,0 +1,103 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using TMPro;
|
||||
using UnityEngine;
|
||||
|
||||
public class TrackUser : MonoBehaviour
|
||||
{
|
||||
public GameObject userLocationPrefab;
|
||||
public TextMeshProUGUI gpsStatus;
|
||||
|
||||
private float latitudeGPS = 0;
|
||||
private float longitudeGPS = 0;
|
||||
private double mappedLongitude = 0;
|
||||
private double mappedLatitude = 0;
|
||||
|
||||
// Start is called before the first frame update
|
||||
void Start()
|
||||
{
|
||||
StartCoroutine(GPSLocation());
|
||||
}
|
||||
|
||||
IEnumerator GPSLocation()
|
||||
{
|
||||
// Check if location is enabled
|
||||
if (!Input.location.isEnabledByUser)
|
||||
{
|
||||
yield break;
|
||||
}
|
||||
|
||||
// Start service before querying locaction
|
||||
Input.location.Start();
|
||||
|
||||
// Wait until service is intilized
|
||||
int maxWait = 20;
|
||||
while (Input.location.status == LocationServiceStatus.Initializing && maxWait > 0)
|
||||
{
|
||||
yield return new WaitForSeconds(1);
|
||||
maxWait -= 1;
|
||||
}
|
||||
|
||||
// Service didn't initilize in 20 seconds
|
||||
if (maxWait < 1)
|
||||
{
|
||||
yield break;
|
||||
}
|
||||
|
||||
if (Input.location.status == LocationServiceStatus.Failed)
|
||||
{
|
||||
yield break;
|
||||
}
|
||||
else
|
||||
{
|
||||
// access granted
|
||||
InvokeRepeating("UpdateGPSData", 0.5f, 1);
|
||||
}
|
||||
}
|
||||
|
||||
private void UpdateGPSData()
|
||||
{
|
||||
if (Input.location.status == LocationServiceStatus.Running)
|
||||
{
|
||||
// Access granted to GPS value and it has been initlized
|
||||
latitudeGPS = Input.location.lastData.latitude;
|
||||
longitudeGPS = Input.location.lastData.longitude;
|
||||
}
|
||||
else
|
||||
{
|
||||
// service stopped
|
||||
}
|
||||
}
|
||||
|
||||
void Update()
|
||||
{
|
||||
if (longitudeGPS != 0 || latitudeGPS != 0)
|
||||
{
|
||||
mappedLongitude = CalculateX((double)longitudeGPS);
|
||||
mappedLatitude = CalculateZ((double)latitudeGPS);
|
||||
|
||||
Vector3 coordinates = new((float)mappedLongitude, 0.05f, (float)mappedLatitude);
|
||||
|
||||
userLocationPrefab.transform.position = coordinates;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// X is equivelant to longitude
|
||||
double CalculateX(double val)
|
||||
{
|
||||
return Map(val, 13.356836, 16.596429, 1, -1);
|
||||
}
|
||||
|
||||
// Z is equivelant to latitude
|
||||
double CalculateZ(double val)
|
||||
{
|
||||
return Map(val, 45.411120, 46.875651, 0.6665, -0.6665);
|
||||
}
|
||||
|
||||
// Mapping function
|
||||
double Map(double x, double inMin, double inMax, double outMin, double outMax)
|
||||
{
|
||||
return (x - inMin) * (outMax - outMin) / (inMax - inMin) + outMin;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user