当前位置:网站首页>Unity location service GPS API

Unity location service GPS API

2022-06-09 06:10:00 about...

Unity Use GPS Of API


stay unity In the official documents of , And equipment positioning (GPS Longitude and latitude 、 Horizontal accuracy, etc ) dependent API, So far I have only found two :LocationService and LocationInfo .

Let's have a simple understanding :

LocationService  Be responsible for starting and closing the location service .

LocationInfo  After the service starts , Get location data information .


LocationService

Official description link :

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

LocationService There are three attributes in , And two ways :

(1)isEnabledByUser   -- Whether the location service in the user settings is enabled .( The measured found , All for true, It doesn't seem to work )

(2)lastData   -- The geographical location of the last measurement (LocationInfo lastData; That is to say, with LocationInfo Associated with the )

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

The status of the location service includes :

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


(4)Start ( )   -- Start location service , Update positioning data . You can get the latest updated position coordinates .

     Data reception , It's through Input.location.lastData To achieve . Services can't get location data right away . The code must be checked Input.location.status To get the current location service status .

Take a look at the function definition :

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

Parameters, :

desiredAccuracyInMeters  The precision required for the service , In meters . If higher value , such as 500, Then you usually don't need to open GPS chip ( For example, the signal base station can be used for triangulation ), This saves battery power . image 5-10 So the value of the , Can be used to obtain the best accuracy . The default value is 10 rice .

updateDistanceInMeters   Minimum distance ( In meters ) The equipment must be moved laterally before Input.location Property is updated . Higher values , Such as 500 Means less overhead . The default value is 10 rice .

(5)Stop ( )  -- Stop the location update of the location service . This is very useful for saving battery power .void


LocationInfo

Properties are as follows :

(1) altitude -- Altitude  

(2) horizontalAccuracy -- Horizontal accuracy

(3) latitude -- latitude

(4) longitude -- longitude

(5) timestamp -- Timestamp of the last location , from 1970 Start

(6) verticalAccuracy -- Vertical accuracy


These attributes , except timestamp by double Outside , The rest are all float type .



Here is Unity Use GPS Example

1, Create a new project .

2, Write a script GetGPS.cs , 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 collaboration 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  Used to access the location properties of the device ( Handheld devices ),  Static LocationService Location   
  42.         // LocationService.isEnabledByUser  Whether the location service in the user settings is enabled   
  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()  Start the update of the location service , The last position coordinate will be used   
  49.         Input.location.Start(10.0f, 10.0f);  
  50.   
  51.         int maxWait = 20;  
  52.         while (Input.location.status == LocationServiceStatus.Initializing && maxWait > 0) {  
  53.             //  Pause the execution of the collaboration 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 mobile phone , function , You can see the effect .


Transfer to :http://blog.csdn.net/chenggong2dm/article/details/24488469


原网站

版权声明
本文为[about...]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/160/202206090559518647.html