我试图用手机位置的Lat和Lon绘制标签。 当我在电脑上调试时,我返回0,0。这是预料到的,因为我在没有电脑的电脑上 全球定位系统。 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);
}
}
}
答案 0 :(得分:0)
可能的原因
1)您正在Start()的最后一行停止位置服务。您是否忘记了if语句中的return-commands?如:
if (!Input.location.isEnabledByUser)
return;
if (maxWait < 1) {
print ("Timed out");
return;
}
2)你的第一个if语句与Input.location.Start()
有关if (!Input.location.isEnabledByUser)
Input.location.Start()
也许这不是故意的?
3)OnGUI()中的if语句返回false
答案 1 :(得分:0)
首先,在Start()方法
中激活您的位置服务string message = "";
int maxwait = 20; //my top waiting time for location start
Input.location.Start(1,1);
While(Input.location.status == LocationServiceStatus.Initializing && maxWait > 0)
{
new WaitForSeconds(1f);
maxWait--;
}
if(Input.location.status == LocationServiceStatus.Failed)
message = "Unable to run location";
else
{
if(Input.location.status == LocationServiceStatus.Running)
message = "location Running";
else
message = Input.location.status.ToString();
}
这是我使用的,也是激活指南针,但我认为你没有使用 如果你是,那么只需添加Input.compass.enabled = true;你知道服务在哪里运行
祝你好运,编码愉快;