#### Topic: Reverse method LatLongToMercat in OnlineMapsUtils

Hello!

I need a method to reverse this. Tell me, please, how to do this? It's strange that the plugin does not have this method.

You need to convert gps Mercator to LonLat

public static Vector2 LatLongToMercat(float x, float y)
{
float sy = Mathf.Sin(y * Mathf.Deg2Rad);
return new Vector2((x + 180) / 360, 0.5f - Mathf.Log((1 + sy) / (1 - sy)) / pi4);
}

#### Re: Reverse method LatLongToMercat in OnlineMapsUtils

Hello.

This method is not used in internal calculations, and no one ever asked about it.
Something like:

``````public static void MercatToLatLong(double mx, double my, out double x, out double y)
{
uint mapSize = (uint)tileSize << 20;
double px = Clip(mx * mapSize + 0.5, 0, mapSize - 1);
double py = Clip(my * mapSize + 0.5, 0, mapSize - 1);
mx = px / tileSize;
my = py / tileSize;
TileToLatLong(mx, my, 20, out x, out y);
}``````
Kind Regards,
Infinity Code Team

#### Re: Reverse method LatLongToMercat in OnlineMapsUtils

Alex Vertax, thx!

#### Re: Reverse method LatLongToMercat in OnlineMapsUtils

Unfortunately, the method does not work correctly.

``````    public static (double, double) MercatToLatLong(float mx, float my) {
uint mapSize = (uint)tileSize << 20;
double px = Clip(mx * mapSize + 0.5, 0, mapSize - 1);
double py = Clip(my * mapSize + 0.5, 0, mapSize - 1);
mx = (float)(px / tileSize);
my = (float)(py / tileSize);
TileToLatLong(mx, my, 20, out double x, out double y);
return (x, y);
}``````

Should return approximately 38,97316; 45,02914, and returns -179.999998658895; -85,0511286641139

Help!)

#### Re: Reverse method LatLongToMercat in OnlineMapsUtils

I made a test script and it works correctly.
Script:

``````using UnityEngine;

public class TestMercatToLngLat : MonoBehaviour
{
private void Start()
{
OnlineMapsControlBase.instance.OnMapClick += OnMapClick;
}

private void OnMapClick()
{
Vector2 coords = OnlineMapsControlBase.instance.GetCoords();
Debug.Log(coords.ToString("F4"));
Vector2 mercat = OnlineMapsUtils.LatLongToMercat(coords.x, coords.y);
Debug.Log(mercat.ToString("F4"));
double x, y;
OnlineMapsUtils.MercatToLatLong(mercat.x, mercat.y, out x, out y);
Debug.Log(x + "  " + y);
}
}``````

Output:

``````(-9.7852, 13.2383) // Coords
(0.4728, 0.4629) // Mercator (0-1)
-9.78522352874279  13.2382842992934 // Coords``````

Perhaps you pass incorrect data to the method.
This requires a position in the Mercator projection (0-1).

Kind Regards,
Infinity Code Team

#### Re: Reverse method LatLongToMercat in OnlineMapsUtils

Alex Vertax, you're right! I 2 years ago from scratch wrote a map based on tiles from OpenStreetMap and used a meters in type Int. I wonder why you use double? This is in fact just a type that is more demanding on memory. Or is it about optimizing internal computation, by using double, instead of int?
And the question. For some reason, each coordinate conversion system "gps to mercator" produces different results. The methods for C # from OpenStreetMap give exactly and exactly the same results as expected when converting. All the others, no matter what I try, give excellent results, and as a result, the results are not correct, including your convertation methods too) How so?)
Even on node.js I could not find the library of conversion, which would give the same results. Therefore, on the server I do not convert the coordinates, but I trust this to the client, because only on the client methods C # from OpenStreetMap

#### Re: Reverse method LatLongToMercat in OnlineMapsUtils

Unfortunately, I do not understand what meters you mean.

Usually the following terms are used in maps:
Geographical coordinates, tile coordinates (tile position), Mercator coordinates.
Meters are used for distances. But this is not used for internal calculations.

Most likely you mean the tile coordinates.
You can always convert double to int (or long), if you only need to know the tile index.
But you can not convert int to double to find out where exactly in the tile is some kind of coordinate.

Using of double is not a bottleneck and does not have any significant effect on performance and memory usage in 99.9% of applications.
So using double has only positive sides.

The methods for C # from OpenStreetMap...

What is it?
I could not find anything suitable in Google.

All the others, no matter what I try, give excellent results, and as a result, the results are not correct, including your convertation methods too) How so?)

I'm not sure I understand what you meant.
You mean that (for example) by converting geographic coordinates into tile coordinates, and then converting back you have a slightly different result, right?
This is a floating-point error.
But it is small enough to ignore it.

Kind Regards,
Infinity Code Team

#### Re: Reverse method LatLongToMercat in OnlineMapsUtils

It turns out they have these methods in all the major languages.
https://wiki.openstreetmap.org/wiki/Mercator

Look at "C# implementation"

#### Re: Reverse method LatLongToMercat in OnlineMapsUtils

I drew at Unity3D at home in 3D on top of the openstreetmap tile map, where houses are painted. So I needed an absolutely exact conversion. I also remember the problem of the mismatch between my 3D homes and houses on tiles closer to the poles.

#### Re: Reverse method LatLongToMercat in OnlineMapsUtils

OSM uses tangent, I use this formula (LatLongToPixelXY method) that uses the sine:
https://msdn.microsoft.com/en-us/library/bb259689.aspx
But in fact we have the same result.
Look at the screenshot (attached). The building is absolutely in the same place where it is painted in OSM.

Kind Regards,
Infinity Code Team

#### Re: Reverse method LatLongToMercat in OnlineMapsUtils

Cool there explain) I would have been 2 years ago) I came up with my map architecture. This information on the link I see for the first time) But the map was completed) And the plug-ins in the assetstore have more features. I was looking in the assetstore then, and apparently then there were no 2d cards, only 3d. Or badly looking)

#### Re: Reverse method LatLongToMercat in OnlineMapsUtils

More question.
Can I also use the Mercator coordinates to calculate the distance? As you(0,6082588; y = 0,3596105) figures in Mercator does not match with OSM (5006688б,4702653). I've always counted the distance in Mercator meters. And these calculations don't match yours. It just turns out that the radius calculated for the Mercator meters using OSM methods is 1.3 times greater than your radius.
And I on the server need to calculate the distance as easy as possible.

#### Re: Reverse method LatLongToMercat in OnlineMapsUtils

You mean the distance between the points?
You need to convert the Mercator's position to geographic coordinates, and use OnlineMapsUtils.DistanceBetweenPoints.

If you meant something else, please show the code so I understand what you are trying to calculate.

Kind Regards,
Infinity Code Team