Unity获取经纬度
Unity提供了Input.location,方便我们获取经纬度。不过,只能获取经纬度,如果想要通过经纬度得出具体省份城市,则需要通过SDK接口了。
测试结果
场景如下
测试结果如下
代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Main : MonoBehavIoUr {
public Text infoText;
public Button getLocationBtn;
void Start()
{
getLocationBtn.onClick.AddListener (() => {
StartCoroutine (StartLocate ());
});
}
IEnumerator StartLocate()
{
if (!Input.location.isEnabledByUser)
{
Input.location.Start();
infoText.text = "false == location.isEnabledByUser";
yield break;
}
// Start service before querying location
Input.location.Start();
// Wait until service initializes
int maxWait = 20;
while (Input.location.status == LocationServiceStatus.Initializing && maxWait > 0)
{
yield return new WaitForSeconds(1);
maxWait--;
}
// Service didn't initialize in 20 seconds
if (maxWait < 1)
{
var logStr = "Timed out";
infoText.text = logStr;
Debug.Log (logStr);
yield break;
}
// Connection has Failed
if (Input.location.status == LocationServiceStatus.Failed)
{
var logStr = "Unable to determine device location";
infoText.text = logStr;
Debug.Log (logStr);
yield break;
}
else
{
// Access granted and location value Could be retrieved
var logStr = "Location,纬度: " + Input.location.lastData.latitude + ",经度: " + Input.location.lastData.longitude + ",高度: " + Input.location.lastData.altitude + "\n水平精确度: " + Input.location.lastData.horizontalAccuracy + ",时间戳: " + Input.location.lastData.timestamp;
infoText.text = logStr;
Debug.Log (logStr);
//取出位置的经纬度
string str = Input.location.lastData.longitude + "," + Input.location.lastData.latitude;
}
// Stop service if there is no need to query location updates continuously
Input.location.Stop();
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。