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

unity 定位

2015-12-27 22:45 549 查看
总结一下前段时间做的地理定位功能吧

需求:定位玩家的当前位置,(不用太精细)

实现步骤:(说明,因为项目不需要定位太过精细,所以将逆地址解析放在了服务端,如果需要精细的定位的话,需要在客户端接SDK的)

1、使用unity的api获取当前设备的经纬度,

\

2、将经纬度上传到服务端,

3、通过服务端访问百度或者高德的定位服务(这一步不再细说,需要注册百度或高德的开发者账户然后获得key)

4、从服务端获得地址解析的信息

具体实现如下:

[code] IEnumerator StartGPS()

   {

       //MyDebuger.LogWarning("IEnumerator StartGPS()");

        GetGPSStatus status = GetGPSStatus.Initializing;

        // Input.location 用于访问设备的位置属性(手持设备), 静态的LocationService位置  

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

        if (!Input.location.isEnabledByUser)

        {

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

        }

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

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

        int maxWait = 20;

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

        {

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

            yield return new WaitForSeconds(1);

            maxWait--;

        }

        if (Input.location.status == LocationServiceStatus.Failed)

        {

            gps_info = "Unable to determine device location";

        }

        else

        {

            gps_info = "N南北:" + Input.location.lastData.latitude + " E东西:" + Input.location.lastData.longitude;

            gps_info = gps_info + " Time:" + Input.location.lastData.timestamp;

        }

        if (maxWait < 1)

        {

            gps_info = "超时";//"Init GPS service time out";

            status = GetGPSStatus.OutTime;

        }

         else

        {

            status = (GetGPSStatus)Enum.Parse(typeof(GetGPSStatus),Input.location.status.ToString());

        }

        StopGPS();

        List<object> list_obj = new List<object>();

        list_obj.Add(status);

        //UIMgr.Single().postMsg((int)_eUiMsg.GetGPSStatus,list_obj);

         //发送定位点的经纬度

    }


其中的Input.location.lastData.latitude 和 Input.location.lastData.longitude就是获得的设备经纬度
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: