| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182 | using System;using System.Collections;using System.Collections.Generic;using UnityEngine;public static class CoordinateConverter{    // 计算两个经纬度之间的距离,返回值为公里    public static double Haversine(double lat1, double lon1, double lat2, double lon2)    {        // 将纬度和经度从度转换为弧度        double R = 6371.0; // 地球半径,单位为公里        double dLat = ToRadians(lat2 - lat1);        double dLon = ToRadians(lon2 - lon1);        lat1 = ToRadians(lat1);        lat2 = ToRadians(lat2);        // Haversine 公式        double a = Mathf.Sin((float)dLat / 2) * Mathf.Sin((float)dLat / 2) +                   Mathf.Cos((float)lat1) * Mathf.Cos((float)lat2) *                   Mathf.Sin((float)dLon / 2) * Mathf.Sin((float)dLon / 2);        double c = 2 * Mathf.Atan2(Mathf.Sqrt((float)a), Mathf.Sqrt((float)(1 - a)));        // 计算距离        double distance = R * c;        return distance;    }    // 将角度转换为弧度    private static double ToRadians(double angle)    {        return angle * Mathf.Deg2Rad;    }    // 将经纬度转换为Unity坐标    public static Vector3 GeoToUnity(double longitude, double latitude)    {        float x = (float)(96151.617 * longitude - 10938527.4823);        float z = (float)(111468.3949 * latitude - 3339986.2697);         return new Vector3(x, 0, z);    }    // 将Unity坐标转换为经纬度    public static (double Longitude, double Latitude) UnityToGeo(Vector3 unityCoord)    {        double longitude = (unityCoord.x + 10938527.4823)/ 96151.617;        double latitude = (unityCoord.z + 3339986.2697)/ 111468.3949;        // 这里我们忽略了截距,因为我们假设Unity坐标的原点对应于经纬度的截距        // 如果需要考虑截距,可以在这里添加相应的计算        return (longitude, latitude);    }    public static Vector3 GeoToUGUI(double longitude, double latitude)    {        float x = (float)(355.53f * longitude - 40339.8622f);        float y = (float)(409.37f * latitude - 12420.05f);        return new Vector3(x, y, 0);    }    public static Vector3 GeoToUGUISmall(double longitude,double latitude)    {        float x = (float)(80.124f * longitude - 9118.032f);        float y = (float)(92.467f * latitude - 2775.33f);        return new Vector3(x, y, -0.400f);    }        public static (double longitude, double latitude) UGUISmallToGeo(Vector3 uiCoord)    {        double longitude = (uiCoord.x + 8607.658) / 75.642;        double latitude = (uiCoord.y + 2640.548) / 87.990;        // 这里我们忽略了截距,因为我们假设Unity坐标的原点对应于经纬度的截距        // 如果需要考虑截距,可以在这里添加相应的计算        return (longitude, latitude);    }    public static (double latitude, double longitude) UGUIToGeo(Vector3 ugui)    {        float x = ugui.x;        float y = ugui.y;        double latitude = (x + 40339.8622f) / 355.53f;        double longitude = (y + 12420.05f) / 409.37f;        return (latitude, longitude);    }    const double PI = Math.PI;    const double AXIS = 6378245.0;   // a    const double EE = 0.00669342162296594323; // e^2    const double EARTH_RADIUS = 20037508.34;  // 墨卡托投影最大值    /// <summary>    /// 判断是否在中国范围外    /// </summary>    static bool OutOfChina(double lon, double lat)    {        return (lon < 72.004 || lon > 137.8347 || lat < 0.8293 || lat > 55.8271);    }    static double TransformLat(double x, double y)    {        double ret = -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y                   + 0.1 * x * y + 0.2 * Math.Sqrt(Math.Abs(x));        ret += (20.0 * Math.Sin(6.0 * x * PI) + 20.0 * Math.Sin(2.0 * x * PI)) * 2.0 / 3.0;        ret += (20.0 * Math.Sin(y * PI) + 40.0 * Math.Sin(y / 3.0 * PI)) * 2.0 / 3.0;        ret += (160.0 * Math.Sin(y / 12.0 * PI) + 320 * Math.Sin(y * PI / 30.0)) * 2.0 / 3.0;        return ret;    }    static double TransformLon(double x, double y)    {        double ret = 300.0 + x + 2.0 * y + 0.1 * x * x                   + 0.1 * x * y + 0.1 * Math.Sqrt(Math.Abs(x));        ret += (20.0 * Math.Sin(6.0 * x * PI) + 20.0 * Math.Sin(2.0 * x * PI)) * 2.0 / 3.0;        ret += (20.0 * Math.Sin(x * PI) + 40.0 * Math.Sin(x / 3.0 * PI)) * 2.0 / 3.0;        ret += (150.0 * Math.Sin(x / 12.0 * PI) + 300.0 * Math.Sin(x / 30.0 * PI)) * 2.0 / 3.0;        return ret;    }    /// <summary>    /// GCJ-02(火星坐标,高德坐标) → WGS84    /// </summary>    public static Vector2 GCJ02ToWGS84(double lon, double lat)    {        if (OutOfChina(lon, lat))        {            return new Vector2((float)lon, (float)lat);        }        double dLat = TransformLat(lon - 105.0, lat - 35.0);        double dLon = TransformLon(lon - 105.0, lat - 35.0);        double radLat = lat / 180.0 * PI;        double magic = Math.Sin(radLat);        magic = 1 - EE * magic * magic;        double sqrtMagic = Math.Sqrt(magic);        dLat = (dLat * 180.0) / ((AXIS * (1 - EE)) / (magic * sqrtMagic) * PI);        dLon = (dLon * 180.0) / (AXIS / sqrtMagic * Math.Cos(radLat) * PI);        double mgLat = lat + dLat;        double mgLon = lon + dLon;        return new Vector2((float)(lon * 2 - mgLon), (float)(lat * 2 - mgLat));    }    /// <summary>    /// WGS84 → WebMercator (EPSG:3857)    /// </summary>    public static Vector2 WGS84ToWebMercator(double lon, double lat)    {        double x = lon * EARTH_RADIUS / 180.0;        double y = Math.Log(Math.Tan((90.0 + lat) * PI / 360.0)) / (PI / 180.0);        y = y * EARTH_RADIUS / 180.0;        return new Vector2((float)x, (float)y);    }    /// <summary>    /// 一步到位: GCJ-02 → WebMercator    /// </summary>    public static Vector2 GCJ02ToWebMercator(double lon, double lat)    {        Vector2 wgs84 = GCJ02ToWGS84(lon, lat);        return WGS84ToWebMercator(wgs84.x, wgs84.y);    }}
 |