标签 LBS 下的文章

整个过程:
1、Unity获取GPS信息
2、GPS坐标转高德坐标
3、高德逆地理编码

步骤1见文章 https://www.xuefei.net.cn/index.php/archives/35/

步骤2代码

    /// <summary>
    /// GPS转高德坐标
    /// </summary>
    /// <returns></returns>
    IEnumerator GPS2GD(string gps)
    {
        lbs.text = "定位中……";
        string api = "https://restapi.amap.com/v3/assistant/coordinate/convert?locations=";
        string pas = "&coordsys=gps&output=json&key=";
        string key = "这里是key";
        Log.Debug(api + gps + pas + key);
        WWW www = new WWW(api + gps + pas + key);
        yield return www;
        Log.Debug(www.text);
        if (www.isDone && www.error == null)
        {
            Log.Debug(www.text);
            GDPOS pos = new GDPOS();
            JsonUtility.FromJsonOverwrite(www.text, pos);
            StartCoroutine(GetGDPos(pos.locations));
        }
        else
        {
            lbs.text = "定位失败";
            yield break;
        }
    }

[System.Serializable]
/// <summary>
/// GPS转高德坐标返回的信息
/// </summary>
public class GDPOS
{
    public int status;
    public string info;
    public int infocode;
    public string locations;
}

步骤3代码

    /// <summary>
    /// 获取反查位置
    /// </summary>
    /// <param name="gps"></param>
    /// <returns></returns>
    IEnumerator GetGDPos(string gdPos)
    { 
        string api = "https://restapi.amap.com/v3/geocode/regeo?output=json&location=";
        string key = "&key=这里是key&radius=500";
        Log.Debug(api + gdPos + key);
        WWW www = new WWW(api + gdPos + key);
        yield return www;
        Log.Debug(www.text);
        if (www.isDone && www.error == null)
        {
            Log.Debug(www.text);
            LBPOS pos = new LBPOS();
            JsonUtility.FromJsonOverwrite(www.text, pos);
            lbs.text = pos.regeocode.formatted_address;
        }
        else
        {
            lbs.text = "定位失败";
            yield break;
        }
    }

[System.Serializable]
public class LBPOS
{
    public int status;
    public Regeocode regeocode = new Regeocode();
    public string info;
    public int infocode;
}

[System.Serializable]
public class Regeocode
{
    public AddressComponent addressComponent = new AddressComponent();
    public string formatted_address;
}

[System.Serializable]
public class AddressComponent
{
    public string city;
    public string province;
    public string adcode;
    public string district;
    public string towncode;
    public StreetNumber streetNumber;
    public string country;
    public string township;
    public List<BusinessArea> businessAreas = new List<BusinessArea>();
    public Building building = new Building();
    public Neighborhood neighborhood = new Neighborhood();
    public string citycode;
}

[System.Serializable]
public class StreetNumber
{
    public string number;
    public string location;
    public string direction;
    public string distance;
    public string street;
}

[System.Serializable]
public class BusinessArea
{
    public string location;
    public string name;
    public string id;
}
[System.Serializable]
public class Building
{
    public List<string> name = new List<string>();
    public List<string> type = new List<string>();
}
[System.Serializable]
public class Neighborhood
{
    public List<string> name = new List<string>();
    public List<string> type = new List<string>();
}