An overview of the

Was in a simulation project, sometimes need to load the map on a piece of landscape and architecture, this time we only know these buildings in the latitude and longitude coordinates on the map, don’t know the world coordinates in Unity, so how do you all buildings according to the real map display has become a problem, this article is mainly introduced, How to calculate its world coordinates in Unity using latitude and longitude.

Note: Since Vector3 in Unity is a float, the data used in this article is of the float type. The accuracy may be slightly incorrect. If the accuracy is high, please rewrite Unity’s Vector by yourself. I’m going to rewrite it as a double, and I’m going to use a double during the conversion, but it’s going to be a float when I return a double at the end.

Thought analysis

Latitude and longitude are combined to form a Vector2 type of data. First we create two points in the Unity, the position of the two points is a square, rectangle, etc.), the diagonal of the location, respectively on the map to find two points of latitude and longitude, also is to use these two points, on the map and circle a range, will find the two points of latitude and longitude, were assigned to create two points in the Unity, Thus, our range position in Unity corresponds to the position in the display map, and the coordinates are calculated by the difference between the X and Z values of the coordinates and the latitude and longitude. The detailed diagram is as follows:

In the figure above, as long as the latitude and longitude of point AB in Figure 2 are assigned to point AB in Figure 2, the coordinates of all points in the square composed of two points AB can be calculated in real time through coordinate difference

Function implementation

The two balls in the figure above are the key points to determine the position. If both of these points are not identified, then everything is in vain. In general terms, given the position of A and B and the corresponding value, and given the position of C, it’s the same thing as asking you to figure out the corresponding value of C. We can set the location of these two registration points dynamically in our program, rather than manually adjusting them over and over again.

In the program, we can also obtain the longitude and latitude of the center point, and determine the coordinates of the two angles by adding or subtracting to the left and right. The advantage of this is that any latitude and longitude can be calculated, and it is within the range of the registration point single.

First of all, the initialization of the program, to determine the coordinates of the two anchor points, and the difference between the two coordinate points in Unity latitude and longitude

// BottomRightSai = new Vector2(113.98071f, 22.52864f); // BottomRightSai = new Vector2(113.98071f, 22.52864f); // BottomRightSai = new Vector2(113.98071f, 22.52864f); // Dot 2 TopLeftSai = new Vector2(80.15071f, 40.56864f); z_offset = BottomRightSai.y - TopLeftSai.y; X_offset = bottomrightsa.x - topleftsa.x; / / map of longitude difference z_w_offset = BottomRightPoint position. Z - TopLeftPoint. Position. Z; / / the unity of the dimension difference x_w_offset = BottomRightPoint. Position. X-ray TopLeftPoint. Position. X; // The longitude difference in UnityCopy the code

The next step is to calculate the coordinates of the point in Unity using the known latitude and longitude

Note: This method cannot calculate the height in Unity, the Y axis of all objects is fixed, the same as the anchor point. If you want to use height, you need Unity to read the grayscale image to get the height

Public Vector3 (Vector3, Vector3, Vector3, Vector3, Vector3, Vector3, Vector3) GetWorldPoint(Vector2 se) { double tempX = se.x - TopLeftSai.x; double tempZ = se.y - BottomRightSai.y; double _tempX = (tempX * x_w_offset / x_offset + TopLeftPoint.position.x); / / calculate X double _tempZ = (tempZ * z_w_offset/z_offset + BottomRightPoint. Position. Z); Return new Vector3((float)_tempX, 0, (float)_tempZ); }Copy the code

The same principle applies to the inverse derivation of longitude and latitude from Unity coordinates, and the problem mentioned above is the same. Generally, latitude and longitude are double data. Float is used in this method, so the accuracy may be biased

Public Vector3 </ /return > public Vector3 </ /return > public Vector3 </ /return GetLatLon (Vector3 curPoint) {/ / double coordinates deviation _x_offset = curPoint. X-ray BottomRightPoint. Position. (x) * x_offset / x_w_offset; double _z_offset = (curPoint.z - TopLeftPoint.position.z) * z_offset / z_w_offset; double resultX = _x_offset + BottomRightSai.x; double resultZ = _z_offset + TopLeftSai.y; return new Vector2((float)resultX, (float)resultZ); }Copy the code

Here in Unity, the function of longitude and latitude rotation and world coordinate rotation is realized, if there is not understand can more research under the code, in fact, the principle is very well understood, the case source code will be released after finishing.

Pay attention to

This case function is relatively single, accuracy due to the conversion between float and double, there will be a little deviation, the students who mind please deroute.

Write in the last

The whole project case will be sorted out and shared with you later. If there are any mistakes, please point them out.