Conversion API

advanced scripting

World Map Strategy Kit · Scripting Support (C#)

Class: Conversion (static, namespace WorldMapStrategyKit). No WMSK instance required, call methods directly: Conversion.GetLocalPositionFromLatLon(...).

Conversion API

Utility methods for converting between latitude/longitude, 2D map coordinates, UV coordinates, billboard space and tile coordinates, plus great-circle distance on Earth.

Constants

const float EARTH_RADIUS = 6371000f

Mean Earth radius in meters.

const float EARTH_RADIUS_KM = 6371f

Mean Earth radius in kilometers.

Lat/Lon ↔ Map Position

static Vector2 GetLocalPositionFromLatLon(float lat, float lon)

Converts geographic coordinates to a 2D map position in WMSK's local space (range roughly -0.5 to 0.5 on both axes).

static Vector3 GetLocalPositionFromLatLon(Vector2 latLon)

Same as above but takes lat/lon as a Vector2 (x = lat, y = lon) and returns a Vector3 (z = 0).

static Vector2 GetLatLonFromLocalPosition(Vector2 position)

Inverse of GetLocalPositionFromLatLon. Returns a Vector2 with x = latitude (-90 to 90) and y = longitude (-180 to 180).

Lat/Lon ↔ UV

static Vector2 GetUVFromLatLon(float lat, float lon)

Converts geographic coordinates to UV texture coordinates (range 0 to 1).

static Vector2 GetLatLonFromUV(Vector2 uv)

Inverse of GetUVFromLatLon. uv must be in the 0-1 range.

static Rect GetUVRectFromLatLonRect(Rect latlonRect)

Converts a lat/lon rectangle into UV space.

Billboard Coordinates

static Vector2 GetBillboardPointFromLatLon(Vector2 latlon)

Converts lat/lon to a position in WMSK's 200×100 billboard space.

static Vector2 GetLatLonFromBillboard(Vector2 position)

Inverse of GetBillboardPointFromLatLon.

static Rect GetBillboardRectFromLatLonRect(Rect latlonRect)

Converts a lat/lon rectangle into billboard space.

static Vector2 GetBillboardPosFromSpherePoint(Vector3 p)

Converts a point on a unit sphere to billboard space. Useful when integrating with the globe edition or with a 3D sphere mesh.

Texture Indexing

static Vector2 ConvertToTextureCoordinates(Vector3 localPos, int width, int height)

Maps a map-local position to integer pixel coordinates on a texture of the given size.

Great-Circle Distance

static float Distance(float lat1, float lon1, float lat2, float lon2)

Returns the great-circle (haversine) distance between two geographic points, in meters. Divide by 1000 for kilometers.

static float Distance(Vector2 position1, Vector2 position2)

Returns the great-circle distance between two map positions, in meters. Internally converts to lat/lon and calls the overload above.

Want kilometers? Divide the result by 1000, or compare against Conversion.EARTH_RADIUS_KM if reasoning about ratios.

Tile Helpers (Web Mercator)

Helpers that map between Web Mercator tile indices and geographic / map-local coordinates. Tile indices follow the standard X (longitude, west-to-east) and Y (latitude, north-to-south) convention used by most online tile providers; latitude is clamped to ±80° to stay inside Web Mercator's valid range.

static void GetTileFromLatLon(int zoomLevel, float lat, float lon, out int xtile, out int ytile)

Returns the tile that contains the given lat/lon at the requested zoom level.

static Vector2 GetLatLonFromTile(float x, float y, int zoomLevel)

Returns the lat/lon of the top-left corner of the given tile.

static Vector2 GetLocalPositionFromTile(float x, float y, int zoomLevel)

Returns the map-local position for the top-left corner of the given tile. Equivalent to GetLocalPositionFromLatLon(GetLatLonFromTile(...)).

Example

using UnityEngine;
using WorldMapStrategyKit;

public class DistanceExample : MonoBehaviour {

    void Start() {
        // Madrid: 40.4168, -3.7038
        // Tokyo: 35.6762, 139.6503
        float meters = Conversion.Distance(40.4168f, -3.7038f, 35.6762f, 139.6503f);
        Debug.Log($"Madrid to Tokyo: {meters / 1000f:F1} km");

        // Place a marker at New York lat/lon
        Vector2 nyMap = Conversion.GetLocalPositionFromLatLon(40.7128f, -74.0060f);
        WMSK.instance.AddMarker2DSprite(myMarkerPrefab, nyMap, 0.01f);
    }
}
Was this page helpful?