Files
3d-qiaozha/Assets/Scripts/DisplayLongitudeAndLatitude.cs
2026-03-10 10:07:11 +08:00

77 lines
3.5 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
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 string outputPosition;
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()
{
cam = Camera.main;
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和zx为东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");
//string outputData = currentCamLongitude.ToString();
Debug.Log(outputData);
outputPosition = outputData;
}
}