This component is useful to:
-
Convert units from one coordinate system to another (for instance from plane coordinates to degrees and vice-versa).
-
Calculate the distance between cities.
You may also use this component to capture the current cursor coordinates and convert them to other coordinate system.
You may enable this component in two ways:
-
From the Editor, clicking on the “Open Calculator” button at the bottom of the World Map Globe inspector.
-
From code, using any of its API through map.calc accessor. The first time you use its API, it will automatically add the component to the globe gameObject.
On the Inspector you will see the following custom editor:
Converting coordinates from code
NEW! The asset provides a new Conversion static class which exposes many methods to quickly convert between lat/lon and map position which don’t require the calculator component.
Example:
Vector3 localPos = Conversion.GetSpherePointFromLatLon(lat, lon);
In addition to the Conversion static class, you may use the calculator component from code. You may access the conversion API of this componet from code through map.calc property. The conversion task involves 3 steps:
- Specify the source unit (eg. “map.calc.fromUnit = UNIT_TYPE.DecimalDegrees”).
- Assign the source parameters (eg. “map.calc.fromLatDec = -15.281”)
- Call map.calc.Convert() method.
- Obtain the results from the fields map.calc.to* (eg. “map.calc.toLatDegrees”, “map.calc.toLatMinutes”, …).
Note that the conversion will provide results for decimal degrees, degrees and spherical coordinates. You don’t have to specify the destination unit (that’s only for the inspector window, in the API the conversion is done for the 3 types).
To convert from Decimal Degrees to any other unit you use:
map.calc.fromUnit = UNIT_TYPE.DecimalDegrees
map.calc.fromLatDec = <decimal degree for latitude>
map.calc.fromLonDec = <decimal degree for longitude>
map.calc.Convert()
To convert from Degrees, you do:
map.calc.fromUnit = UNIT_TYPE.Degrees
map.calc.fromLatDegrees = <degree for latitude>
map.calc.fromLatMinutes = <minutes for latitude>
map.calc.fromLatSeconds = <seconds for latitude>
map.calc.fromLonDegrees = <degree for longitude>
map.calc.fromLonMinutes = <minutes for longitude >
map.calc.fromLonSeconds = <seconds for longitude >
map.calc.Convert()
And finally, to convert from X, Y, Z (normalized) you use:
map.calc.fromUnit = UNIT_TYPE.SphereCoordinates
map.calc.fromX = <X position in local sphere coordinates >
map.calc.fromY = <Y position in local sphere coordinates >
map.calc.fromZ = <Z position in local sphere coordinates>
map.calc.Convert()
The results will be stored in (you pick what you need):
map.calc.toLatDec = <decimal degree for latitude>
map.calc.toLonDec = <decimal degree for longitude>
map.calc.toLatDegrees = <degree for latitude>
map.calc.toLatMinutes = <minutes for latitude>
map.calc.toLatSeconds = <seconds for latitude>
map.calc.toLonDegrees = <degree for longitude>
map.calc.toLonMinutes = <minutes for longitude >
map.calc.toLonSeconds = <seconds for longitude >
map.calc.toX = <X position in local sphere coordinates >
map.calc.toY = <Y position in local sphere coordinates >
map.calc.toZ = <Z position in local sphere coordinates>
You may also use the property map.calc.captureCursor = true, and that will continuously convert the current coordinates of the cursor (mouse) until it’s set to false or you right-click the game window.
Using the distance calculator from code
The component includes the following two APIs to calculate the distances in meters between two coordinates (latitude/longitude) or two cities of the current selected catalogue.
map.calc.Distance(float latDec1, float lonDec1, float latDec2, float lonDec2)
map.calc.Distance(City city1, City city2)
map.calc.Distance(Vector3 spherePosition1, Vector3 spherePosition2)
Using the Conversion static class
This class provides conversion methods that you can use directly without having an instance of the globe in the scene. It’s the new way to convert coordinates and we recommend you to use it instead of the map.calc component:
Vector2 GetUVFromLatLon(float lat, float lon)
Returns the texture coordinates (UV) of a latitude/longitude assuming the texture fills the entire globe.
Vector2 GetLatLonFromUV(Vector2 uv)
Returns the latitude/longitude from a UV coordinate.
Vector2 GetUVFromSpherePoint(Vector3 p)
Returns the texture coordinates (UV) from a point on the globe sphere.
Vector3 GetSpherePointFromLatLon(double lat, double lon, double altitude = 0)
Vector3 GetSpherePointFromLatLon(float lat, float lon, float altitude = 0f)
Vector3 GetSpherePointFromLatLon(Vector2 latLon, float altitude = 0)
Returns the sphere local position from a latitude/longitude and optional altitude.
GetLatLonFromSpherePoint(Vector3 p, out double lat, out double lon)
GetLatLonFromSpherePoint(Vector3 p, out float lat, out float lon)
GetLatLonFromSpherePoint(Vector3 p, out Vector2 latLon)
Vector2 GetLatLonFromSpherePoint(Vector3 p)
Returns the latitude/longitude from a local sphere position
float Distance(Vector3 position1, Vector3 position2)
float Distance(float latDec1, float lonDec1, float latDec2, float lonDec2)
Returns the distance in meters from two positions.
GetTileFromLatLon(int zoomLevel, float lat, float lon, out int xtile, out int ytile)
Returns the x/y indices of a tile from a zoom level, latitude and longitude.
Vector2 GetLatLonFromTile(float x, float y, int zoomLevel)
Returns the latitude/longitude of a tile.