Unity3d Gis Coordinate transformation

Source: Internet
Author: User

Recently in the Unity3d and GIS projects, the most important thing is the transformation of coordinates. For example, the latitude and longitude (166.23.9.27. 39.55.15.74) transitions into unity into a corresponding location point. Don't say much nonsense about the code:
Using unityengine;using System.collections;public class Saigetlatlog:monobehaviour {public Transform firstpoint;// Unity in the upper left point public Transform secondpoint;//unity in the right-hand side of the public Saiearth firstsai;//map in the corresponding upper-left latitude and longitude point public Saiearth secondsai;// Corresponding right down latitude point in the map private float z_offset,x_offset,z_w_offset,x_w_offset;private Raycasthit rayhit;//use the This for Initializationvoid Start () {initbasicnum ();//Initialize the parameter}//update is called once per framevoid update () {if (input.getmouseb Utton (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 = n EW 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));//The dimension difference in the map 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));//longitude difference in map Z_w_offset = Mathf.abs (firstpoint.localposition.z-secondpoint.localposition.z); the dimension difference in//unity X_w_offset = Mathf.Abs ( firstpoint.localposition.x-secondpoint.localposition.x); Longitude difference in//unity}vector3 getworldpoint (SaiEarth se)// Get location Point {float tempx = (float) (se.longitude.x + se.longitude.y/60 + se.longitude.z/3600-(secondsai.longitude.x + se) by latitude and longitude CONDSAI.LONGITUDE.Y/60 + secondsai.longitude.z/3600)); float Tempz = (float) (se.latitude.x + se.latitude.y/60 + se.la titude.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 + secon DPOINT.LOCALPOSITION.Z); return new Vector3 ((float) _tempx,0f, (float) _tempz);} SaieartH Getlatlon (Vector3 curpoint)//By the location point to get latitude and longitude {Saiearth Tempearth = new Saiearth (); Float _z_offset = Mathf.abs (curpoint.z-sec ONDPOINT.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.longitud e.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;}}

It has a Saiearth class, which is actually very easy to write:

Using unityengine;using System.collections;public class Saiearth  {public Vector3 longitude;public Vector3 latitude ;p ublic Saiearth () {}public Saiearth (Vector3 Longitude,vector3 latitude) {this.latitude = Latitude;this.longitude = Longitude;} public string toString () {return "Lat:" +this.latitude + ", Long:" + This.longitude;}}
OK to finish.

Doing u3d for some time is purely personal interest. Now has not graduated, will encounter a lot of problems in the future, I hope I have perseverance can persist. Update the blog post for introspection.

The infinite behind of the boundless

Unity3d Gis Coordinate transformation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.