Android C#GPS位置未绘制到屏幕?
我正试图用我手机位置的纬度和经度来画一个标签。 在pc上调试时,返回0,0。这是意料之中的,因为我在一台没有电脑的电脑上 全球定位系统。电脑版本确实画出了标签。当我运行相同的android构建时。标签根本没有画出来——我想是因为它工作不正常。 My androidmanifest.xml已正确设置并具有正确的权限 这是我的GPS代码-这是附加到游戏对象上的Android C#GPS位置未绘制到屏幕?,android,gps,location,unity3d,Android,Gps,Location,Unity3d,我正试图用我手机位置的纬度和经度来画一个标签。 在pc上调试时,返回0,0。这是意料之中的,因为我在一台没有电脑的电脑上 全球定位系统。电脑版本确实画出了标签。当我运行相同的android构建时。标签根本没有画出来——我想是因为它工作不正常。 My androidmanifest.xml已正确设置并具有正确的权限 这是我的GPS代码-这是附加到游戏对象上的 using UnityEngine; using System.Collections; public class
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);
}
}
}
使用UnityEngine;
使用系统集合;
公共类GPS:单一行为{
浮动车床;
浮子离子;
浮动alt;
浮动地轴;
浮动时间;
公共GUIStyle myGuiStyle_跳线;
IEnumerator启动(){
//首先,检查用户是否启用了位置服务
如果(!Input.location.isEnabledByUser)
//在查询位置之前启动服务
Input.location.Start();
//等待服务初始化
int maxWait=20;
while(Input.location.status
==LocationServiceStatus.Initializing&&maxWait>0){
返回新的WaitForSeconds(1);
maxWait--;
}
//服务在20秒内没有初始化
if(maxWait<1){
打印(“超时”);
}
//连接失败
if(Input.location.status==LocationServiceStatus.Failed){
打印(“无法确定设备位置”);
}
//已授予访问权限,无法检索位置值
否则{
iPhoneSettings.StartLocationServiceUpdates();
打印(“位置:+Input.Location.lastData.latitude+”)+
Input.location.lastData.longitude+“”+
Input.location.lastData.altitude+“”+
Input.location.lastData.horizontalAccuracy+“”+
输入.location.lastData.timestamp);
}
//如果不需要连续查询位置更新,则停止服务
Input.location.Stop();
}
void OnGUI(){
如果(Globals.DisplayMode==“GPSST”){
GUI.depth=0;
lon=Input.location.lastData.longitude;
lat=Input.location.lastData.LATIONE;
GUI.Label(新的Rect(Globals.displayOffsetX+51110100,50),“”+Input.location.lastData.longitude,myGuiStyle_线);
GUI.Label(新的Rect(Globals.displayOffsetX+5130100,50),“+Input.location.lastData.latitude,myGuiStyle_-CORDS);
}
}
}
可能的原因
1) 您正在Start()的最后一行停止位置服务。您是否忘记了if语句中的返回命令?例如:
if (!Input.location.isEnabledByUser)
return;
if (maxWait < 1) {
print ("Timed out");
return;
}
也许这不是故意的
3) OnGUI()中的if语句返回false首先,在Start()方法中激活位置服务 这是我用过的,也激活了指南针,但我想你没有用 如果是,则只需添加Input.compass.enabled=true;您知道服务正在运行的地方
祝你好运和快乐 最有可能的情况是,你正在手机摄像头外画画。您是否尝试过查看Globals.displayOffsetX的值?为什么不改用Screen.Width呢?Globals.displayOffsetX是40,40。在我的手机上,我还有其他类似的东西。添加return stations给了我-Assets/main menu/Scripts/GPS.cs(17,25):错误CS0126:return语句需要一个类型可转换为“bool”的对象。这是因为您使用的是用于协同路由的IEnumerator。我想你正在使用协同程序,否则你就不需要了。您可以退出带有
yield break的协同程序代码>而不是返回
if (!Input.location.isEnabledByUser)
Input.location.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();
}