作者:游你精彩_980_469 | 来源:互联网 | 2023-07-31 15:29
GIS经纬度坐标代表现实生活中地球的经纬度坐标,具体代码如下:usingSystem.Collections;usingSystem.Collections.Generic;us
GIS经纬度坐标代表现实生活中地球的经纬度坐标,具体代码如下:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GisPointTo3DPoint : MonoBehaviour {
public static GisPointTo3DPoint Instance = null;
public Transform firstPoint; //Unity中右下点
public Transform secondPoint;//Unity中左上点
private Vector2 firstSai;//地图中对应的左上经纬度点
private Vector2 secondSai;//地图中对应的右下经纬度点
private float z_offset, x_offset, z_w_offset, x_w_offset;
private RaycastHit rayHit;
// Use this for initialization
private void Start()
{
if (Instance == null) Instance = this;
else if (Instance != this) Destroy(this);
InitBasicNum();//初始化参数
}
// Update is called once per frame
private void Update()
{
//if (Input.GetMouseButton(0) && Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out rayHit, Mathf.Infinity))
//{
// print(getWorldPoint(new Vector2(113.94495f, 22.52078f)).x + "," + getWorldPoint(new Vector2(113.94495f, 22.52078f)).z);
// //print(getLatLon(rayHit.point).x + " " + getLatLon(rayHit.point).y);
//}
}
private void InitBasicNum()
{
firstSai = new Vector2(113.94187f, 22.52283f);
secOndSai= new Vector2(113.94797f, 22.51844f);
z_offset = Mathf.Abs(firstSai.y - secondSai.y);//地图中的维度差
x_offset = Mathf.Abs(firstSai.x - secondSai.x);//地图中的经度差
z_w_offset = Mathf.Abs(firstPoint.localPosition.z - secondPoint.localPosition.z);//unity中的维度差
x_w_offset = Mathf.Abs(firstPoint.localPosition.x - secondPoint.localPosition.x);//unity中的经度差
}
public Vector3 getWorldPoint(Vector2 se)//由经纬度得到位置点
{
float tempX = Mathf.Abs(se.x - secondSai.x);
float tempZ = Mathf.Abs(se.y - firstSai.y);
float _tempX = (tempX * x_w_offset / x_offset + secondPoint.localPosition.x);
float _tempZ = (tempZ * z_w_offset / z_offset + firstPoint.localPosition.z);
return new Vector3(_tempX, 0f, _tempZ);
}
public Vector3 getLatLon(Vector3 curPoint)//由位置点得到经纬度
{
float _z_offset = Mathf.Abs(curPoint.z - secondPoint.localPosition.z) * z_offset / z_w_offset;
float _x_offset = Mathf.Abs(curPoint.x - firstPoint.localPosition.x) * x_offset / x_w_offset;
float resultX = _x_offset + firstSai.x;
float resultZ = _z_offset + secondSai.y;
return new Vector2(resultX, resultZ);
}
}
注意:在百度地图上获取的经纬度与实际地理位置差距会比较大,原因是国家规定百度地图上的地理位置与经纬度坐标不能一一对应,会有比较大的误差。所以检校时最好用自己公司的软件校验。