using System; using System.Collections; using System.Collections.Generic; using TMPro; using UnityEngine; using static DebugTools; public class DisplayLongitudeAndLatitude : MonoBehaviour { // Start is called before the first frame update private Vector3 east = new Vector3(1, 0, 0);//x东 private Vector3 south = new Vector3(0, 0, -1);//-z南 private Vector3 west = new Vector3(-1, 0, 0);//-x西 private Vector3 north = new Vector3(0, 0, 1);//z北 private Camera cam; Vector3 towerCenterPosition = new Vector3(-177.477f, 62.62579f, -73.2408f);//xyz Vector3 towerCenterWorldPosition = new Vector3(116.6541536233f, 23.649973126f, 62.62579f);//经纬高程东经和北纬如何和unity里面的米按照固定的比例转换,高程和unity里是一致的 //一个unity里面的东西,一个现实世界的坐标,2个控制点得到unity里面1米,即现实世界的1米在不考虑地球曲率的时候对应大概多少经纬度 Vector3 beixiqiaoStoneTabletPosition = new Vector3(-19.4974f, 17.278f, 7.5837f); Vector3 beixiqiaoStoneTabletWorldPosition = new Vector3(116.655079685f, 23.650741f, 17.278f); private Vector3 ControlPointsDeltaInUnity; private Vector3 ControlPointsDeltaInRealWorld; private float horizontalUnit; private float verticalUnit; public TextMeshProUGUI text; public DebugTools debugTools; //判断当前帧是否要传感器的数据 public static string ConvertToDegreesMinutesSeconds(float decimalDegrees, int decimalPlaces = 2) { // 处理负数 bool isNegative = decimalDegrees < 0; decimalDegrees = Mathf.Abs(decimalDegrees); // 提取度 int degrees = Mathf.FloorToInt(decimalDegrees); // 提取分 float remaining = (decimalDegrees - degrees) * 60; int minutes = Mathf.FloorToInt(remaining); // 提取秒 float seconds = (remaining - minutes) * 60; // 格式化输出,添加正负号 string sign = isNegative ? "-" : ""; return $"{sign}{degrees}°{minutes}'{seconds.ToString("F" + decimalPlaces)}\""; } void Start() { debugTools = GameObject.Find("WEB").GetComponent(); cam = Camera.main; text = GetComponent(); ControlPointsDeltaInUnity = beixiqiaoStoneTabletPosition - towerCenterPosition;//控制点在unity的差距 ControlPointsDeltaInRealWorld = beixiqiaoStoneTabletWorldPosition - towerCenterWorldPosition;//控制点在现实世界的差距 //设计目标转换为多少米每度,先算经度方向,再算纬度方向,计算出来单位之后还是用塔顶点来做距离计算 horizontalUnit = Math.Abs(ControlPointsDeltaInUnity.x / ControlPointsDeltaInRealWorld.x); verticalUnit = Math.Abs(ControlPointsDeltaInUnity.z / ControlPointsDeltaInRealWorld.y); } // Update is called once per frame void Update() { //float deltaHeight = cam.transform.position.y - towerCenterPosition.y; float currentCamElevation = cam.transform.position.y; Vector3 deltaBetweenCamAndtowerCenter = cam.transform.position - towerCenterPosition;//这个是unity里面的delta,转化为经纬度,但是高程是没用的,只要看x和z,x为东,z为北 float horizontalDelta = deltaBetweenCamAndtowerCenter.x / horizontalUnit; float verticalDelta = deltaBetweenCamAndtowerCenter.z / verticalUnit; float currentCamLongitude = towerCenterWorldPosition.x + horizontalDelta; float currentCamLatitude = towerCenterWorldPosition.y + verticalDelta; string Longitude = ConvertToDegreesMinutesSeconds(currentCamLongitude); string Latitude = ConvertToDegreesMinutesSeconds(currentCamLatitude); string outputData = "经度:" + Longitude + " " + "纬度:" + Latitude + " " + "高程:" + currentCamElevation.ToString("F2"); if(!debugTools._isRunning) { SendData sendData = new SendData(); sendData.LongitudeAndLatitude = outputData; string CoutJsonData = JsonUtility.ToJson(sendData, true); Application.ExternalCall("sendDataToVue", CoutJsonData); } //string outputData = "经度:" + Longitude + " " + "纬度:" + Latitude ; //string outputData = currentCamLongitude.ToString(); //Debug.Log(outputData); text.text = outputData; } public class SendData { public string LongitudeAndLatitude; } }