当前位置:网站首页>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 .
(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 .
- using UnityEngine;
- using System.Collections;
- public class GetGPS : MonoBehaviour {
- public string gps_info = "";
- public int flash_num = 1;
- // Use this for initialization
- void Start () {
- }
- void OnGUI () {
- GUI.skin.label.fontSize = 28;
- GUI.Label(new Rect(20,20,600,48),this.gps_info);
- GUI.Label(new Rect(20,50,600,48),this.flash_num.ToString());
- GUI.skin.button.fontSize = 50;
- if (GUI.Button(new Rect(Screen.width/2-110,200,220,85),"GPS location "))
- {
- // Here you need to start a collaboration program
- StartCoroutine(StartGPS());
- }
- if (GUI.Button(new Rect(Screen.width/2-110,500,220,85)," Refresh GPS"))
- {
- this.gps_info = "N:" + Input.location.lastData.latitude + " E:"+Input.location.lastData.longitude;
- this.gps_info = this.gps_info + " Time:" + Input.location.lastData.timestamp;
- this.flash_num += 1;
- }
- }
- // Input.location = LocationService
- // LocationService.lastData = LocationInfo
- void StopGPS () {
- Input.location.Stop();
- }
- IEnumerator StartGPS () {
- // Input.location Used to access the location properties of the device ( Handheld devices ), Static LocationService Location
- // LocationService.isEnabledByUser Whether the location service in the user settings is enabled
- if (!Input.location.isEnabledByUser) {
- this.gps_info = "isEnabledByUser value is:"+Input.location.isEnabledByUser.ToString()+" Please turn on the GPS";
- return false;
- }
- // LocationService.Start() Start the update of the location service , The last position coordinate will be used
- Input.location.Start(10.0f, 10.0f);
- int maxWait = 20;
- while (Input.location.status == LocationServiceStatus.Initializing && maxWait > 0) {
- // Pause the execution of the collaboration program (1 second )
- yield return new WaitForSeconds(1);
- maxWait--;
- }
- if (maxWait < 1) {
- this.gps_info = "Init GPS service time out";
- return false;
- }
- if (Input.location.status == LocationServiceStatus.Failed) {
- this.gps_info = "Unable to determine device location";
- return false;
- }
- else {
- this.gps_info = "N:" + Input.location.lastData.latitude + " E:"+Input.location.lastData.longitude;
- this.gps_info = this.gps_info + " Time:" + Input.location.lastData.timestamp;
- yield return new WaitForSeconds(100);
- }
- }
- }
3, Export to mobile phone , function , You can see the effect .
Transfer to :http://blog.csdn.net/chenggong2dm/article/details/24488469
边栏推荐
- MT2712 USB
- Le Conseil de développement ITop - 2k1000 démarre ramdisk - make Start USB
- C# Lambda表达式
- Topic24——3. Longest substring without duplicate characters
- Helvetic Coding Contest 2017 online mirror (teams allowed, unrated) K - Send the Fool Further! (medi
- 图像处理-特征融合相关延伸
- Interpretation of join method in thread
- Analysis of the most complete webrtc server technology selection in history
- JS to obtain URL parameters and perform Chinese parsing
- C iterator
猜你喜欢

DBeaver导出查询数据sql文件

Detailed understanding and learning of transactions in MySQL (transaction management, transaction isolation level, transaction propagation mechanism)

测试和线程

CountDownLatch

SSC spread spectrum

Parallels Desktop installation windows10 prompt "safe startup function prevents operating system startup" solution

Here comes the era of metaltc2.0

传输介质双绞线和光纤及二进制

Shopify theme style development

ORACLE锁表解决办法
随机推荐
小米4安装微信失败
[reprint] LCD common interface principle
NAND flash Basics
Thread interrupted detailed parsing
unity 定位服务GPS API
图像处理-特征融合相关延伸
对多旅行商问题:应用、方法和分类进行了全面的综述
Velocity VM template code format problem
IP地址的分配原理
El table scroll bar style
srs-nodejs
Use__ proto__ To assign prototypes
Complete webrtc video recording in five minutes
Topic25——4. 寻找两个正序数组的中位数
Wireshark图解TCP三次握手与四次挥手
C# 匿名函数
测试和线程
Wireshark illustrates TCP three handshakes and four waves
Jitsi meet video recording with jibri
复杂流程挖掘技术隐藏于平民化产品背后-专访容智信息创始人柴亚团