134 lines
5.2 KiB
C#
134 lines
5.2 KiB
C#
using System.Collections;
|
||
using System.Collections.Generic;
|
||
using UnityEngine;
|
||
|
||
// public class GisPointTo3DPoint : MonoBehaviour
|
||
// {
|
||
|
||
// public static GisPointTo3DPoint Instance = null;
|
||
// public Transform BottomRightPoint; //Unity中右下点 (X正方向和Z轴的负方向之间)
|
||
// public Transform TopLeftPoint;//Unity中左上点 (Z轴正方向和X轴负方向之间)
|
||
|
||
// private Vector2 BottomRightSai;//地图中对应的左上经纬度点
|
||
// private Vector2 TopLeftSai;//地图中对应的右下经纬度点
|
||
|
||
// private float z_offset, x_offset, z_w_offset, x_w_offset;
|
||
|
||
// private RaycastHit rayHit;
|
||
|
||
// private void Awake()
|
||
// {
|
||
// if (Instance == null) Instance = this;
|
||
// else if (Instance != this) Destroy(this);
|
||
// InitBasicNum();//初始化参数
|
||
// }
|
||
|
||
// private void InitBasicNum()
|
||
// {
|
||
// //通过传过来的第一个点,确定坐标范围。以导弹为中心
|
||
// //右下
|
||
// BottomRightSai = new Vector2(117.013f, 36.6599f);
|
||
// //左上
|
||
// TopLeftSai = new Vector2(117.0197f, 36.66141f);
|
||
// z_offset = 36.6599f - 36.66141f;
|
||
// x_offset = 117.013f - 117.0197f;
|
||
// //z_offset = BottomRightSai.y - TopLeftSai.y;//地图中的维度差
|
||
// // x_offset = BottomRightSai.x - TopLeftSai.x;//地图中的经度差
|
||
// z_w_offset = BottomRightPoint.position.z - TopLeftPoint.position.z;//unity中的维度差
|
||
// x_w_offset = BottomRightPoint.position.x - TopLeftPoint.position.x;//unity中的经度差
|
||
// }
|
||
// /// <summary>
|
||
// /// 由经纬度得到位置点
|
||
// /// </summary>
|
||
// /// <param name="se">x经度</param>
|
||
// /// y 纬度
|
||
// /// <returns></returns>
|
||
// public Vector3 GetWorldPoint(double x, double y)
|
||
// {
|
||
// double tempX = x - 117.0197f;
|
||
// double tempZ = y - 36.6599f;
|
||
// Debug.Log(tempX);
|
||
// double _tempX = (tempX * x_w_offset / x_offset + TopLeftPoint.position.x);
|
||
// double _tempZ = (tempZ * z_w_offset / z_offset + BottomRightPoint.position.z);
|
||
// return new Vector3((float)_tempX, 0, (float)_tempZ);
|
||
// }
|
||
// /// <summary>
|
||
// /// 由位置点得到经纬度
|
||
// /// </summary>
|
||
// /// <param name="curPoint"></param>
|
||
// /// <returns></returns>
|
||
// public Vector3 GetLatLon(Vector3 curPoint)
|
||
// {
|
||
// //坐标偏差
|
||
// float _x_offset = (curPoint.x - BottomRightPoint.position.x) * x_offset / x_w_offset;
|
||
// float _z_offset = (curPoint.z - TopLeftPoint.position.z) * z_offset / z_w_offset;
|
||
// float resultX = _x_offset + BottomRightSai.x;
|
||
// float resultZ = _z_offset + TopLeftSai.y;
|
||
// return new Vector2(resultX, resultZ);
|
||
// }
|
||
// }
|
||
|
||
public class GisPointTo3DPoint : MonoBehaviour
|
||
{
|
||
public static GisPointTo3DPoint Instance = null;
|
||
public Transform BottomRightPoint; // Unity右下点(X正方向,Z负方向)
|
||
public Transform TopLeftPoint; // Unity左上点(X负方向,Z正方向)
|
||
|
||
private Vector2 BottomRightSai; // 地图右下点(大经度,小纬度)
|
||
private Vector2 TopLeftSai; // 地图左上点(小经度,大纬度)
|
||
|
||
private float z_offset, x_offset, z_w_offset, x_w_offset;
|
||
|
||
private void Awake()
|
||
{
|
||
if (Instance == null) Instance = this;
|
||
else if (Instance != this) Destroy(this);
|
||
InitBasicNum(); // 初始化参数
|
||
}
|
||
|
||
private void InitBasicNum()
|
||
{
|
||
// 正确设置经纬度点
|
||
BottomRightSai = new Vector2(117.0197f, 36.6599f); // 右下:大经度,小纬度
|
||
TopLeftSai = new Vector2(117.013f, 36.66141f); // 左上:小经度,大纬度
|
||
|
||
// 计算经纬度差
|
||
x_offset = BottomRightSai.x - TopLeftSai.x; // 经度差(正数)
|
||
z_offset = BottomRightSai.y - TopLeftSai.y; // 纬度差(负数)
|
||
|
||
// 获取Unity中的坐标差
|
||
x_w_offset = BottomRightPoint.position.x - TopLeftPoint.position.x;
|
||
z_w_offset = BottomRightPoint.position.z - TopLeftPoint.position.z;
|
||
|
||
// 调试输出,确保数值正确
|
||
Debug.Log($"经度差: {x_offset}, 纬度差: {z_offset}");
|
||
Debug.Log($"Unity X差: {x_w_offset}, Z差: {z_w_offset}");
|
||
}
|
||
|
||
public Vector3 GetWorldPoint(double longitude, double latitude)
|
||
{
|
||
// 计算经度差(输入经度 - 左上经度)
|
||
double tempX = longitude - TopLeftSai.x;
|
||
// 计算纬度差(输入纬度 - 右下纬度)
|
||
double tempZ = latitude - BottomRightSai.y;
|
||
|
||
// 转换为Unity坐标
|
||
double unityX = tempX * (x_w_offset / x_offset) + TopLeftPoint.position.x;
|
||
double unityZ = tempZ * (z_w_offset / z_offset) + BottomRightPoint.position.z;
|
||
|
||
return new Vector3((float)unityX, 0, (float)unityZ);
|
||
}
|
||
|
||
public Vector2 GetLatLon(Vector3 position)
|
||
{
|
||
// 计算经度
|
||
float deltaX = (position.x - TopLeftPoint.position.x) / x_w_offset;
|
||
float lon = TopLeftSai.x + deltaX * x_offset;
|
||
|
||
// 计算纬度
|
||
float deltaZ = (position.z - BottomRightPoint.position.z) / z_w_offset;
|
||
float lat = BottomRightSai.y + deltaZ * z_offset;
|
||
|
||
return new Vector2(lon, lat);
|
||
}
|
||
} |