/* INFINITY CODE */ /* https://infinity-code.com */ using System; using System.Collections.Generic; using UnityEngine; namespace InfinityCode.OnlineMapsExamples { /// /// Example how to draw a circle around a marker /// public class BouncingCircle : MonoBehaviour { private static BouncingCircle instance; public AnimationCurve curve = AnimationCurve.EaseInOut(0, 0, 1, 1); public float duration = 3; /// /// Radius of the circle /// public float radiusKM = 0.1f; /// /// Number of segments /// public int segments = 32; private List items; /// /// This method is called when a user clicks on a map /// private void OnMapClick() { // Get the coordinates under cursor double lng, lat; OnlineMapsControlBase.instance.GetCoords(out lng, out lat); OnlineMapsVector2d center = new OnlineMapsVector2d(lng, lat); // Create a new marker under cursor OnlineMapsMarkerManager.CreateItem(lng, lat, "Marker " + OnlineMapsMarkerManager.CountItems); OnlineMaps map = OnlineMaps.instance; // Get the coordinate at the desired distance double nlng, nlat; OnlineMapsUtils.GetCoordinateInDistance(lng, lat, radiusKM, 90, out nlng, out nlat); double tx1, ty1, tx2, ty2; // Convert the coordinate under cursor to tile position map.projection.CoordinatesToTile(lng, lat, 20, out tx1, out ty1); // Convert remote coordinate to tile position map.projection.CoordinatesToTile(nlng, nlat, 20, out tx2, out ty2); // Calculate radius in tiles double r = tx2 - tx1; // Create a new array for points OnlineMapsVector2d[] points = new OnlineMapsVector2d[segments]; // Calculate a step double step = 360d / segments; // Calculate each point of circle for (int i = 0; i < segments; i++) { double px = tx1 + Math.Cos(step * i * OnlineMapsUtils.Deg2Rad) * r; double py = ty1 + Math.Sin(step * i * OnlineMapsUtils.Deg2Rad) * r; map.projection.TileToCoordinates(px, py, 20, out lng, out lat); points[i] = new OnlineMapsVector2d(lng, lat); } // Create a new polygon to draw a circle OnlineMapsDrawingPoly poly = new OnlineMapsDrawingPoly(points, Color.red, 3); OnlineMapsDrawingElementManager.AddItem(poly); if (items == null) items = new List(); items.Add(new BoundingItem { points = points, center = center }); } /// /// This method is called when the script starts /// private void Start() { instance = this; // Subscribe to click on map event OnlineMapsControlBase.instance.OnMapClick += OnMapClick; } private void Update() { if (items == null || items.Count == 0) return; items.RemoveAll(i => i.Update()); OnlineMaps.instance.Redraw(); } public class BoundingItem { public OnlineMapsVector2d[] points; public bool finished; public OnlineMapsVector2d center; private float progress; public bool Update() { progress += Time.deltaTime / instance.duration; if (progress >= 1) { progress = 1; finished = true; } float radius = instance.radiusKM * instance.curve.Evaluate(progress); double nlng, nlat; OnlineMapsUtils.GetCoordinateInDistance(center.x, center.y, radius, 90, out nlng, out nlat); double tx1, ty1, tx2, ty2; OnlineMaps map = OnlineMaps.instance; // Convert the coordinate under cursor to tile position map.projection.CoordinatesToTile(center.x, center.y, 20, out tx1, out ty1); // Convert remote coordinate to tile position map.projection.CoordinatesToTile(nlng, nlat, 20, out tx2, out ty2); // Calculate radius in tiles double r = tx2 - tx1; int segments = points.Length; // Calculate a step double step = 360d / segments; double lng, lat; // Calculate each point of circle for (int i = 0; i < segments; i++) { double px = tx1 + Math.Cos(step * i * OnlineMapsUtils.Deg2Rad) * r; double py = ty1 + Math.Sin(step * i * OnlineMapsUtils.Deg2Rad) * r; map.projection.TileToCoordinates(px, py, 20, out lng, out lat); points[i] = new OnlineMapsVector2d(lng, lat); } return finished; } } } }