Reputation: 13
Script GetGPS
gets user coordinates and GetAqiInfo
gets AQI information in users current location using weatherbit.io API. I have tested and found GetGPS script works fine (lat/long vales are correct), but when I try to pass those values to GetAqi script, it automatically puts lat: 0 long: 0. Where am I going wrong?
GetGPS Script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Android;
using UnityEngine.Networking;
public class GetGPS : MonoBehaviour
{
public static float latitude;
public static float longitude;
private void Start()
{
DontDestroyOnLoad(gameObject);
StartCoroutine(StartLocationService());
}
private void Awake()
{
if (!Permission.HasUserAuthorizedPermission(Permission.FineLocation))
{
Permission.RequestUserPermission(Permission.FineLocation);
}
}
private IEnumerator StartLocationService()
{
if (!Input.location.isEnabledByUser)
{
Debug.Log("User has not enabled GPS");
yield break;
}
Input.location.Start();
int maxWait = 20;
while (Input.location.status == LocationServiceStatus.Initializing &&
maxWait > 0)
{
yield return new WaitForSeconds(1);
maxWait--;
}
if (maxWait <= 0)
{
Debug.Log("Timed Out");
yield break;
}
if (Input.location.status == LocationServiceStatus.Failed)
{
Debug.Log("Unable to determine device location");
yield break;
}
latitude = Input.location.lastData.latitude;
longitude = Input.location.lastData.longitude;
yield break;
}
}
GetAqi Script
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.UI;
using SimpleJSON;
public class GetAqi : MonoBehaviour
{
public static string latitude;
public static string longitude;
public static string cityName;
public static double currentAqi;
private readonly string baseWeatherbitURL =
"https://api.weatherbit.io/v2.0/current/airquality?";
private readonly string key = "*********************";
// Start is called before the first frame update
void Start()
{
latitude = GetGPS.latitude.ToString();
longitude = GetGPS.longitude.ToString();
StartCoroutine(GetAqiInfo());
}
private IEnumerator GetAqiInfo()
{
string weatherbitURL = baseWeatherbitURL + "lat=" + latitude +
"&lon=" + longitude + "&key=" + key;
UnityWebRequest aqiInfoRequest = UnityWebRequest.Get(weatherbitURL);
yield return aqiInfoRequest.SendWebRequest();
//error
if (aqiInfoRequest.isNetworkError || aqiInfoRequest.isHttpError)
{
Debug.LogError(aqiInfoRequest.error);
yield break;
}
JSONNode aqiInfo = JSON.Parse(aqiInfoRequest.downloadHandler.text);
cityName = aqiInfo["city_name"];
currentAqi = aqiInfo["data"][0]["aqi"].AsInt;
Debug.Log($"New data available: ${currentAqi}");
}
}
EDIT:
As suggested, I did add userLatitude
and userLongitude
in IEnumerator
instead of void start()
, the code still returns the same.
but what I want is to get the users current location. It feels like the GetAqi
script runs before GetGPS
script. Is there a fix to that?
Upvotes: 1
Views: 73
Reputation: 13
Answer: Should have used Void Update ()
instead of void Start ()
Void Update = The script is updating the number of times of the refresh rate of the device Void Start = The script runs only once.
Yes, I realize this was a extremely noob mistake. But thank you so much @UnholySheep and @RustyBucketBay for your valuable advice.
Upvotes: 0
Reputation: 4561
Input.location.lastData.latitude
must be 0 at the time its called, or the variable is being overwritten somewhere else before its called in the GetAqi script.
Check for more instances of the GetGPS script in your scene in case there could be any other, or check for all the the references to the static variables latitude
and longitude
(alt + F12 in visual studio), so that you can check if the variables might be overwritten elsewhere.
Edit: You need to remove:
latitude = GetGPS.latitude.ToString();
longitude = GetGPS.longitude.ToString();
From the Start()
, because this can be read before the StartLocationService
corroutine call in the GetGPS script, so at the moment they are read they are 0. You need to ensure that the values were already assigned when you read them in the GetAqi class.
Upvotes: 1