纳金网
标题:
Unity如何在ios平台使用GPS地址位置定位?
[打印本页]
作者:
狂风大尉
时间:
2015-1-31 18:19
标题:
Unity如何在ios平台使用GPS地址位置定位?
通过iPhoneSettings.StartLocationServiceUpdates()方法开始定位服务的更新。最后通过iPhoneInput.lastLocation可以收回位置坐标变量。
API: static void
StartLocationServiceUpdates
( floatdesiredAccuracyInMeters = 10f,float updateDistanceInMeters = 10f)
参数1:desiredAccuracyInMeters – 理想服务精确度(单位是米)。使用更高的值像500通常不需要打开GPS芯片从而保持电池电量,像5-10的值可以被用来得到最好的精确度。默认值是10米。
参数2:updateDistanceInMeters – 最小的距离(单位是米)的一种服务在横向移动之前必须更新
iPhoneInput.lastLocation
属性。像500意味着更少的开销。默认的是10米。
代码如下:
void Start () {
// 开始服务在查询定位之前
iPhoneSettings.StartLocationServiceUpdates();
// 等待知道服务初始化
//iPhoneSettings.locationServiceStatus为当前服务状态
int maxWait = 20;
while (iPhoneSettings.locationServiceStatus == LocationServiceStatus.Initializing && maxWait > 0) {
yield return WaitForSeconds(1);
maxWait--;
}
// 在20秒内服务没有初始化
if (maxWait < 1) {
print("Timed out");
return;
}
// 用户拒绝访问定位服务
if (iPhoneSettings.locationServiceStatus == LocationServiceStatus.Failed) {
print("User denied access to device location");
return;
}
// 被给予许可并且定位数值可以取回
else {
print("Location: " + iPhoneInput.lastLocation.latitude + " " +
iPhoneInput.lastLocation.longitude + " " +
iPhoneInput.lastLocation .altitude+ " " +
iPhoneInput.lastLocation.horizontalAccuracy + " " +
iPhoneInput.lastLocation.timestamp);
}
// 如果不需要持续查询刷新定位停止服务
iPhoneSettings.StopLocationServiceUpdates();
}
复制代码
作者:
hariboot
时间:
2015-1-31 18:52
Thanks for sharing!
欢迎光临 纳金网 (http://go.narkii.com/club/)
Powered by Discuz! X2.5