Unity3d Gis 坐标转换

Posted Lerry.Zhao

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Unity3d Gis 坐标转换相关的知识,希望对你有一定的参考价值。

最近在做unity3d与Gis结合的项目,最基本的就是坐标的转换问题,比如把经纬度为(166.23.9.27 , 39.55.15.74) 转换到unity里面成相应的位置点,废话不多说 上代码:

using UnityEngine;
using System.Collections;

public class SaiGetLatLog : MonoBehaviour {

    public Transform firstPoint; //Unity中左上点
    public Transform secondPoint;//Unity中右下点

    
    public SaiEarth firstSai;//地图中对应的左上经纬度点
    public SaiEarth secondSai;//地图中对应的右下经纬度点

    private float z_offset,x_offset,z_w_offset,x_w_offset;

    private RaycastHit rayHit;

    // Use this for initialization
    void Start () {
        InitBasicNum ();//初始化参数
    }
    
    // Update is called once per frame
    void Update () {
        if (Input.GetMouseButton (0) && Physics.Raycast (Camera.main.ScreenPointToRay (Input.mousePosition), out rayHit, Mathf.Infinity)) {

            print ( getWorldPoint(firstSai).x+","+ getWorldPoint(firstSai).z);

        }
    }

    void InitBasicNum()
    {

        firstSai = new SaiEarth (new Vector3 (116f, 23f, 9.27f), new Vector3 (39f, 55f, 15.74f));
        secondSai = new SaiEarth (new Vector3(116f,23f,44.39f),new Vector3(39f,54f,44.27f));

        z_offset = Mathf.Abs ((firstSai.latitude.x+firstSai.latitude.y/60+firstSai.latitude.z/3600) - (secondSai.latitude.x+secondSai.latitude.y/60+secondSai.latitude.z/3600));//地图中的维度差
        x_offset = Mathf.Abs ((firstSai.longitude.x+firstSai.longitude.y/60+firstSai.longitude.z/3600)-(secondSai.longitude.x+secondSai.longitude.y/60+secondSai.longitude.z/3600));//地图中的经度差

         z_w_offset = Mathf.Abs (firstPoint.localPosition.z - secondPoint.localPosition.z);//unity中的维度差
         x_w_offset = Mathf.Abs (firstPoint.localPosition.x - secondPoint.localPosition.x);//unity中的经度差


    }


    Vector3 getWorldPoint(SaiEarth se)//由经纬度得到位置点
    {

        float tempX = (float)(se.longitude.x + se.longitude.y / 60 + se.longitude.z / 3600 - (secondSai.longitude.x + secondSai.longitude.y / 60 + secondSai.longitude.z / 3600));
        float tempZ = (float)(se.latitude.x + se.latitude.y / 60 + se.latitude.z / 3600 - (secondSai.latitude.x + secondSai.latitude.y / 60 + secondSai.latitude.z / 3600));

        float _tempX = (float)(tempX * x_w_offset / x_offset + secondPoint.localPosition.x);
        float _tempZ = (float)(tempZ * z_w_offset / z_offset + secondPoint.localPosition.z);

        return new Vector3((float)_tempX,0f,(float)_tempZ);


    }

    SaiEarth getLatLon(Vector3 curPoint)//由位置点得到经纬度
    {
        SaiEarth tempEarth = new SaiEarth();

        float _z_offset = Mathf.Abs (curPoint.z - secondPoint.localPosition.z) * z_offset / z_w_offset;
        float _x_offset = Mathf.Abs (curPoint.x - secondPoint.localPosition.x) * x_offset / x_w_offset;

        float resultX = _x_offset + (secondSai.longitude.x + secondSai.longitude.y/60 + secondSai.longitude.z/3600);
        float resultZ = _z_offset + (secondSai.latitude.x + secondSai.latitude.y/60 + secondSai.latitude.z/3600);

        tempEarth.longitude = new Vector3 ((int)resultX, (int)((resultX - (int)resultX)*60),((resultX - (int)resultX)*60 - (int)((resultX - (int)resultX)*60))*60);
        tempEarth.latitude = new Vector3 ((int)resultZ, (int)((resultZ - (int)resultZ)*60),((resultZ - (int)resultZ)*60 - (int)((resultZ - (int)resultZ)*60))*60);

        return tempEarth;

    }


}

上面有一个SaiEarth的类,其实写的很简单:

using UnityEngine;
using System.Collections;

public class SaiEarth  {


    public Vector3 longitude;
    public Vector3 latitude;

    public SaiEarth()
    {

    }

    public SaiEarth(Vector3 longitude,Vector3 latitude)
    {
        this.latitude = latitude;
        this.longitude = longitude;
    }

    public string toString()
    {
        return "Lat:"+this.latitude + ",Long:" + this.longitude;
    }
}

来源:http://blog.csdn.net/saide6000/article/details/26992945

 

以上是关于Unity3d Gis 坐标转换的主要内容,如果未能解决你的问题,请参考以下文章

北京54坐标如何转换WGS84坐标?

GIS中怎么将投影坐标转换成地理坐标

Unity3D之笛卡尔坐标系转换——屏幕坐标转换世界坐标,世界坐标转换相机坐标工具

Unity3D之笛卡尔坐标系转换——屏幕坐标转换世界坐标,世界坐标转换相机坐标工具

关于GIS坐标系转换的问题!!急!

gis空间分析案例_7参数单坐标转换