Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by Kaikat · Apr 12, 2017 at 01:29 AM · androidbug-perhapsgps

GPS does not work on Android - only on iOS

Hello,

I am using Unity for my project for its ability to compile to both Android and iOS. However, the GPS does not seem to work for Android. From searching these forums it seems like everyone else has also been having issues with GPS updating on Android? How should I fix this problem? If it is a bug, how long will it take for Unity to fix this issue with Android devices?

My code currently updates the GPS location every 5 seconds. It works on iOS and updates as I walk, but for Android no matter how far I walk I keep getting the same coordinate, the initial coordinate, it never updates. I tested on two Android devices, one was Android 7.1.2

 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections;
 
 public class UpdateGPSLocation : MonoBehaviour
 {
     public Text debugText;
     public Text distanceDebug;
 
     LocationInfo myGPSLocation;
     float fiveSecondCounter = 0.0f;
     
     IEnumerator Start()
     {
         debugText.text += "Starting the GPS Script\n";
 
         #if UNITY_ANROID
         Input.compass.enabled = true;
         #endif
 
         return InitializeGPSServices ();
     }
 
     IEnumerator InitializeGPSServices()
     {
         // First, check if user has location service enabled
         if (!Input.location.isEnabledByUser) {
             debugText.text += "GPS disabled by user\n";
             yield break;
         }
 
         // Start service before querying location
         Input.location.Start(0.1f, 0.1f);
 
         // Wait until service initializes
         int maxWait = 20;
         while (Input.location.status == LocationServiceStatus.Initializing && maxWait > 0)
         {
             yield return new WaitForSeconds(1);
             maxWait--;
         }
 
         // Service didn't initialize in 20 seconds
         if (maxWait < 1)
         {
             debugText.text += "Timed out\n";
             yield break;
         }
     }
 
     void Update()
     {
         fiveSecondCounter += Time.deltaTime;
         if (fiveSecondCounter > 5.0)
         {
             UpdateGPS ();
             fiveSecondCounter = 0.0f;
         }
     }
 
     void UpdateGPS()
     {
         // Connection has failed
         if (Input.location.status == LocationServiceStatus.Failed)
         {
             debugText.text += "Unable to determine device location\n";
             Input.location.Stop();
             Start ();
         }
         else
         {
             debugText.text += getUpdatedGPSstring();
         }
     }
 
     string getUpdatedGPSstring()
     {
         myGPSLocation = Input.location.lastData;
 
         return "Location: " + myGPSLocation.latitude + " " + myGPSLocation.longitude + " " + myGPSLocation.altitude + " " + myGPSLocation.horizontalAccuracy + " " + myGPSLocation.timestamp + "\n"; 
     }
 
     public Vector2 GetCoordinate()
     {
         return new Vector2 (myGPSLocation.latitude, myGPSLocation.longitude);
     }
 
     // For the IShowHideListener from the HomeUIObject
     public void OnShow()
     {
         InitializeGPSServices ();
     }
 
     public void OnHide()
     {
         // Stop service if there is no need to query location updates continuously
         Input.location.Stop ();
         debugText.text = "";
 
         // Make sure it immediately updates when the screen shows again
         fiveSecondCounter = 5.1f;    
     }
 }



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 splitpete · Apr 17, 2017 at 10:29 PM 0
Share

Not sure which version of Unity you're on but there was a known bug in 5.5.1 that was causing a problem on Android for this issue. I upgraded to 5.5.2 and it had been fixed.

Here's a link to the issue tracker: https://issuetracker.unity3d.com/issues/android-location-app-crashes-due-to-error-jstring-has-wrong-type-android-dot-location-dot-location

avatar image Asta_D · Apr 25, 2017 at 03:44 PM 0
Share

I have exactly the same problem. Im using Unity 5.6.0f3 and no matter what i do it only gets the gps coordinates once and then never again. I even followed three diffrent tutorials and every time the same ... the gps coordinates dosen't change. even if i walk have a mile. $$anonymous$$y Device is an Galaxy S6 Edge with Android 6.0.1 on it.

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by zapantis88 · May 03, 2017 at 02:26 AM

@Asta_D did you manage to make gps coordinates change?i am in unity 5.3.5p6 having the same problem . @splitpete Should i update to 5.5.2 in order to fix this problem?

Comment
Add comment · Show 2 · 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 splitpete · May 03, 2017 at 02:46 PM 1
Share

