Unity之GPS定位详解

版权声明:本文为博主原创文章,转载请注明出处。 https://blog.csdn.net/qq563129582/article/details/78050504

关于gps的定位服务,原生的IOS和Android都可以实现,但是Unity和ios、android的交互略微麻烦。因此Unity官方给我们提供好了关于gps的服务,就在我们熟悉的Input当中。他就是Input.Location;


相关的API,有两个:LocationService 和 LocationInfo 。

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

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


LocationService

官方说明链接:http://docs.unity3d.com/Documentation/ScriptReference/LocationService.Start.html

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

(1)isEnabledByUser   -- 检测用户设置里的定位服务是否启用(首次会弹出提示,询问用户是否同意。)

(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 ( )  -- 停止定位服务的定位更新。对节省电池的电量非常有用。


LocationInfo

属性如下:

(1) altitude -- 海拔高度 

(2) horizontalAccuracy -- 水平精度 

(3) latitude -- 纬度 

(4) longitude -- 经度 

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

(6) verticalAccuracy -- 垂直精度 


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


官方脚本:

using UnityEngine;
using System.Collections;

public class TestLocationService : MonoBehaviour
{
    IEnumerator Start()
    {
        // First, check if user has location service enabled
        if (!Input.location.isEnabledByUser)
            yield break;

        // Start service before querying location
        Input.location.Start();

        // 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)
        {
            print("Timed out");
            yield break;
        }

        // Connection has failed
        if (Input.location.status == LocationServiceStatus.Failed)
        {
            print("Unable to determine device location");
            yield break;
        }
        else
        {
            // Access granted and location value could be retrieved
            print("Location: " + Input.location.lastData.latitude + " " + Input.location.lastData.longitude + " " + Input.location.lastData.altitude + " " + Input.location.lastData.horizontalAccuracy + " " + Input.location.lastData.timestamp);
        }

        // Stop service if there is no need to query location updates continuously
        Input.location.Stop();
    }
}

问题跟踪:

Android闪退:

1.Unity5.6以下,LocationServer存在一定的缺陷,实测android5.0以上机型,部分闪退。官方于5.6.1修复该问题。建议使用Unity5.6.1以上版本。

2.部分android机即使用5.6.x以上打测试包APK,依然闪退。

a>当手机弹出警告框“是否允许使用定位服务”时,一定选择允许。

b>检查设置中隐私中,是否对应用的定位服务开启。

b>有些手机允许了定位服务,隐私中也确实开启了定位服务。但是依然闪退,原因:以小米手机为例,有一个模式叫:对未知应用的信任,如果选择否,那么手机是只信任正式渠道的签名APK,我们的测试APK包都属于“未知应用”,因此即使在隐私的定位中开启了对该应用的定位服务,但是由于手机就不信任“未知应用”,所以依然会闪退。

IOS闪退:

1.添加Info.plist中的权限就OK了;

a>NSLocationAlwaysUsageDescription

b>NSLocationWhenInUseUsageDescription


猜你喜欢

转载自blog.csdn.net/qq563129582/article/details/78050504