1.通过GPS获取经纬度

直接上代码了

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 用于访问设备的位置属性(手持设备), 静态的LocationService位置  
        // LocationService.isEnabledByUser 用户设置里的定位服务是否启用  
        if (!Input.location.isEnabledByUser)
        {
            GetGps = "isEnabledByUser value is:" + Input.location.isEnabledByUser.ToString() + " Please turn on the GPS";
            yield return false;
        }

        // LocationService.Start() 启动位置服务的更新,最后一个位置坐标会被使用  
        Input.location.Start(10.0f, 10.0f);

        int maxWait = 20;
        while (Input.location.status == LocationServiceStatus.Initializing && maxWait > 0)
        {
            // 暂停协同程序的执行(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>
    /// 停止刷新位置(节省手机电量)
    /// </summary>
    void StopGPS()
    {
        Input.location.Stop();
    }
}

 

2.通过百度的逆地理编码API进行经纬度跟中文地址的转化

进入百度地图开放平台

https://lbsyun.baidu.com/apiconsole/quota#/home

 创建服务端类型的应用

 

这是逆地理编码的接口说明:

https://lbsyun.baidu.com/faq/api?title=webapi/guide/webservice-geocoding-abroad-base

这是API调用的问题文档: 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
    //这里使用的是百度开放平台 应用类别:服务端 的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;//经纬度
    string formatted_address_poi;//机构化地址
}
class JsonLocation
{
    public float lng;//经度值
    public float lat;//纬度值
}

注意:

逆地理编码的时候需要用GetGPS.cs搭配使用

而且需要提前注册号百度地图开放平台的服务端类型的应用,并获取AK码

这里解析JSON时,使用了Newtonsoft,分享链接如下:

链接:https://pan.baidu.com/s/1zoXDUr03miEKgnAk__U5YQ?pwd=cdbh
提取码:cdbh

 

posted on 2024-07-11 11:04  凌落成迷  阅读(33)  评论(0编辑  收藏  举报