Realtime GPS not working

Could someone tell me why the GPS location is acquired on Start but is not updated in realtime, please?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class GPSLocation : MonoBehaviour
{
    // Get start co-ordinates
    // Keep current co-ordinates updating

    // Measure how much gameObject Map needs to be shifted
    // Shift map accordingly

    private bool enableByRequest = true;
    public int maxWait = 10;

    [Header("Position of Tracking Device on Start")]
    public Vector2 startPositionCoordinates;
    public bool startPositionAcquired = false;
   
    [Header("Actively Updated Tracking Device Location")]
    public Vector2 deviceCoordinates;
    public float deviceLatitude, deviceLongitude;

    [Header("Texts")]
    public Text text;

    void Update()
    {
        deviceCoordinates = new Vector2(deviceLatitude, deviceLongitude);

        if(startPositionAcquired == false)
        {
            startPositionCoordinates = new Vector2(deviceLatitude, deviceLongitude);
           
            if(startPositionCoordinates != Vector2.zero)
            {
                startPositionAcquired = true;
            }
        }
    }

    IEnumerator Start()
    {
        LocationService service = Input.location;
        if (!enableByRequest && !service.isEnabledByUser)
        {
            Debug.Log("Location Services not enabled by user");
            yield break;
        }
        service.Start();
        while (service.status == LocationServiceStatus.Initializing && maxWait > 0)
        {
            yield return new WaitForSeconds(1);
            maxWait--;
        }
        if (maxWait < 1)
        {
            Debug.Log("Timed out");
            yield break;
        }
        if (service.status == LocationServiceStatus.Failed)
        {
            Debug.Log("Unable to determine device location");
            yield break;
        }
        else
        {
            text.text = "Start Location : " + startPositionCoordinates + "\nMy Location: " + service.lastData.latitude + ", " + service.lastData.longitude;
            deviceLatitude = service.lastData.latitude;
            deviceLongitude = service.lastData.longitude;
            CalculateMapPosition();
            Debug.Log("Updated GPS Position");
        }
    }

    private void CalculateMapPosition()
    {
        Debug.Log("Calculating Map position shift");
    }
}

It’s because you set them in Start and then never update them again.

You need lines 70-71 to be repeated on a regular basis, either in a loop in your coroutine, or in Update.

Ohh! Thank you eagle eyes, I was under the impression if you don’t yield an IEnumerator it will continue to run?

Only if you put everything in a loop.

1 Like

@millefoliumink
Unity Input.Location API is not event based like the ones you may may have used in native iOS or Android. You have to manually check the value of current location and see if it’s changed or not from the last value, that would be a classic location event.
I have made a complete location wrapper for my plugin GO Map.
Bye
Alan