- Home /
Get updated values from gps as moving
So, this may sound a little stupid but I have a basic question. I am using the script from the documentation to get the longtitude and latitude to an android device. LocationService.Start
It works fine and I actually get very accurate results. My problem is how can I make it change and get the new values while I am moving. It says that by default it changes every 10 meters, but when I try and move and I still have my application running it doesn't change. Do I have to put the function in Update() or somewhere else in order for it to be updated and change when I move? Is there any other way?
hi Troula can u please share a code snippet which u re trying? @Troula?
@Troula This code made my coordinates update.I know maybe it is not the right approach but it works (my coding knowledge is limited).
using UnityEngine;
using UnityEngine.Scene$$anonymous$$anagement;
using System.Collections;
using UnityEngine.UI;
public class UpdatingGpsLL : $$anonymous$$onoBehaviour
{
private Vector2 deviceCoordinates;
private bool enableByRequest = true;
public int maxWait = 10;
public Text text;
void Awake()
{
StartCoroutine("getLocation");
}
IEnumerator getLocation()
{
LocationService service = Input.location;
if (!enableByRequest && !service.isEnabledByUser)
{
Debug.Log("Location Services not enabled by user");
yield break;
}
service.Start(1f, 0.1f);
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 deter$$anonymous$$e device location");
yield break;
}
else
{
text.text = "\$$anonymous$$y Location: " + service.lastData.latitude + ", " + service.lastData.longitude;
}
while (true)
{
yield return new WaitForSeconds(2);
Debug.Log("updating");
text.text = "\$$anonymous$$y Location: " + service.lastData.latitude + ", " + service.lastData.longitude;
}
}
}
Answer by antx · May 03, 2017 at 08:45 AM
You have off course continuously get the current position from the LocationService.
The whole thing works like this:
Check if LocationService is enabled by User (whether the user has GPS and Co turn on for the device)
Start the LocationService -> LocationService.Start()
check the status of the LocationService -> LocationService.status (LocationServiceStatus.Running is what you need)
then continuously ( like in Update() ) read out LocationService.lastData.latitude and LocationService.lastData.longitute.
Look at the script from zapantis88 which has most of it in there somehow, except the continuous readout of the values is missing.
Answer by ivoras · Feb 10 at 10:36 AM
Ressurecting the thread in case someone can comment on it:
In our experience, reading the GPS location from Unity just returns the "last known" readings from the actual GPS sensor, which is usually out of date. This is on Android.
We see it very well since we use a map: after we start reading the location from Unity, the location can (and usually is) way off.
It seems that the GPS gets re-read only when the user starts Google Maps, the location precision doesn't get better from Unity.
So, is there a way to force re-reading GPS?
It seems that the GPS gets re-read only when the user starts Google Maps, the location precision doesn't get better from Unity.
I'm having exactly this issue, did you find a solution?
Edit: I fixed it, see my answer to the question
Answer by zapantis88 · May 03, 2017 at 07:53 AM
@Troula did you find the solution?i have the exact same problem.
@Ramesh_New here is my code if you can help me i am trying a week to find a solution.I am using galaxy s6 android version 5.1.1 and unity 5.3.5p6 .when i open the app i get the coordinates but they never update.
using UnityEngine;
using UnityEngine.SceneManagement;
using System.Collections;
using UnityEngine.UI;
public class DetectLocation : MonoBehaviour
{
private Vector2 targetCoordinates;
private Vector2 deviceCoordinates;
private float distanceFromTarget = 0.0002f;
private float proximity = 0.001f;
private float sLatitude, sLongitude;
public float dLatitude = 38.235406f, dLongitude = 21.768376f;
private bool enableByRequest = true;
public int maxWait = 10;
public bool ready = false;
public Text text;
void Start()
{
targetCoordinates = new Vector2(dLatitude, dLongitude);
StartCoroutine(getLocation());
}
IEnumerator getLocation()
{
LocationService service = Input.location;
if (!enableByRequest && !service.isEnabledByUser)
{
Debug.Log("Location Services not enabled by user");
yield break;
}
service.Start(1f, 0.1f);
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 = "Target Location : " + dLatitude + ", " + dLongitude + "\nMy
Location: " + service.lastData.latitude + ", " + service.lastData.longitude;
sLatitude = service.lastData.latitude;
sLongitude = service.lastData.longitude;
}
//service.Stop();
ready = true;
startCalculate();
}
public void startCalculate()
{
deviceCoordinates = new Vector2(sLatitude, sLongitude);
proximity = Vector2.Distance(targetCoordinates, deviceCoordinates);
if (proximity <= distanceFromTarget)
{
text.text = text.text + "\nDistance : " + proximity.ToString();
text.text += "\nTarget Detected";
SceneManager.LoadScene("Temple");
}
else
{
text.text = text.text + "\nDistance : " + proximity.ToString();
text.text += "\nTarget not detected, too far!";
}
}
}
Your are not continuously reading out the values for latitude and longitude. Your coroutine is not looping and is run only once in Start().
I would suggest to make the readout in Update() under the condition that the initialization was successful in your coroutine (use a bool for that which you set then in your coroutine).
@antx Thank you.i will give it a try later when i get back at home.
Has someone a solution? I have the same problem. Everything works fine, once a time. The coordinates never change only just by restart the app.
What antx says works for my. I do
void Update() {
bgps= Input.location.lastData.latitude;
Status_Text.text =("gps"+ bgps.ToString ());
lgps = Input.location.lastData.longitude;
Status_Text2.text =("lps"+ lgps.ToString ());
}
and it works, if I be outside.
Answer by LexDeKogel · Mar 14 at 10:44 AM
I had the same issue on Android as @ivoras had (IOS works fine as far as I can tell on just the unity functions), I ended up writing a plugin that just calls the update function in native android code using the following tutorial: http://markcastle.com/steps-to-create-a-native-android-plugin-for-unity-in-java-using-android-studio-part-1-of-2/
I was stuck on this issue for several days and don't wish that fate on any of you. Follow the above tutorial and use the following code. Then just get the location in Input.location.lastData.
package YOURPACKAGENAMEHERE;
import android.annotation.SuppressLint;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Build;
import androidx.annotation.RequiresApi;
public final class LocationHelper {
public static Context context;
public static LocationManager locationManager;
public static LocationListener locationListener;
public static void setContext(Context context)
{
LocationHelper.context = context;
locationManager = (LocationManager)context.getSystemService(Context.LOCATION_SERVICE);
locationListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
}};
}
@RequiresApi(api = Build.VERSION_CODES.P)
@SuppressLint({"MissingPermission"})
public static void StartUpdates()
{
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 800, 0, locationListener);
}
public static void StopUpdates()
{
locationManager.removeUpdates(locationListener);
}
public static String test() { return "IT WORKS";}
}
Can't believe this bug has existed for so long....
Your answer
![](https://koobas.hobune.stream/wayback/20220613142420im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Problem converting longitude/latitude to screen positions. 1 Answer
Accessing sensors of iOS/Android devices 1 Answer
Convert LatLong values into unity coordinates and display hotspots in Camera View 0 Answers
Launching "Locations and Security" Android Menu from Unity? 0 Answers
How to use Locationinfo in unity. 0 Answers