Unity3d,获取GPS定位信息

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class GPSManager : MonoBehaviour
{

	public Text txt;

	public void GetGPS ()
	{
		StartCoroutine (StartGPS ());  
	}

	IEnumerator StartGPS ()
	{
		txt.text = "开始获取GPS信息";

		// 检查位置服务是否可用
		if (!Input.location.isEnabledByUser) {
			txt.text = "位置服务不可用";
			yield break;
		}

		// 查询位置之前先开启位置服务
		txt.text = "启动位置服务";
		Input.location.Start ();

		// 等待服务初始化
		int maxWait = 20;
		while (Input.location.status == LocationServiceStatus.Initializing && maxWait > 0) {
			txt.text = Input.location.status.ToString () + ">>>" + maxWait.ToString ();
			yield return new WaitForSeconds (1);
			maxWait--;
		}

		// 服务初始化超时
		if (maxWait < 1) {
			txt.text = "服务初始化超时";
			yield break;
		}

		// 连接失败
		if (Input.location.status == LocationServiceStatus.Failed) {
			txt.text = "无法确定设备位置";
			yield break;
		} else {
			txt.text = "Location: \r\n" +
			"纬度:" + Input.location.lastData.latitude + "\r\n" +
			"经度:" + Input.location.lastData.longitude + "\r\n" +
			"海拔:" + Input.location.lastData.altitude + "\r\n" +
			"水平精度:" + Input.location.lastData.horizontalAccuracy + "\r\n" +
			"垂直精度:" + Input.location.lastData.verticalAccuracy + "\r\n" +
			"时间戳:" + Input.location.lastData.timestamp;
		}

		// 停止服务,如果没必要继续更新位置,(为了省电)
		Input.location.Stop ();
	}
}



评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值