The issue tracker I referenced has the earliest version of the bug at 5.5.0f1. That doesn't mean it wasn't present prior to that. You could look for other issue trackers which are more specific to your problem. I'm currently using the latest version 5.6.0 and I don't have this issue.

avatar image zapantis88 splitpete · May 03, 2017 at 06:32 PM 0
Share

@splitpete Thank you.

avatar image
0

Answer by Kaikat · May 09, 2017 at 11:08 PM

Hello,

My classmates and I ended up upgrading to Unity 5.6 and have seen the GPS work on the Android devices where it was previously not working. The GPS now updates, however when we switch screens it doesn't restart but that is a bug on my end. I can share my partially working code (since I don't remember if I changed it) and I'll probably be working on a fix later this week. I have the code hooked up to a text field on screen in order to see the printouts from the GPS every 5 seconds (it doesn't actually update every 5 seconds I believe; I don't think we get that control but I could be wrong).

Edit: Though I must also mention that I saw it begin to work when I changed my Google API code to UnityWebRequest. This is not related and should not have changed anything so I found that very confusing because that code is in a different script. Maybe it just works on 5.6 now.

 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections;
 
 public class UpdateGPSLocation : MonoBehaviour
 {
     public Text debugText;
     public Text distanceDebug;
 
     LocationInfo myGPSLocation;
     float fiveSecondCounter = 0.0f;
 
     float currentDistance;
 
     IEnumerator Start()
     {
         debugText.text += "Starting the GPS Script\n";
         EventManager.TriggerEvent (GameEvent.GPSInitialized);
 
         #if UNITY_ANROID
         Input.compass.enabled = true;
         #endif
 
         return InitializeGPSServices ();
     }
 
     IEnumerator InitializeGPSServices()
     {
         // First, check if user has location service enabled
         if (!Input.location.isEnabledByUser) {
             debugText.text += "GPS disabled by user\n";
             yield break;
         }
 
         // Start service before querying location
         Input.location.Start(0.1f, 0.1f);
 
         // Wait until service initializes
         int maxWait = 20;
         while (Input.location.status == LocationServiceStatus.Initializing && maxWait > 0)
         {
             yield return new WaitForSeconds(1);
             maxWait--;
         }
 
         // Service didn't initialize in 20 seconds
         if (maxWait < 1)
         {
             debugText.text += "Timed out\n";
             yield break;
         }
     }
 
     void Update()
     {
         fiveSecondCounter += Time.deltaTime;
         if (fiveSecondCounter > 5.0)
         {
             UpdateGPS ();
             fiveSecondCounter = 0.0f;
         }
     }
 
     void UpdateGPS()
     {
         // Connection has failed
         if (Input.location.status == LocationServiceStatus.Failed)
         {
             debugText.text += "Unable to determine device location\n";
             Input.location.Stop();
             Start ();
         }
         else
         {
             debugText.text += getUpdatedGPSstring();
         }
     }
 
     string getUpdatedGPSstring()
     {
         myGPSLocation = Input.location.lastData;
         return "Location: " + myGPSLocation.latitude + " " + myGPSLocation.longitude + " " + myGPSLocation.altitude + " " + myGPSLocation.horizontalAccuracy + " " + myGPSLocation.timestamp + "\n"; 
     }
 
     public Vector2 GetCoordinate()
     {
         return new Vector2 (myGPSLocation.latitude, myGPSLocation.longitude);
     }
 
     // For the IShowHideListener from the HomeUIObject
     public void OnShow()
     {
         InitializeGPSServices ();
     }
 
     public void OnHide()
     {
         // Stop service if there is no need to query location updates continuously
         Input.location.Stop ();
         debugText.text = "";
 
         // Make sure it immediately updates when the screen shows again
         fiveSecondCounter = 5.1f;
     }
 }
 
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
0

Answer by Nimdanet · Jun 25, 2019 at 09:48 PM

Hi!

I think your sintax is wrong. Line 21 should be:

 yield return InitializeGPSServices ();
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

160 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 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 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 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 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 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 avatar image avatar image avatar image avatar image

Related Questions

Black Artifacts on some android devices after upgrading from 2017 to 2018 2 Answers

Location Services not starting when system GPS is enabled. 0 Answers

Location service not updating the input value 0 Answers

GPS power consumption 0 Answers

how to place 2d/3d objects in real world based on location and altitude... 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