1.通過GPS獲取經(jīng)緯度
直接上代碼了
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class GetGPS : MonoBehaviour { string GetGps = ""; //public Button updateBtn; Vector2 jingWei = new Vector2(); // Start is called before the first frame update void Start() { #region 初始化一次位置 StartCoroutine(StartGPS()); GetGps = "N:" + Input.location.lastData.latitude + " E:" + Input.location.lastData.longitude; jingWei.x = Input.location.lastData.latitude; jingWei.y = Input.location.lastData.longitude; GetGps = GetGps + " Time:" + Input.location.lastData.timestamp; //Debug.Log(GetGps); #endregion //updateBtn.onClick.AddListener(updateGps); } /// <summary> /// 刷新位置信息 /// </summary> public Vector2 updateGps() { StartCoroutine(StartGPS()); GetGps = "N:" + Input.location.lastData.latitude + " E:" + Input.location.lastData.longitude; jingWei.x = Input.location.lastData.latitude; jingWei.y = Input.location.lastData.longitude; GetGps = GetGps + " Time:" + Input.location.lastData.timestamp; Debug.Log(GetGps); return jingWei; } IEnumerator StartGPS() { // Input.location 用于訪問設(shè)備的位置屬性(手持設(shè)備), 靜態(tài)的LocationService位置 // LocationService.isEnabledByUser 用戶設(shè)置里的定位服務(wù)是否啟用 if (!Input.location.isEnabledByUser) { GetGps = "isEnabledByUser value is:" + Input.location.isEnabledByUser.ToString() + " Please turn on the GPS"; yield return false; } // LocationService.Start() 啟動(dòng)位置服務(wù)的更新,最后一個(gè)位置坐標(biāo)會(huì)被使用 Input.location.Start(10.0f, 10.0f); int maxWait = 20; while (Input.location.status == LocationServiceStatus.Initializing && maxWait > 0) { // 暫停協(xié)同程序的執(zhí)行(1秒) yield return new WaitForSeconds(1); maxWait--; } if (maxWait < 1) { GetGps = "Init GPS service time out"; yield return false; } if (Input.location.status == LocationServiceStatus.Failed) { GetGps = "Unable to determine device location"; yield return false; } else { GetGps = "N:" + Input.location.lastData.latitude + " E:" + Input.location.lastData.longitude; jingWei.x = Input.location.lastData.latitude; jingWei.y = Input.location.lastData.longitude; GetGps = GetGps + " Time:" + Input.location.lastData.timestamp; yield return new WaitForSeconds(100); } } /// <summary> /// 停止刷新位置(節(jié)省手機(jī)電量) /// </summary> void StopGPS() { Input.location.Stop(); } }
2.通過百度的逆地理編碼API進(jìn)行經(jīng)緯度跟中文地址的轉(zhuǎn)化
進(jìn)入百度地圖開放平臺(tái)
https://lbsyun.baidu.com/apiconsole/quota#/home

創(chuàng)建服務(wù)端類型的應(yīng)用
這是逆地理編碼的接口說明:
https://lbsyun.baidu.com/faq/api?title=webapi/guide/webservice-geocoding-abroad-base
這是API調(diào)用的問題文檔: https://lbsyun.baidu.com/faq/search?id=260
下面上代碼:
using Newtonsoft.Json; using Newtonsoft.Json.Linq; using System.Collections; using UnityEngine; public class AddressTool : MonoBehaviour { //https://api.map.baidu.com/reverse_geocoding/v3/?ak=您的ak&output=json&coordtype=wgs84ll&location=31.225696563611,121.49884033194 //這里使用的是百度開放平臺(tái) 應(yīng)用類別:服務(wù)端 的AK string ak = ""; float latitude; float longitude; string url; //string url = "https://api.map.baidu.com/reverse_geocoding/v3/?ak="+ ak + "&output=json&coordtype=wgs84ll&location="; [SerializeField] GetGPS getGPS; void Start() { //北京 latitude = 40f; longitude = 116.467f; GetDiDian(); //天津 latitude = 39.12169f; longitude = 117.7334f; GetDiDian(); //url = "https://api.map.baidu.com/reverse_geocoding/v3/?ak=" + ak + "&output=json&coordtype=wgs84ll&location="+ latitude+","+ longitude; /*url = "https://api.map.baidu.com/reverse_geocoding/v3/?ak=" + ak + "&output=json&coordtype=wgs84ll&extensions_poi=1&location=" + latitude + "," + longitude; StartCoroutine(Request());*/ GetDiDian(); } void GetDiDian() { url = "https://api.map.baidu.com/reverse_geocoding/v3/?ak=" + ak + "&output=json&coordtype=wgs84ll&extensions_poi=1&location=" + latitude + "," + longitude; StartCoroutine(Request()); } IEnumerator Request() { WWW www = new WWW(url); yield return www; if (string.IsNullOrEmpty(www.error)) { Debug.Log(www.text); //ResponseBody req = JsonConvert.DeserializeObject<ResponseBody>(www.text); //Debug.Log(req.content.address_detail.city + " X: " + req.content.point.x + " Y: " + req.content.point.x); string ss=www.text; //Object newStudent = JsonConvert.DeserializeObject<Object>(www.text); //Object newStudent = JsonMgr.DeSerialize<Object>(ss); /*JsonData newStudent = JsonConvert.DeserializeObject<JsonData>(www.text); Debug.Log(newStudent.status); Debug.Log(newStudent.result);*/ JObject jo = (JObject)JsonConvert.DeserializeObject(ss); Debug.Log(jo); Debug.Log(jo["result"]); Debug.Log(jo["result"]["pois"]); Debug.Log(jo["result"]["pois"][0]["addr"]); Debug.Log(jo["result"]["pois"][0]["name"]); Debug.Log(jo["result"]["pois"][0]["uid"]); } } } public class JsonMgr { public static string Serialize<T>(T t) { return JsonConvert.SerializeObject(t); } public static T DeSerialize<T>(string json) { return JsonConvert.DeserializeObject<T>(json); } } class JsonData { public int status; public JsonResult result; } class JsonResult { public JsonLocation location;//經(jīng)緯度 string formatted_address_poi;//機(jī)構(gòu)化地址 } class JsonLocation { public float lng;//經(jīng)度值 public float lat;//緯度值 }
注意:
逆地理編碼的時(shí)候需要用GetGPS.cs搭配使用
而且需要提前注冊(cè)號(hào)百度地圖開放平臺(tái)的服務(wù)端類型的應(yīng)用,并獲取AK碼
這里解析JSON時(shí),使用了Newtonsoft,分享鏈接如下:
鏈接:https://pan.baidu.com/s/1zoXDUr03miEKgnAk__U5YQ?pwd=cdbh
提取碼:cdbh
浙公網(wǎng)安備 33010602011771號(hào)