近期在做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的类,事实上写的非常easy:

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;
	}
}
OK 完工。

做u3d有一段时间了  纯属个人兴趣。如今还没毕业,以后还会遇到非常多问题 ,希望自己有毅力能坚持。更新博文以自省。  

学海无涯 不进则退

posted on 2017-04-15 21:22  ycfenxi  阅读(1198)  评论(0编辑  收藏  举报