Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Troula · Apr 10, 2014 at 05:02 PM · androidgps

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?

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Ramesh_New · Mar 21, 2017 at 08:16 AM 0
Share

hi Troula can u please share a code snippet which u re trying? @Troula?

avatar image zapantis88 · May 04, 2017 at 09:59 PM 0
Share

@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;
 
         }
     }
 }


4 Replies

· Add your reply
  • Sort: 
avatar image
2

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

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?

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image LexDeKogel · Mar 10 at 04:25 PM 0
Share

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

avatar image
0

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!";
         }
     }
 }
Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image antx · May 03, 2017 at 09:06 AM 1
Share

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).

avatar image zapantis88 antx · May 03, 2017 at 10:34 AM 0
Share

@antx Thank you.i will give it a try later when i get back at home.

avatar image $$anonymous$$ · May 10, 2017 at 10:15 AM 0
Share

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.

avatar image $$anonymous$$ · May 11, 2017 at 06:10 AM 1
Share

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.

avatar image
0

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....

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

26 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges