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?
@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 + "
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.
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";}
}