Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Use updating GPS to calculate distance travelled

Discussion in 'General Discussion' started by DJgray3D, Jun 18, 2021.

  1. DJgray3D

    DJgray3D

    Joined:
    Mar 6, 2013
    Posts:
    15
    Unity: 2020.1.11f1
    Mapbox: 2.1.1
    Android and iOS deployment

    I'm currently using GPS to show where the User is located; the Marker on the map moves as the User does.

    I want to be able to track the distance a user has walked and display it in meters.
    I think I've worked out the theory of it but need some help with the application:

    1. Get Users current location on Start()
    2. User starts walking
    3. When the location changes, calculate the distance between the previous action and the current location
    4. This will output a number - the distance between the two points
    5. Convert this number to meters
    6. Add to GUI text layer
    7. Repeat steps 3-6

    This is the script I'm currently using to show the Users current location:

    Code (CSharp):
    1.  private Vector2 targetCoordinates;
    2.     private Vector2 deviceCoordinates;
    3.     public static float lat;
    4.     public static float longi;
    5.     private bool ready = false;
    6.  
    7.     private void Start(){
    8.         StartCoroutine(StartLocationService());
    9.         StartCoroutine(updateGPS());
    10.     }
    11.     public IEnumerator StartLocationService(){
    12.         if (!Input.location.isEnabledByUser){
    13.             Debug.Log("User has not enabled GPS");
    14.             yield break;
    15.         }
    16.         Input.location.Start();
    17.         int maxWait = 20;
    18.         while (Input.location.status == LocationServiceStatus.Initializing && maxWait > 0){
    19.             yield return new WaitForSeconds(1);
    20.             maxWait--;
    21.         }
    22.         if (maxWait < 1){
    23.             Debug.Log("Timed out");
    24.             yield break;
    25.         }
    26.  
    27.         if (Input.location.status == LocationServiceStatus.Failed){
    28.             Debug.Log("Unable to determine device location");
    29.             yield break;
    30.         }
    31.         else{
    32.             longi = Input.location.lastData.longitude;
    33.             lat = Input.location.lastData.latitude;
    34.         }
    35.         ready = true;
    36.     }
    37.     public IEnumerator updateGPS(){
    38.         if (!Input.location.isEnabledByUser){
    39.             Debug.Log("User has not enabled GPS");
    40.             yield break;
    41.         }
    42.         float UPDATE_TIME = 1f;
    43.         WaitForSeconds updateTime = new WaitForSeconds(UPDATE_TIME);
    44.         while (true){
    45.             /* mapScript.Refresh();*/
    46.             longi = Input.location.lastData.longitude;
    47.             lat = Input.location.lastData.latitude;
    48.             SetLocation();
    49.             yield return updateTime;
    50.         }
    51.     }
    Note: I am using Mapbox but cannot use Directions API as this snaps to known pathways and roads but routes taken by Users are often through parks with no predefined routes and I want to track their exact route.
     
  2. Murgilod

    Murgilod

    Joined:
    Nov 12, 2013
    Posts:
    10,083
    ...okay? What's the problem?
     
  3. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    13,554
    Well, you can convert latitude+longitude+altitude to 3d coordinates then use those to calculate distance.

    Be aware that you might need double precision, and the results are not guaranteed to be perfect. Also keep in mind that earth is a spheroid and not a sphere...

    https://stackoverflow.com/questions/10473852/convert-latitude-and-longitude-to-point-in-3d-space
     
    Joe-Censored likes this.
  4. DJgray3D

    DJgray3D

    Joined:
    Mar 6, 2013
    Posts:
    15