xuefei 发布的文章

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

task makeJar(type: Copy) {
    //删除存在的
    delete 'build/libs/plugin.jar'
    //设置拷贝的文件
    from('build/intermediates/packaged-classes/release/')
    //打进jar包后的文件目录
    into('build/libs/')
    //将classes.jar放入build/libs/目录下
    //include ,exclude参数来设置过滤
    //(我们只关心classes.jar这个文件)
    include('classes.jar')
    //重命名
    rename('classes.jar', 'plugin.jar')
}
makeJar.dependsOn(build)

Termina 输入命令 gradlew makeJar 打包jar

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;
        }
    }
}

using UnityEngine;
using UnityEditor;
using System.IO;
using System.Text;

/// <summary>
/// CS文件转化为UTF-8格式的工具
/// </summary> 
public class SetCSToUTF8 : EditorWindow
{
    /// <summary>
    /// 文件路径
    /// </summary>
    private string csPath = "";

    /// <summary>
    /// 文件后缀
    /// </summary>
    private string suffix = ".cs";

    [MenuItem("工具/代码转UTF-8格式")]
    static void Init()
    {
        SetCSToUTF8 window = (SetCSToUTF8)EditorWindow.GetWindow(typeof(SetCSToUTF8), false, "代码格式转换", true);
    }

    void OnGUI()
    {
        EditorGUILayout.Space();
        GUILayout.Label("鼠标点击代码文件夹");
        EditorGUILayout.Space();
        GUILayout.Label("文件夹路径: ");
        csPath = GUILayout.TextField(csPath);
        Object[] objs = Selection.GetFiltered(typeof(Object), SelectionMode.TopLevel);
        csPath = Application.dataPath + "/../" + AssetDatabase.GetAssetPath(objs[0]) + "/";
        GUILayout.Label("文件后缀: ");
        suffix = GUILayout.TextField(suffix);

        if (GUILayout.Button("CS文件转UTF-8格式"))
        {
            Conversion();
        }
    }

    private void Conversion()
    {
        if (csPath.Equals(string.Empty))
        {
            return;
        }

        if (!Directory.Exists(csPath))
        {
            Debug.LogError("找不到文件夹路径!");
            return;
        }

        string[] files = Directory.GetFiles(csPath, "*", SearchOption.AllDirectories);
        foreach (string file in files)
        {
            if (!file.EndsWith(suffix)) continue;
            string strTempPath = file.Replace(@"\", "/");
            Debug.Log("文件路径:" + strTempPath);
            ConvertFileEncoding(strTempPath, new UTF8Encoding(true, true));
        }
        AssetDatabase.Refresh();
        Debug.Log("格式转换完成!");
    }

    /// <summary>
    /// 文件编码转换
    /// </summary>
    /// <param name="sourceFile">源文件</param> 
    /// <param name="targetEncoding">目标编码</param>
    private static void ConvertFileEncoding(string sourceFile, Encoding targetEncoding)
    {
        string fileString = File.ReadAllText(sourceFile, Encoding.Default);
        File.WriteAllText(sourceFile, fileString, targetEncoding);
    }
} 

TIM截图20181216170657.jpg