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 ();
	}
}



©️2020 CSDN 皮肤主题: 深蓝海洋 设计师:CSDN官方博客 返回首页