Unity3d Gis Coordinate Conversion

Source: Internet
Author: User

Recently, the unity3d and Gis integration project is about coordinate conversion, for example, converting the longitude and latitude (166.23.9.27, 39.55.15.74) to the corresponding position point in unity, not much nonsense about the Code:

Using UnityEngine; using System. collections; public class SaiGetLatLog: MonoBehaviour {public Transform firstPoint; // public Transform secondPoint at the upper left of Unity; // public SaiEarth firstSai at the lower right of Unity; // public SaiEarth secondSai of the upper left latitude and longitude of the map; // private float z_offset, x_offset, z_w_offset, x_w_offset, and private RaycastHit rayHit; // Use this for initializationvoid Start () {InitBasicNum (); // initialization parameter} // Update is called once per framevoid 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 (effecf, 23f, 9.27f), new Vector3 (39f, 55f, 15.74f )); secondSai = new SaiEarth (new Vector3 (effecf, 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 x_offset = Mathf in the map. abs (firstSai. longpolling. x + firstSai. longpolling. y/60 + firstSai. longpolling. z/3600)-(secondSai. longpolling. x + secondSai. longpolling. y/60 + secondSai. longpolling. z/3600); // the longitude difference z_w_offset = Mathf in the map. abs (firstPoint. localPosition. z-secondPoint. localPosition. z); // dimension difference x_w_offset = Mathf in unity. abs (firstPoint. localPosition. x-secondPoint. localPosition. x); // longitude difference in unity} Vector3 getWorldPoint (SaiEarth se) // obtain the position point {float tempX = (float) (se. longpolling. x + se. longpolling. y/60 + se. longpolling. z/3600-(secondSai. longpolling. x + secondSai. longpolling. y/60 + secondSai. longpolling. 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) // obtain the longitude and latitude from the location point {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. longpolling. x + secondSai. longpolling. y/60 + secondSai. longpolling. z/3600); float resultZ = _ z_offset + (secondSai. latitude. x + secondSai. latitude. y/60 + secondSai. latitude. z/3600); tempEarth. longpolling = 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 ;}}

There is a SaiEarth class above, which is actually very simple:

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.

I have been doing u3d for a period of time. I have never graduated yet and will encounter many problems in the future. I hope that I can stick to it with perseverance and update my blog post for introspection!

If you do not want to enter, you will leave.

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.