此方法仅能获取当前IP所在城市的位置
废话不多说直接上代码
public class GpsTest : MonoBehaviour {
string url = "http://api.map.baidu.com/location/ip?ak=bretF4dm6W5gqjQAXuvP0NXW6FeesRXb&coor=bd09ll";
void Start()
{
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);
object a= JsonConvert.DeserializeObject(www.text);
Debug.Log(a.ToString());
Debug.Log(req.content.address_detail.city + " X: " + req.content.point.x + " Y: " + req.content.point.y);
}
}
public class ResponseBody
{
public string address;
public Content content;
public int status;
}
public class Content
{
public string address;
public Address_Detail address_detail;
public Point point;
}
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(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;
}
}
public class Point
{
public string x;
public string y;
public Point(string x, string y)
{
this.x = x;
this.y = y;
}
}
简洁明了,有问题的留言讨论交流!