/// <summary> /// 在 Start 取得用戶地理位置 /// </summary> IEnumerator GetLocation(bool needCallBack, int radius = 0, GetResNamesDel callBackEvent = null) { getLocSucceed = false; isLoadingLoc = true; // First, check if user has location service enabled if (!Input.location.isEnabledByUser) { OnGetLocationFailed("未開啟 GPS"); isLoadingLoc = false; 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) { OnGetLocationFailed("讀取裝置位置時等待逾時"); isLoadingLoc = false; yield break; } // Connection has failed if (Input.location.status == LocationServiceStatus.Failed) { OnGetLocationFailed("無法讀取裝置位置"); isLoadingLoc = false; yield break; } else { // Access granted and location value could be retrieved lat = Input.location.lastData.latitude; lng = Input.location.lastData.longitude; getLocSucceed = true; isLoadingLoc = false; if (needCallBack) { StartCoroutine(GetNames(lat, lng, radius, callBackEvent)); } } // Stop service if there is no need to query location updates continuously Input.location.Stop(); }
//public void GetAllResAndDetails(double lat, double lng, int radius, GetDetailsDel callBackEvent) { // //StartCoroutine(GetInfo(lat, lng, radius, callBackEvent)); //} public void GetResNames(int radius, GetResNamesDel callBackEvent) { #if (UNITY_EDITOR || UNITY_STANDALONE) // 在 Editor 或 電腦執行下,用測試座標 (中和連城路) StartCoroutine(GetNames(24.99579212f, 121.48876185f, radius, callBackEvent)); return; #endif if (getLocSucceed) { StartCoroutine(GetNames(lat, lng, radius, callBackEvent)); } else { StartCoroutine(GetLocation(true, radius, callBackEvent)); } }
IEnumerator GetNames(float lat, float lng, int radius, GetResNamesDel callBackEvent) { StringBuilder url = new StringBuilder(); url.Append("https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=") .Append(lat.ToString()).Append(",").Append(lng.ToString()).Append("&radius=").Append(radius.ToString()) .Append("&type=restaurant&language=zh-TW&rankby =distance&key=").Append(googlePlaceKey); WWW www = new WWW(url.ToString()); yield return(www); JArray resultsArray = (JArray)JObject.Parse(www.text)["results"]; List <string> names = new List <string>(); #region =========================== 取得餐廳列表 =========================== for (int i = 0; i < resultsArray.Count; i++) { JObject result = (JObject)resultsArray[i]; string name = result.Value <string>("name"); // 取得餐廳名 Details d; if (!resDetailDict.TryGetValue(name, out d)) { d = new Details(); } d.name = name; d.permanentlyClosed = false; d.placeID = result.Value <string>("place_id"); d.visited = false; names.Add(name); resDetailDict[name] = d; } #endregion ================================================================== if (callBackEvent != null) { callBackEvent(names); } }