Small knowledge, big challenge! This paper is participating in theEssentials for programmers”Creative activities
0, write first
Longitude and latitude are the sum of longitude and latitude to form a coordinate system. Also known as geographic coordinate system, it is a spherical coordinate system that uses the sphere of three dimensions to define the space on the earth, and can mark any position on the earth. Latitude: Latitude is an auxiliary line hypothesized by humans for measuring convenience, as well as longitude. It is defined as the trajectory formed by a point on the earth’s surface as the Earth rotates. Any latitude line is circular and parallel to each other. The length of the latitude line is the circumference of the equator times the cosine of the latitude of the latitude line, so the equator is the longest, and the farther away the latitude line is from the equator, the shorter its circumference gets, and it shrinks to zero at the poles. The latitude indicates the east-west direction. North and south from the equator, each 90 degrees, known as the north and south latitude, denoted by “N” and “S” respectively. Meridian: meridian is also known as meridian, and latitude is the same as the human for the convenience of measurement and assumed out of the auxiliary line, defined as the earth’s surface connecting the north and south poles of the great circle of the semi-arc. Any two meridians of equal length intersect at the north and south poles. Each line of longitude has a corresponding value, called longitude. The longitude indicates a north-south direction. Longitude: positive east longitude, negative west longitude. Longitude is the number of degrees east or west of a point on Earth from a north-south line called the prime meridian. Latitude: positive north latitude, negative south latitude. Latitude is the line Angle between a point and the earth’s center and the earth’s equatorial plane. It is between 0 and 90 degrees. The latitude of the point north of the equator is called north latitude, denoted N; The latitude of the point south of the equator is called south latitude and is denoting S.
In some Unity 3 d simulation of the project, we sometimes meet requirements about Unity world coordinates to turn the earth longitude and latitude, on other sites also have some friends and some code on the Unity of world coordinates to turn the earth longitude and latitude algorithm’s case, but some don’t support the commercial mostly, the error is bigger, first we starting from the fundamental, To understand the theoretical knowledge of the longitude and latitude algorithm, combined with the theoretical knowledge, we wrote the code according to the formula will greatly reduce the error.
1. Draw a sketch with Word first
2, analysis,
It can be seen that O is the center of the sphere, O ‘is the theoretical North Pole, A is the zero point of latitude and longitude, C is any point of longitude at the same latitude, B is on ⌒O ‘c, OB is the radius of the sphere, ⌒AC is an arc on the equator, B is the target latitude and longitude coordinates, B’ is the negative projection of the z axis of B on the plane XY. OC is the negative projection of O ‘C on the z axis of plane XY, then B’ is on OC, so ∠AOB ‘is longitude, and ∠B’ OB is latitude.
Lon = ∠AOB ‘, Lat = ∠B ‘OB, R = OB, B(X, Y, Z)
3, latitude and longitude to space coordinates
We know that the actual distance for each degree of longitude or latitude is different, so when we calculate it, we calculate the latitude function by Fourier cosine series.
These parameters are given by the elliptic function of E2 for the WGS84 sphere
M1 = 111132.95255 m2 = -559.84957 m3 = 1.17514 m4 = -0.00230 P1 = 111412.87733 P2 = -93.50412 p3 = 0.11774p4 = 0.000165Copy the code
Note: P4 is too small to be ignored. Then substitute the constant into the Fourier cosine series as follows:
Translate into C# code
Then, the code is represented as
// </summary> // <param name="lat"></param> private void FindMetersPerLat(double lat) {double lat M1 = 111132.92 d; // double m2 = -559.82d; // double m3 = 1.175d; // double m4 = -0.0023d; // double p1 = 111412.84d; // double p2 = -93.5d; // double p3 = 0.118d; // lat *= mathf.deg2rad; // Calculate the length of a latitude and longitude, In meters metersPerLat = M1 + (m2 * convert. ToDouble(mathf. Cos(2 * (float)lat)) + (m3 * convert. ToDouble(mathf. Cos(4 * (float)lat))) + (m4 * Convert.ToDouble(Mathf.Cos(6 * (float)lat))); metersPerLon = (p1 * Convert.ToDouble(Mathf.Cos((float)lat))) + (p2 * Convert.ToDouble(Mathf.Cos(3 * (float)lat))) + (p3 * Convert.ToDouble(Mathf.Cos(5 * (float)lat))); }Copy the code
World coordinates to latitude and longitude:
private DoubleVector2 ConvertUCStoGPS(DoubleVector3 position) { FindMetersPerLat(_LatOrigin); DoubleVector2 geoLocation = new DoubleVector2(0, 0); geoLocation.x = (_LatOrigin + (position.z) / metersPerLat); GeoLocation. Y = (_LonOrigin + (position.x)/metersPerLon); // Calculate the current longitude. Return geoLocation; }Copy the code
Latitude and longitude to world coordinates:
private DoubleVector3 ConvertGPStoUCS(DoubleVector2 gps) { FindMetersPerLat(_LatOrigin); double zPosition = metersPerLat * (gps.x - _LatOrigin); Double xPosition = metersPerLon * (gps.y - _LonOrigin); Return new DoubleVector3(xPosition, 0, zPosition); }Copy the code
Note: DoubleVector3 and DoubleVector2 are vectors of type Double that I encapsulate separately, the same as Vector2 and Vector3, except that they must use Double data.
Float Vector3 to DoubleVector3:
internal static class VectorUtil { public static DoubleVector3 ToDoubleVector3(this Vector3 vector3) { double x = Convert.ToDouble(vector3.x); double y = Convert.ToDouble(vector3.y); double z = Convert.ToDouble(vector3.z); return new DoubleVector3(x, y, z); }}Copy the code
Tips: there is a SetLocalOrigin method in this project, by default all your transformations will assume that you use a (0,0) longitude and latitude origin. This puts your real-life origins at earth’s latitude and longitude outside the Gulf of New Guinea in Africa. When using the default (0,0), all your GPS conversions will be located in real life. However, sometimes you just want to convert the GPS coordinates to the uniformity of the local area within the UCS (0,0,0). In order to concentrate on a local area in origin. You need to find the origin with your scene. If you pass that coordinate to that function. All remaining session transitions will be associated with the new origin.
Links:Pan.baidu.com/s/19vYKmS1b…Extraction code: MF5B