unity Gets GPS Information of Equipment

Keywords: Unity less Mobile

Unity uses GPS API s


In unity's official documents, there are only two API s related to device positioning (GPS longitude and latitude, horizontal accuracy, etc.) that I have found at present: Location Service and Location Info.

Let's start with a simple understanding:

Location Service is responsible for starting and closing location services.

Location Info obtains location data information after the service starts.


LocationService

Links to official notes:

http://docs.Unity3D.com/Documentation/ScriptReference/LocationService.Start.html

Location Service has three attributes and two methods:

(1) isEnabled ByUser -- Whether the location service in the user settings is enabled. (The measured results show that all of them are true, which seems to have little effect.)

(2) lastData -- Location Info lastData; that is, it's associated with Location Info

(3) Status -- Locate the status of the service.

The status of location services includes:

Stopped
Location service is stopped. Location service has stopped
Initializing
Location service is initializing, some time later it will switch to. Location service is initializing, after a period of time, the state will switch back.
Running
Location service is running and locations can be queried. Location service is running and location can be obtained.
Failed
Location service failed (user denied access to location service). Location service failed (user denied access to location service).


(4) Start () -- Start location service and update location data. Recent updated position coordinates can be obtained.

Data reception is achieved through Input.location.lastData. The service cannot get location data immediately. The code must check Input.location.status to get the current location service status.

Look at the function definition:

void Start(float desiredAccuracyInMeters = 10f, float updateDistanceInMeters = 10f); 

Detailed parameters:

The precision required for desired Accuracy InMeters service is in meters. If you use a higher value, such as 500, you don't usually need to turn on the GPS chip (such as triangulation using the signal base station) to save battery power. Values like 5-10 can be used to achieve optimal accuracy. The default value is 10 meters.

Update Distance InMeters The Input.location property is updated before the device with the smallest distance (in meters) has to move horizontally. Higher values, such as 500, mean less overhead. The default value is 10 meters.

(5) Stop () -- Stop location updates for location services. This is very useful for saving battery power. void


LocationInfo

The attributes are as follows:

(1) altitude - altitude

(2) horizontal Accuracy -- horizontal accuracy

(3) latitude - latitude

(4) longitude -- longitude

(5) timestamp -- the timestamp of the last location, starting in 1970

(6) Vertical Accuracy -- Vertical Accuracy


All of these attributes are float except that timestamp is double.



Here's an example of Unity using GPS

1. Create a new project.

2. Write the script GetGPS.cs and hang it on the main camera.

  1. using UnityEngine;  
  2. using System.Collections;  
  3.   
  4. public class GetGPS : MonoBehaviour {  
  5.   
  6.     public string gps_info = "";  
  7.     public int flash_num = 1;  
  8.   
  9.     // Use this for initialization  
  10.     void Start () {  
  11.       
  12.     }  
  13.       
  14.     void OnGUI () {  
  15.         GUI.skin.label.fontSize = 28;  
  16.         GUI.Label(new Rect(20,20,600,48),this.gps_info);   
  17.         GUI.Label(new Rect(20,50,600,48),this.flash_num.ToString());   
  18.           
  19.         GUI.skin.button.fontSize = 50;  
  20.         if (GUI.Button(new Rect(Screen.width/2-110,200,220,85),"GPS Location"))  
  21.         {  
  22.             //Here you need to start a collaborative program  
  23.             StartCoroutine(StartGPS());  
  24.         }  
  25.         if (GUI.Button(new Rect(Screen.width/2-110,500,220,85),"Refresh GPS"))  
  26.         {  
  27.             this.gps_info = "N:" + Input.location.lastData.latitude + " E:"+Input.location.lastData.longitude;  
  28.             this.gps_info = this.gps_info + " Time:" + Input.location.lastData.timestamp;  
  29.             this.flash_num += 1;   
  30.         }  
  31.     }  
  32.   
  33.     // Input.location = LocationService  
  34.     // LocationService.lastData = LocationInfo   
  35.   
  36.     void StopGPS () {  
  37.         Input.location.Stop();  
  38.     }  
  39.   
  40.     IEnumerator StartGPS () {  
  41.         //Input.location for accessing device location attributes (handheld devices), static Location Service locations  
  42.         //Is Location Service.isEnabledByUser enabled in user settings  
  43.         if (!Input.location.isEnabledByUser) {  
  44.             this.gps_info = "isEnabledByUser value is:"+Input.location.isEnabledByUser.ToString()+" Please turn on the GPS";   
  45.             return false;  
  46.         }  
  47.   
  48.         //LocationService.Start() Starts the update of location service, and the last location coordinate is used  
  49.         Input.location.Start(10.0f, 10.0f);  
  50.   
  51.         int maxWait = 20;  
  52.         while (Input.location.status == LocationServiceStatus.Initializing && maxWait > 0) {  
  53.             //Suspend the execution of the collaborative program (1 second)  
  54.             yield return new WaitForSeconds(1);  
  55.             maxWait--;  
  56.         }  
  57.   
  58.         if (maxWait < 1) {  
  59.             this.gps_info = "Init GPS service time out";  
  60.             return false;  
  61.         }  
  62.   
  63.         if (Input.location.status == LocationServiceStatus.Failed) {  
  64.             this.gps_info = "Unable to determine device location";  
  65.             return false;  
  66.         }   
  67.         else {  
  68.             this.gps_info = "N:" + Input.location.lastData.latitude + " E:"+Input.location.lastData.longitude;  
  69.             this.gps_info = this.gps_info + " Time:" + Input.location.lastData.timestamp;  
  70.             yield return new WaitForSeconds(100);  
  71.         }  
  72.     }  
  73. }  

3. Export to the mobile phone, run, you can see the effect.

Posted by blankextacy on Sat, 30 Mar 2019 01:09:30 -0700