using System.Collections; using UnityEngine; public static class GPSAddress { public static void LocalAddress(LuaInterface.LuaFunction callback) { //2018版本之后需要检查运行时权限 #if UNITY_ANDROID // 先检查权限 if (!UnityEngine.Android.Permission.HasUserAuthorizedPermission(UnityEngine.Android.Permission.FineLocation)) { UnityEngine.Android.Permission.RequestUserPermission(UnityEngine.Android.Permission.FineLocation); // 异步:用户点完才会生效,这里先返回失败,或者做延迟处理 callback.Call(false, 0, 0); callback.Dispose(); return; } #endif if (!Input.location.isEnabledByUser) { //tex_address.text = "定位失败"; //Debugger.LogError("isEnabledByUser value is:" + Input.location.isEnabledByUser.ToString() + " Please turn on the GPS"); callback.Call(false, 0, 0); callback.Dispose(); return; } GameApplication.Instance.StartCoroutine(StartGPS(callback)); } //public static void NetAddress(FairyGUI.GTextField tex_address, float latitude, float longitude) //{ // GameApplication.Instance.StartCoroutine(GetAddress(tex_address, latitude, longitude)); //} public static bool IsGPSEnable() { return Input.location.isEnabledByUser; } static IEnumerator StartGPS(LuaInterface.LuaFunction callback) { //重复检查先注释了 if (!Input.location.isEnabledByUser) { callback.Call(false, 0, 0); callback.Dispose(); yield break; } Input.location.Start(10f, 10f); //int maxWait = 8; //ChatGPT推荐延长时间为20秒 int maxWait = 20; while (Input.location.status == LocationServiceStatus.Initializing && maxWait > 0) { // 暂停协同程序的执行(1秒) yield return new WaitForSeconds(1); maxWait--; } if (maxWait < 1) { // Debugger.LogError( "Init GPS service time out"); callback.Call(false, 0, 0); callback.Dispose(); yield break; } if (Input.location.status == LocationServiceStatus.Failed) { //Debugger.LogError("Unable to determine device location"); callback.Call(false, 0, 0); callback.Dispose(); yield break; } callback.Call(true, Input.location.lastData.latitude, Input.location.lastData.longitude); callback.Dispose(); Input.location.Stop(); } //static IEnumerator GetAddress(FairyGUI.GTextField tex_address,float latitude,float longitude) //{ // string url = string.Format("http://api.map.baidu.com/geocoder/v2/?ak=pPGNKs75nVZPloDFuppTLFO3WXebPgXg&callback=renderReverse&location={0},{1}&output=json&pois=0", // latitude, longitude); // WWW www = new WWW(url); // yield return www; // if (string.IsNullOrEmpty(www.error)) // { // var txt = www.text; // txt = txt.Replace("renderReverse&&renderReverse(", ""); // txt = txt.Replace(")", ""); // var json = MiniJSON.Json.Deserialize(txt) as Hashtable; // var result = json["result"] as Hashtable; // Debugger.Log(result["formatted_address"]); // tex_address.text = (string)result["formatted_address"]; // } // Input.location.Stop(); //} }