我如何要求用户在我的应用程序中启用设备位置(如果未启用)? -团结

时间:2019-07-29 10:59:22

标签: c# android unity3d gps

我正在用统一的C#语言编写一个简单的脚本,在其中检索纬度和经度,然后对其进行处理

 IEnumerator Start()
{
    // First, check if user has location service enabled
    if (!Input.location.isEnabledByUser)
        yield break;

    // 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");
        yield break;
    }

    // Connection has failed
    if (Input.location.status == LocationServiceStatus.Failed)
    {
        print("Unable to determine device location");
        yield break;
    }
    else
    {
        // Access granted and location value could be retrieved
        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();
}

问题是代码可以工作,但前提是已经为用户启用了设备位置。 如果未启用设备位置,则此代码将失效。我试图找出为什么如果不启用设备位置的情况,则会显示类似的提示:

enter image description here

现在按“确定”将在不存在该应用程序的情况下启用设备定位,而这正是我在此处尝试做的事情。

1 个答案:

答案 0 :(得分:0)

关于此主题的解释很多。您可以检查一下!  Turn on location