标签 百度地图 下的文章

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