GPSPin.cs 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. namespace Imagine.WebAR{
  5. public class GPSPin : MonoBehaviour{
  6. [SerializeField] public string id;
  7. [SerializeField] public double latitude = 0;
  8. [SerializeField] public double longitude = 0;
  9. [SerializeField] public double altitude = 0;
  10. [HideInInspector] public Vector3 targetPos;
  11. [HideInInspector] public bool inRange = false;
  12. [HideInInspector] public bool entered = false;
  13. public void Start(){
  14. targetPos = transform.position;
  15. }
  16. public Vector3 ConvertGPSToCartesian(double referenceLatitude, double referenceLongitude, double referenceAltitude)
  17. {
  18. // Radius of the Earth (in meters) for a simplified flat Earth model
  19. double earthRadius = 6371000.0;
  20. // Convert latitude and longitude to radians
  21. double latRad = latitude * Mathf.Deg2Rad;
  22. double lonRad = longitude * Mathf.Deg2Rad;
  23. // Calculate the meters per degree for latitude and longitude
  24. double metersPerDegreeLatitude = 111132.92 - 559.82 * Mathf.Cos(2 * (float)latRad) + 1.175 * Mathf.Cos(4 * (float)latRad) - 0.0023 * Mathf.Cos(6 * (float)latRad);
  25. double metersPerDegreeLongitude = Mathf.PI * earthRadius * Mathf.Cos((float)latRad) / 180.0;
  26. // Calculate X, Y, Z coordinates
  27. double z = (latitude - referenceLatitude) * metersPerDegreeLatitude;
  28. double y = altitude - referenceAltitude;
  29. double x = (longitude - referenceLongitude) * metersPerDegreeLongitude;
  30. return new Vector3((float)x, (float)y, (float)z);
  31. }
  32. }
  33. }