Unity 工具类 之 简单快速 获取当前所在位置,所在城市,经纬度等
Unity工具类 之 简单快速 获取当前所在位置,所在城市,经纬度等
目录
一、方法提要:
通过string url = "http://api.map.baidu.com/location/ip?ak=bretF4dm6W5gqjQAXuvP0NXW6FeesRXb&coor=bd09ll";
快速得到当前所在位置的信息,由于返回的是 Json 数据,所以使用 litjson 进行解析数据
二、使用注意:
在使用Litjson解析的时候,由于用到自定义的数据结构,所以注意添加[SerializeField]
三、json 数据:
{"address":"CN|\u5317\u4eac|\u5317\u4eac|None|CHINANET|0|0","content":{"address":"\u5317\u4eac\u5e02","address_detail":{"city":"\u5317\u4eac\u5e02","city_code":131,"district":"","province":"\u5317\u4eac\u5e02","street":"","street_number":""},"point":{"x":"116.40387397","y":"39.91488908"}},"status":0}
四、使用截图:
五、Unity代码:
using LitJson;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using UnityEngine;
public class GetLocationData : MonoBehaviour {
public static string TheCurrentCity = "unknown";
public Action OnGetLocationDataOKEvent;
string url = "http://api.map.baidu.com/location/ip?ak=bretF4dm6W5gqjQAXuvP0NXW6FeesRXb&coor=bd09ll";
private void Awake()
{
StartCoroutine(Request());
}
void Start()
{
}
public void ToGetLocationData() {
StartCoroutine(Request());
}
IEnumerator Request()
{
// 判断是否有网络 更具需要处理是否连续判断是否有网
while (Application.internetReachability == NetworkReachability.NotReachable) {
Debug.Log(ArrowBLEDebugHelper.DEBUGMARK + "正在连接网络");
yield return new WaitForSeconds(1.0f);
}
WWW www = new WWW(url);
yield return www;
if (www.error != null)
{
Debug.Log(ArrowBLEDebugHelper.DEBUGMARK + " 网络错误!!请检查网络 ");
}
else
{
Debug.Log(ArrowBLEDebugHelper.DEBUGMARK + www.text);
if (www.text.ToLower().Contains("Error".ToLower()) == false)
{
ResponseBody req = JsonMapper.ToObject<ResponseBody>(www.text);
//Debug.Log(ArrowBLEDebugHelper.DEBUGMARK + StringUnicodeHelper.UnicodeToString(req.content.address_detail.city) +" X: "+ req.content.point.x +" Y: "+ req.content.point.x);
Debug.Log(ArrowBLEDebugHelper.DEBUGMARK + req.content.address_detail.city + " X: " + req.content.point.x + " Y: " + req.content.point.x);
TheCurrentCity = req.content.address_detail.city;
}
else
{
Debug.Log(ArrowBLEDebugHelper.DEBUGMARK + " 局域网,或者虚假连接网络");
}
}
// 获取到位置信息的事件
if (OnGetLocationDataOKEvent != null) {
OnGetLocationDataOKEvent.Invoke();
}
}
}
public class ResponseBody
{
public string address;
public Content content;
public int status;
}
[SerializeField]
public class Content
{
public string address;
public Address_Detail address_detail;
public Point point;
}
[SerializeField]
public class Address_Detail
{
public string city;
public int city_code;
public string district;
public string province;
public string street;
public string street_number;
public Address_Detail() { }
public Address_Detail(string city, int city_code, string district, string province, string street, string street_number)
{
this.city = city;
this.city_code = city_code;
this.district = district;
this.province = province;
this.street = street;
this.street_number = street_number;
}
}
[SerializeField]
public class Point
{
public string x;
public string y;
public Point() { }
public Point(string x, string y)
{
this.x = x;
this.y = y;
}
}
public class StringUnicodeHelper{
/// <summary>
/// 字符串转Unicode码
/// </summary>
/// <returns>The to unicode.</returns>
/// <param name="value">Value.</param>
public static string StringToUnicode(string value)
{
byte[] bytes = Encoding.Unicode.GetBytes(value);
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < bytes.Length; i += 2)
{
// 取两个字符,每个字符都是右对齐。
stringBuilder.AppendFormat("u{0}{1}", bytes[i + 1].ToString("x").PadLeft(2, '0'), bytes[i].ToString("x").PadLeft(2, '0'));
}
return stringBuilder.ToString();
}
/// <summary>
/// Unicode转字符串
/// </summary>
/// <returns>The to string.</returns>
/// <param name="unicode">Unicode.</param>
public static string UnicodeToString(string unicode)
{
string resultStr = "";
string[] strList = unicode.Split('u');
for (int i = 1; i < strList.Length; i++)
{
resultStr += (char)int.Parse(strList[i], System.Globalization.NumberStyles.HexNumber);
}
return resultStr;
}
}