您的位置:首页 > 移动开发 > Unity3D

【Unity】中如何通过GPS获取设备经纬度(测试脚本)

2017-02-22 17:24 483 查看
在游戏开发中需要使用gps获取经纬度坐标定位玩家的当前位置,那么在开发过程中这个功能容不容易实现呢?下面就给大家介绍下Unity中获取设备经纬度的方法,一起来看看吧。

  

  Unity使用GPS 的API

  

  在unity的官方文档中,与设备定位(GPS经纬度、水平精度等等)相关的API,目前我只找到两个:LocationService 和 LocationInfo 。

  

  先来个简单的理解:

  

  LocationService 负责启动和关闭定位服务。  

  LocationInfo  在服务启动后,获取定位数据信息。

  

  LocationService

  

  LocationService 中有三个属性,和两个方法:

  

  (1)isEnabledByUser   -- 用户设置里的定位服务是否启用。(实测发现,都为true,似乎不大起作用)

  

  (2)lastData   -- 最近一次测量的地理位置(LocationInfo lastData; 也就是要和 LocationInfo 关联了)

  

  (3)status   -- 定位服务的状态。

  

  定位服务的状态包括:

  

  Stopped

  

  Location service is stopped. 定位服务已经停止

  

  Initializing

  

  Location service is initializing, some time later it will switch to.  定位服务正在初始化,在一段时间后,状态会切换回来。

  

  Running

  

  Location service is running and locations could be queried.  位置服务正在运行,位置可以获取。

  

  Failed

  

  Location service failed (user denied access to location service).  位置服务失败(用户拒绝访问位置服务)。

  

  (4)Start ( )   -- 启动定位服务,更新定位数据。可以获取最近更新的位置坐标。

  

  数据接收,是通过 Input.location.lastData 来实现的。服务不能马上获得定位数据。代码必须检查Input.location.status以获取当前的定位服务状态。

  

  看一下函数定义:

  

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

  

  参数详解:

  

  desiredAccuracyInMeters  服务所需的精度,以米为单位。如果使用较高的值,比如500,那么通常不需要打开GPS芯片(比如可以利用信号基站进行三角定位),从而节省电池电量。像5-10这样的值,可以被用来获得最佳的精度。默认值是10米。

  

  updateDistanceInMeters  最小距离(以米为单位)的设备必须横向移动前Input.location属性被更新。较高的值,如500意味着更少的开销。默认值是10米。

  

  (5)Stop ( )  -- 停止定位服务的定位更新。这对节省电池的电量非常有用。void

  

  LocationInfo

  

  属性如下:

  

  (1) altitude -- 海拔高度  

  (2) horizontalAccuracy -- 水平精度  

  (3) latitude -- 纬度  

  (4) longitude -- 经度  

  (5) timestamp -- 最近一次定位的时间戳,从1970开始  

  (6) verticalAccuracy -- 垂直精度

  

  这些属性,除了timestamp为double外, 其余全为 float 型。

  

  Unity使用GPS

  

  1、新建一个项目。

  

  2、编写脚本 GetGPS.cs ,挂到主摄像机上。

[csharp] view
plain copy

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定位"))  

        {  

            // 这里需要启动一个协同程序  

            StartCoroutine(StartGPS());  

        }  

        if (GUI.Button(new Rect(Screen.width/2-110,500,220,85),"刷新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 用于访问设备的位置属性(手持设备), 静态的LocationService位置  

        // LocationService.isEnabledByUser 用户设置里的定位服务是否启用  

        if (!Input.location.isEnabledByUser) {  

            this.gps_info = "isEnabledByUser value is:"+Input.location.isEnabledByUser.ToString()+" Please turn on the GPS";   

            return false;  

        }  

  

        // LocationService.Start() 启动位置服务的更新,最后一个位置坐标会被使用  

        Input.location.Start(10.0f, 10.0f);  

  

        int maxWait = 20;  

        while (Input.location.status == LocationServiceStatus.Initializing && maxWait > 0) {  

            // 暂停协同程序的执行(1秒)  

            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、导出到手机,运行,即可看到效果。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  gps unity api 经纬度 位置