0

我正在尝试使用手机位置的纬度和经度绘制标签。当我在 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);


    }
}


}
4

2 回答 2

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; 您知道服务正在运行的地方

祝你好运,编码愉快;

于 2013-11-15T02:21:07.717 回答
0

可能的原因

1) 您在 Start() 的最后一行停止定位服务。您是否忘记了 if 语句中的返回命令?如:

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

于 2013-11-14T12:46:48.437 回答