【问题标题】:Unity Device Location in Latitude and LongitudeUnity 设备在经纬度中的位置
【发布时间】:2018-10-04 12:58:24
【问题描述】:

这是我尝试过的,我想要一个 Android 设备的位置(纬度和经度),但它返回 0、0。

private float userLatitude , userLongitude;

void Start()
{
    StartCoroutine(StartLocation());

    if(!Input.location.isEnabledByUser){
        Screen1ErrorMessage.ShowUserMessage("Location is Disabled.");
    }
}

IEnumerator StartLocation(){
    if (!Input.location.isEnabledByUser)
        Screen1ErrorMessage.ShowUserMessage("Enable Location to Play, else you can't be able to Join any Room");
        yield break;

    Input.location.Start();

    int maxWait = 20;
    while (Input.location.status == LocationServiceStatus.Initializing && maxWait > 0)
    {
        yield return new WaitForSeconds(1);
        maxWait--;
    }

    if (maxWait < 1)
    {
        Screen1ErrorMessage.ShowUserMessage("Timeout retrieving device location. Make sure you are connected to the internet");
        yield break;
    }

    if (Input.location.status == LocationServiceStatus.Failed)
    {
        Screen1ErrorMessage.ShowUserMessage("can't obtain device location, Enable Device Location to Play.");
        yield break;
    }
    else
    {
        userLatitude = Input.location.lastData.latitude ;
        userLongitude =  Input.location.lastData.longitude;
        isLocationAcquired = true;
        StartCoroutine(StartLocationUpdate());
    }

    Input.location.Stop();
}

IEnumerator StartLocationUpdate()
{
    string url = "--------------------------------------";

    Screen1ErrorMessage.ShowUserMessage("please wait...");

    WWWForm form = new WWWForm();

    form.AddField("UserId", PlayerPrefs.GetString("userId"));
    form.AddField("longitude", userLongitude.ToString());
    form.AddField("latitude", userLatitude.ToString());


    Debug.Log("Updation Location for User:"+PlayerPrefs.GetString("userId")+" : Latitude :" + userLatitude + " Longitude : "+userLongitude);

    UnityWebRequest www = UnityWebRequest.Post(url, form);
    yield return www.SendWebRequest();

    var Response = JSON.Parse(www.downloadHandler.text);
    if (Response["status"].Value == "true")
        // Screen1ErrorMessage.ShowUserMessage(result.message);
        Debug.Log("User Location Update Success");
    else
    {
        Debug.Log("User Location Update Failed Response ->" + Response.ToString());
    }
}

我已经在我的启动方法中调用了协程,我是否也应该尝试唤醒?我正在尝试基于 n first 的结果调用另一个协程,但它永远不会被调用。这意味着存在定位服务问题。

【问题讨论】:

  • 您从哪里调用 StartLocation() 以及您在哪里尝试访问显示 0,0 的结果?
  • 我在 start() 方法中调用 StartLocation(),在成功初始化 userLatitude 和 userLongitude 后,我调用了同时使用 userLatitude 和 userLongitude 全局参数的 StartCoroutine(StartLocationUpdate())。
  • 请提供您问题的代码。更容易发现问题
  • 请检查一下。
  • 看起来不错。你是从编辑器运行这个吗?将Input.location.lastData.latitudeInput.location.lastData.longitude 放入更新函数会发生什么?

标签: android unity3d geolocation location latitude-longitude


【解决方案1】:

这里也是同样的问题。我正在使用自定义 android 清单文件(构建设置->播放器设置->发布设置->自定义主清单),所以我通过添加编辑自定义清单文件来修复它

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多