• 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

  • 相关阅读:
    gdb简易教程
    使用WoeUSB在Linux上制作Win10启动盘
    How to fix git: new blank line at EOF
    使用CSS3动画属性实现各种旋转跳跃
    jQuery事件
    挖矿程序minerd入侵分析和解决办法
    css3的伪(伪类和伪元素)大合集
    HTML 5 <blockquote><p>的分工与合作
    HTML<figure> <figcaption> 标签定义图文并茂
    监听 返回按钮事件
  • 原文地址:https://www.cnblogs.com/dj1232090/p/5628572.html
Copyright © 2020-2023  润新知