标签 定位 下的文章

整个过程:
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>();
}

using System;
using System.Collections;
using UnityEngine;
using UnityEngine.UI;

public class TestLocation : MonoBehaviour
{
    public InputField inputField;

    // Use this for initialization
    void Start()
    {
        StartCoroutine(Location());
        //Debug.Log(WGS2BD(31, 118));
    }

    // Update is called once per frame
    void Update()
    {

    }

    /// <summary>
    /// 定位
    /// </summary>
    /// <returns></returns>
    IEnumerator Location()
    {
        // First, check if user has location service enabled
        if (!Input.location.isEnabledByUser)
        {
            inputField.text = "未获取GPS定位权限,定位失败";
            yield break;
        }

        // Start service before querying location
        Input.location.Start();

        // Wait until service initializes
        int maxWait = 20;
        while (Input.location.status == LocationServiceStatus.Initializing && maxWait > 0)
        {
            yield return new WaitForSeconds(1);
            maxWait--;
        }

        // Service didn't initialize in 20 seconds
        if (maxWait < 1)
        {
            print("Timed out");
            inputField.text = "定位超时";
            yield break;
        }

        // Connection has failed
        if (Input.location.status == LocationServiceStatus.Failed)
        {
            print("Unable to determine device location");
            inputField.text = "定位失败";
            yield break;
        }
        else
        {
            // Access granted and location value could be retrieved
            string location = "latitude:" + Input.location.lastData.latitude
                + " longitude:" + Input.location.lastData.longitude
                + " altitude:" + Input.location.lastData.altitude
                + " horizontalAccuracy:" + Input.location.lastData.horizontalAccuracy
                + " timestamp:" + Input.location.lastData.timestamp;
            inputField.text = location;
            print("Location: " + location);
        }

        // Stop service if there is no need to query location updates continuously
        Input.location.Stop();
    }

    //系数常量
    readonly double a = 6378245.0;
    readonly double ee = 0.00669342162296594323;
    readonly double x_pi = 3.14159265358979324 * 3000.0 / 180.0;
    
    //转换经度
    double transformLat(double lat, double lon)
    {
        double ret = -100.0 + 2.0 * lat + 3.0 * lon + 0.2 * lon * lon + 0.1 * lat * lon + 0.2 * Math.Sqrt(Math.Abs(lat));
        ret += (20.0 * Math.Sin(6.0 * lat * Math.PI) + 20.0 * Math.Sin(2.0 * lat * Math.PI)) * 2.0 / 3.0;
        ret += (20.0 * Math.Sin(lon * Math.PI) + 40.0 * Math.Sin(lon / 3.0 * Math.PI)) * 2.0 / 3.0;
        ret += (160.0 * Math.Sin(lon / 12.0 * Math.PI) + 320 * Math.Sin(lon * Math.PI / 30.0)) * 2.0 / 3.0;
        return ret;
    }


    //转换纬度
    double transformLon(double lat, double lon)
    {
        double ret = 300.0 + lat + 2.0 * lon + 0.1 * lat * lat + 0.1 * lat * lon + 0.1 * Math.Sqrt(Math.Abs(lat));
        ret += (20.0 * Math.Sin(6.0 * lat * Math.PI) + 20.0 * Math.Sin(2.0 * lat * Math.PI)) * 2.0 / 3.0;
        ret += (20.0 * Math.Sin(lat * Math.PI) + 40.0 * Math.Sin(lat / 3.0 * Math.PI)) * 2.0 / 3.0;
        ret += (150.0 * Math.Sin(lat / 12.0 * Math.PI) + 300.0 * Math.Sin(lat / 30.0 * Math.PI)) * 2.0 / 3.0;
        return ret;
    }
     
    /// <summary>
    /// WGS transform to GCJ
    /// </summary>
    /// <param name="lat">维度</param>
    /// <param name="lon">经度</param>
    /// <returns></returns>
    POS WGS2GCJ(double lat, double lon)
    {
        double dLat = transformLat(lon - 105.0, lat - 35.0);
        double dLon = transformLon(lon - 105.0, lat - 35.0);
        double radLat = lat / 180.0 * Math.PI;
        double magic = Math.Sin(radLat);
        magic = 1 - ee * magic * magic;
        double sqrtMagic = Math.Sqrt(magic);
        dLat = (dLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * Math.PI);
        dLon = (dLon * 180.0) / (a / sqrtMagic * Math.Cos(radLat) * Math.PI);
        double mgLat = lat + dLat;
        double mgLon = lon + dLon;
        POS loc = new POS(mgLat, mgLon);
        return loc;
    }

    /// <summary>
    /// GCJ transform to BD2
    /// </summary>
    /// <param name="lat"></param>
    /// <param name="lon"></param>
    /// <returns></returns>
    POS GCJ2BD2(double lat, double lon)
    {
        double x = lon;
        double y = lat;
        double z = Math.Sqrt(x * x + y * y) + 0.00002 * Math.Sin(y * x_pi);
        double theta = Math.Atan2(y, x) + 0.000003 * Math.Cos(x * x_pi);
        double bd_lon = z * Math.Cos(theta) + 0.0065;
        double bd_lat = z * Math.Sin(theta) + 0.006;
        POS bdpoint = new POS(bd_lat, bd_lon);
        return bdpoint;
    }

    // wgs transform to bd
    POS WGS2BD(double lat, double lon)
    {
        POS wgs_to_gcj = WGS2GCJ(lat, lon);
        POS gcj_to_bd = GCJ2BD2(wgs_to_gcj.Lat, wgs_to_gcj.Lon);
        return gcj_to_bd;
    }

    /// <summary>
    /// 位置
    /// </summary>
    struct POS
    {
        /// <summary>
        /// 维度
        /// </summary>
        public double Lat;
        /// <summary>
        /// 经度
        /// </summary>
        public double Lon;

        public POS(double La, double Lo)
        {
            Lat = La;
            Lon = Lo;
        }

        public override string ToString()
        {
            return "维度:" + Lat + " 经度:" + Lon;
        }
    }
}