Unity3D 获取地理坐标启用locationInfo

LocationService:
这是一个unity提供的地理位置的接口。

三个属性:
●isEnabledByUser:检测是否有了GPS开启。
●lastData:上一次更新位置信息的数据,初始化都为0。LocationInfo类型。
●status:更新进程的状态。状态有Stopped、Initaializing、Running、Failed,这四个状态。

两个方法:
●Start:开始更新位置信息。
●Stop:停止更新位置信息。

LocationInfo:
这是获得地理位置的数据体。属性有:
●altitude:海拔
●horizontalAccuracy:水平精度
●verticalAccuracy:垂直精度
●latitude:经度
●longitude:纬度
●timestamp:时间节点

方法一:

private IEnumerator GetLocationInfo()
{
    //先判断是否打开了定位
    if (!Input.location.isEnabledByUser)
    {
        Debug.Log("没有打开定位");
        yield break;
    }
    //这里开起定位计算
    Input.location.Start();
    //时间为20秒
    var maxWait = 20;
    while (Input.location.status == LocationServiceStatus.Initializing)
    {
        yield return new WaitForSeconds(1);
        maxWait--;
    }
    if (maxWait < 1)
    {
        Debug.Log("时间到");
        yield break;
    }
    if (Input.location.status == LocationServiceStatus.Failed)
    {
        Debug.Log("无法使用定位");
        yield break;
    }
    Debug.Log("经纬度: " + Input.location.lastData.latitude + " " + Input.location.lastData.longitude + " " +
              Input.location.lastData.altitude + " " + Input.location.lastData.horizontalAccuracy + " " +
              Input.location.lastData.timestamp);
    Input.location.Stop();
}

方法二:

  private const float EARTH_RADIUS = 6371010;
    private string lat;
    private string lon;
    private string Coordinate;

   /// <summary>
    /// 启用GPS定位
    /// </summary>
    /// <returns></returns>
    IEnumerator StartGPS()
    {
        // Input.location 用于访问设备的位置属性(手持设备), 静态的LocationService位置
        // LocationService.isEnabledByUser 用户设置里的定位服务是否启用
        if (!Input.location.isEnabledByUser)
        {
            Debug.Log("请启用GPS定位");
            yield break;
        }
        // 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)
        {
            Debug.Log("GPS服务超时");
            yield break;
        }
        if (Input.location.status == LocationServiceStatus.Failed)
        {
            Debug.Log("无法确定设备位置");
            yield break;
        }
        else
        {
            Debug.Log("N:" + (Input.location.lastData.latitude) + " E:" + (Input.location.lastData.longitude));
            Debug.Log(" Time:" + Input.location.lastData.timestamp);
            Coordinate = ConvertGPS.Gpsgps84_To_Gcj02(Input.location.lastData.latitude, Input.location.lastData.longitude);
            lat = Coordinate.Substring(0, Coordinate.LastIndexOf("|"));
            lon = Coordinate.Substring(Coordinate.LastIndexOf("|") + 1);
            Debug.Log(lat + ":::" + lon);
            InitMaps(double.Parse(lat), double.Parse(lon));
            yield return new WaitForSeconds(100);
        }
    }

    /// <summary>
    /// 关闭GPS
    /// </summary>
    void StopGPS()
    {
        Input.location.Stop();
    }

 

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值