Unity 获得当前经纬度 用于求 设备间的距离

今天项目 遇到一个求 设备间距离的需求。考虑经纬度来实现,拷贝别人代码,加上距离函数做了个demo,主要是获得经纬度。亲测 出包Ios 与android 皆可用(Android 注意要将设置里面的位置 打开),即可实现获得当前经纬度,至于距离方面地理数学知识就可以解决了,下面也实现了,不过没测距离是否准确,先完成任务先。


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 = 22;
        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"; 
        }else{
            // LocationService.Start() 启动位置服务的更新,最后一个位置坐标会被使用
            Input.location.Start(300.0f, 300.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";
            }else if (Input.location.status == LocationServiceStatus.Failed) {
                this.gps_info = "Unable to determine device location";
            }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);
            }
        }
    }


    //参数为两个经纬度的数值 获得之间的间距

    public static float getDistance(float lat1,float lng1,float lat2,float lng2)

    {
        float a, b, R;
        R = 6378137; //地球半径

        lat1 = lat1 * Mathf.PI / 180.0f;
        lat2 = lat2 * Mathf.PI / 180.0f;


        a = (lat1 - lat2);
        b = (lng1 - lng2) * Mathf.PI / 180.0f;

        float d;
        float sa2, sb2;
        sa2 = Mathf.Sin(/ 2.0f);
        sb2 = Mathf.Sin(/ 2.0f);

        d = 2 * R * Mathf.Asin(Mathf.Sqrt(sa2 * sa2 + Mathf.Cos(lat1) * Mathf.Cos(lat2) * sb2 * sb2));

        return d;
    }

    private static float rad(float d)
    {
        return d * Mathf.PI / 180.0f;
    }
}
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值