我正在尝试使用手机位置的纬度和经度绘制标签。当我在 pc 上调试时,我返回 0 , 0 。这是意料之中的,因为我在没有 GPS 的 PC 上。PC 版本确实绘制了标签。当我运行相同事物的 android 版本时。标签根本没有绘制 - 我假设是因为它无法正常工作。我的 androidmanifest.xml 已正确设置并具有正确的权限。
这是我的 GPS 代码 - 这是附加到游戏对象的。
using UnityEngine;
using System.Collections;
public class GPS : MonoBehaviour {
float lat ;
float lon ;
float alt ;
float horz ;
float time ;
public GUIStyle myGuiStyle_CORDS;
IEnumerator Start (){
// First, check if user has location service enabled
if (!Input.location.isEnabledByUser)
// 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) {
print ("Timed out");
}
// Connection has failed
if (Input.location.status == LocationServiceStatus.Failed) {
print ("Unable to determine device location");
}
// Access granted and location value could be retrieved
else {
iPhoneSettings.StartLocationServiceUpdates();
print ("Location: " + Input.location.lastData.latitude + " " +
Input.location.lastData.longitude + " " +
Input.location.lastData.altitude + " " +
Input.location.lastData.horizontalAccuracy + " " +
Input.location.lastData.timestamp);
}
// Stop service if there is no need to query location updates continuously
Input.location.Stop ();
}
void OnGUI (){
if (Globals.DisplayMode == "GPSST") {
GUI.depth = 0;
lon = Input.location.lastData.longitude;
lat = Input.location.lastData.latitude;
GUI.Label (new Rect (Globals.displayOffsetX + 5, 110, 100, 50), "" + Input.location.lastData.longitude, myGuiStyle_CORDS);
GUI.Label (new Rect (Globals.displayOffsetX + 5, 130, 100, 50), "" + Input.location.lastData.latitude, myGuiStyle_CORDS);
}
}
}