diff --git a/sdkproject/Assets/Mapbox/Core/Unity/Editor/GeocodeAttributeDrawer.cs b/sdkproject/Assets/Mapbox/Core/Unity/Editor/GeocodeAttributeDrawer.cs index 56641df93..83136285c 100644 --- a/sdkproject/Assets/Mapbox/Core/Unity/Editor/GeocodeAttributeDrawer.cs +++ b/sdkproject/Assets/Mapbox/Core/Unity/Editor/GeocodeAttributeDrawer.cs @@ -4,6 +4,11 @@ namespace Mapbox.Editor using UnityEditor; using Mapbox.Unity.Utilities; + /// + /// Custom property drawer for geocodes + /// Includes a search window to enable search of Lat/Lon via geocoder. + /// Requires a Mapbox token be set for the project + /// [CustomPropertyDrawer(typeof(GeocodeAttribute))] public class GeocodeAttributeDrawer : PropertyDrawer { diff --git a/sdkproject/Assets/Mapbox/Core/Unity/MapboxAccess.cs b/sdkproject/Assets/Mapbox/Core/Unity/MapboxAccess.cs index af27b7375..ec76f8f1c 100644 --- a/sdkproject/Assets/Mapbox/Core/Unity/MapboxAccess.cs +++ b/sdkproject/Assets/Mapbox/Core/Unity/MapboxAccess.cs @@ -9,11 +9,19 @@ namespace Mapbox.Unity using Mapbox.Platform; using Mapbox.Unity.Utilities; + /// + /// Object for retrieving an API token and making http requests. + /// Contains a lazy Geocoder and a lazy Directions for convenience. + /// public class MapboxAccess : IFileSource { readonly string _accessPath = Path.Combine(Application.streamingAssetsPath, Constants.Path.TOKEN_FILE); static MapboxAccess _instance = new MapboxAccess(); + + /// + /// The singleton instance. + /// public static MapboxAccess Instance { get @@ -28,6 +36,10 @@ public static MapboxAccess Instance LoadAccessToken(); } + /// + /// The Mapbox API access token. + /// See Mapbox API Congfiguration in Unity. + /// string _accessToken; public string AccessToken { @@ -53,6 +65,9 @@ void ValidateMapboxAccessFile() } } + /// + /// Loads the access token from StreamingAssets. + /// void LoadAccessToken() { #if UNITY_EDITOR || !UNITY_ANDROID @@ -62,6 +77,9 @@ void LoadAccessToken() #endif } + /// + /// Android-specific token file loading. + /// IEnumerator LoadMapboxAccess() { var request = new WWW(_accessPath); @@ -72,6 +90,12 @@ IEnumerator LoadMapboxAccess() AccessToken = request.text; } + /// + /// Makes an asynchronous url query. + /// + /// The request. + /// URL. + /// Callback. public IAsyncRequest Request(string url, Action callback) { var uriBuilder = new UriBuilder(url); @@ -97,10 +121,11 @@ public InvalidTokenException(string message) : base(message) } } + Geocoder _geocoder; + /// /// Lazy geocoder. /// - Geocoder _geocoder; public Geocoder Geocoder { get @@ -113,10 +138,11 @@ public Geocoder Geocoder } } + Directions _directions; + /// /// Lazy Directions. /// - Directions _directions; public Directions Directions { get diff --git a/sdkproject/Assets/Mapbox/Core/Unity/Utilities/Conversions.cs b/sdkproject/Assets/Mapbox/Core/Unity/Utilities/Conversions.cs index ce927b1d1..458e94068 100644 --- a/sdkproject/Assets/Mapbox/Core/Unity/Utilities/Conversions.cs +++ b/sdkproject/Assets/Mapbox/Core/Unity/Utilities/Conversions.cs @@ -48,6 +48,21 @@ private static Vector2d LatLonToMeters(double lat, double lon) return new Vector2d((float)posx, (float)posy); } + /// + /// Converts WGS84 lat/lon to x/y meters in reference to a center point + /// + /// The latitude. + /// The longitude. + /// A center point to offset resultant xy + /// Scale in meters. (default scale = 1) + /// A xy tile ID. + /// + /// Converts a Lat/Lon of (37.7749, 122.4194) into Unity coordinates for a map centered at (10,10) and a scale of 2.5 meters for every 1 Unity unit + /// + /// var worldPosition = Conversions.GeoToWorldPosition(37.7749, 122.4194, new Vector2d(10, 10), (float)2.5); + /// // worldPosition = ( 11369163.38585, 34069138.17805 ) + /// + /// public static Vector2d GeoToWorldPosition(double lat, double lon, Vector2d refPoint, float scale = 1) { var posx = lon * OriginShift / 180; @@ -56,8 +71,8 @@ public static Vector2d GeoToWorldPosition(double lat, double lon, Vector2d refPo return new Vector2d((posx - refPoint.x) * scale, (posy - refPoint.y) * scale); } - public static Vector2d GeoToWorldPosition(Vector2d latLong, Vector2d refPoint, float scale = 1) - { + public static Vector2d GeoToWorldPosition(Vector2d latLong, Vector2d refPoint, float scale = 1) + { return GeoToWorldPosition(latLong.x, latLong.y, refPoint, scale); } @@ -67,6 +82,15 @@ public static Vector2d GeoToWorldPosition(Vector2d latLong, Vector2d refPoint, f /// /// A of coordinates in meters. /// The in lat/lon. + + /// + /// Converts EPSG:900913 xy meter coordinates to lat lon + /// + /// var worldPosition = new Vector2d (4547675.35434,13627665.27122); + /// var latlon = Conversions.MetersToLatLon(worldPosition); + /// // latlon = ( 37.77490, 122.41940 ) + /// + /// public static Vector2d MetersToLatLon(Vector2d m) { var vx = (m.x / OriginShift) * 180; @@ -81,6 +105,15 @@ public static Vector2d MetersToLatLon(Vector2d m) /// XY coords in meters. /// Zoom level. /// A xy tile ID. + /// + /// + /// Converts EPSG:900913 xy meter coordinates to web mercator tile XY coordinates at zoom 12. + /// + /// var meterXYPosition = new Vector2d (4547675.35434,13627665.27122); + /// var tileXY = Conversions.MetersToTile (meterXYPosition, 12); + /// // tileXY = ( 655, 2512 ) + /// + /// public static Vector2 MetersToTile(Vector2d m, int zoom) { var p = MetersToPixels(m, zoom); diff --git a/sdkproject/Assets/Mapbox/Core/Unity/Utilities/GeocodeAttribute.cs b/sdkproject/Assets/Mapbox/Core/Unity/Utilities/GeocodeAttribute.cs index e120e73ae..512f814e7 100644 --- a/sdkproject/Assets/Mapbox/Core/Unity/Utilities/GeocodeAttribute.cs +++ b/sdkproject/Assets/Mapbox/Core/Unity/Utilities/GeocodeAttribute.cs @@ -2,6 +2,11 @@ { using UnityEngine; + /// + /// Property attribute used to create custom Geocode GUI field + /// Links to custom drawer: + /// . + /// public class GeocodeAttribute : PropertyAttribute { diff --git a/sdkproject/Assets/Mapbox/Core/Unity/Utilities/VectorExtensions.cs b/sdkproject/Assets/Mapbox/Core/Unity/Utilities/VectorExtensions.cs index 5e77131e9..d6c9db83f 100644 --- a/sdkproject/Assets/Mapbox/Core/Unity/Utilities/VectorExtensions.cs +++ b/sdkproject/Assets/Mapbox/Core/Unity/Utilities/VectorExtensions.cs @@ -11,47 +11,118 @@ namespace Mapbox.Unity.Utilities public static class VectorExtensions { + /// + /// Vector2 convenience method to convert Vector2 to Vector3. + /// + /// Vector3 with a y value of zero. + /// Vector2. public static Vector3 ToVector3xz(this Vector2 v) { return new Vector3(v.x, 0, v.y); } + /// + /// Vector2d convenience method to convert Vector2d to Vector3. + /// + /// Vector3 with a y value of zero. + /// Vector2d. public static Vector3 ToVector3xz(this Vector2d v) { return new Vector3((float)v.x, 0, (float)v.y); } + /// + /// Vector3 convenience method to convert Vector3 to Vector2. + /// + /// The Vector2. + /// Vector3. public static Vector2 ToVector2xz(this Vector3 v) { - return new Vector3(v.x, v.z); + return new Vector2(v.x, v.z); } + /// + /// Vector3 convenience method to convert Vector3 to Vector2d. + /// + /// The Vector2d. + /// Vector3. public static Vector2d ToVector2d(this Vector3 v) { return new Vector2d(v.x, v.z); } + /// + /// Transform extension method to move a Unity transform to a specific latitude/longitude. + /// + /// Transform. + /// Latitude. + /// Longitude. + /// Reference point. + /// Scale. + /// + /// Place a Unity transform at 10, 10, with a map center of (0, 0) and scale 1: + /// + /// transform.MoveToGeocoordinate(10, 10, new Vector2d(0, 0), 1f); + /// Debug.Log(transform.position); + /// // (1113195.0, 0.0, 1118890.0) + /// + /// public static void MoveToGeocoordinate(this Transform t, double lat, double lng, Vector2d refPoint, float scale = 1) { t.position = Conversions.GeoToWorldPosition(lat, lng, refPoint, scale).ToVector3xz(); } + /// + /// Transform extension method to move a Unity transform to a specific Vector2d. + /// + /// Transform. + /// Latitude Longitude. + /// Reference point. + /// Scale. + /// + /// Place a Unity transform at 10, 10, with a map center of (0, 0) and scale 1: + /// + /// transform.MoveToGeocoordinate(new Vector2d(10, 10), new Vector2d(0, 0), 1f); + /// Debug.Log(transform.position); + /// // (1113195.0, 0.0, 1118890.0) + /// + /// public static void MoveToGeocoordinate(this Transform t, Vector2d latLon, Vector2d refPoint, float scale = 1) { t.MoveToGeocoordinate(latLon.x, latLon.y, refPoint, scale); } + /// + /// Vector2 extension method to convert from a latitude/longitude to a Unity Vector3. + /// + /// The Vector3 Unity position. + /// Latitude Longitude. + /// Reference point. + /// Scale. public static Vector3 AsUnityPosition(this Vector2 latLon, Vector2d refPoint, float scale = 1) { return Conversions.GeoToWorldPosition(latLon.x, latLon.y, refPoint, scale).ToVector3xz(); } + /// + /// Transform extension method to return the transform's position as a Vector2d latitude/longitude. + /// + /// Vector2d that represents latitude/longitude. + /// T. + /// Reference point. + /// Scale. + /// + /// Get the latitude/longitude of a transform at (1113195, 0, 1118890), map center (0, 0) and scale 1. + /// + /// var latLng = transform.GetGeoPosition(new Vector2d(0, 0), 1); + /// Debug.Log(latLng); + /// // (10.00000, 10.00000) + /// + /// public static Vector2d GetGeoPosition(this Transform t, Vector2d refPoint, float scale = 1) { var pos = refPoint + (t.position / scale).ToVector2d(); return Conversions.MetersToLatLon(pos); } - - // TODO: add ability to get geo position from a vector2 or vector 3, as well (not just transform). } }