/// <summary> /// Returns yScale for an area /// </summary> /// <param name="tlx">Left longitude</param> /// <param name="tly">Top latitude</param> /// <param name="brx">Right longitude</param> /// <param name="bry">Bottom latitude</param> /// <returns>yScale for an area</returns> public static float GetBestElevationYScale(double tlx, double tly, double brx, double bry) { if (_instance != null && _instance.lockYScale) { return(_instance.yScaleValue); } double dx, dy; OnlineMaps map = _instance != null ? _instance.map : OnlineMaps.instance; if (map == null) { return(0); } OnlineMapsControlBaseDynamicMesh control = map.control as OnlineMapsControlBaseDynamicMesh; if (control == null) { return(0); } OnlineMapsUtils.DistanceBetweenPoints(tlx, tly, brx, bry, out dx, out dy); dx = dx / control.sizeInScene.x * map.width; dy = dy / control.sizeInScene.y * map.height; return((float)Math.Min(map.width / dx, map.height / dy) / 1000); }
private void OnGUI() { if (GUI.Button(new Rect(5, 5, 100, 30), "Calc")) { // Calculate the distance in km between locations. float distance = OnlineMapsUtils.DistanceBetweenPoints(userCoordinares, markerCoordinates).magnitude; Debug.Log("Distance: " + distance); int zoom = 15; int maxX = 1 << (zoom - 1); // Calculate the tile position of locations. double userTileX, userTileY, markerTileX, markerTileY; OnlineMaps.instance.projection.CoordinatesToTile(userCoordinares.x, userCoordinares.y, zoom, out userTileX, out userTileY); OnlineMaps.instance.projection.CoordinatesToTile(markerCoordinates.x, markerCoordinates.y, zoom, out markerTileX, out markerTileY); // Calculate the angle between locations. double angle = OnlineMapsUtils.Angle2D(userTileX, userTileY, markerTileX, markerTileY); if (Math.Abs(userTileX - markerTileX) > maxX) { angle = 360 - angle; } Debug.Log("Angle: " + angle); // Calculate relative angle between locations. double relativeAngle = angle - compassTrueHeading; Debug.Log("Relative angle: " + relativeAngle); } }
/// <summary> /// Returns best value yScale, based on the coordinates corners of map. /// </summary> /// <param name="tlx">Top-left longitude.</param> /// <param name="tly">Top-left latitude.</param> /// <param name="brx">Bottom-right longitude.</param> /// <param name="bry">Bottom-right latitude.</param> /// <returns></returns> public virtual float GetBestElevationYScale(double tlx, double tly, double brx, double bry) { double dx, dy; OnlineMapsUtils.DistanceBetweenPoints(tlx, tly, brx, bry, out dx, out dy); return((float)Math.Min(map.width / dx, map.height / dy) / 1000); }
private void OnGUI() { if (GUI.Button(new Rect(5, 5, 100, 30), "Calc")) { // Calculate the distance in km between locations. float distance = OnlineMapsUtils.DistanceBetweenPoints(userCoordinares, markerCoordinates).magnitude; Debug.Log("Distance: " + distance); int zoom = 15; int maxX = 1 << zoom; // Calculate the tile position of locations. Vector2 userTile = OnlineMapsUtils.LatLongToTilef(userCoordinares, zoom); Vector2 markerTile = OnlineMapsUtils.LatLongToTilef(markerCoordinates, zoom); // Calculate the angle between locations. float angle = OnlineMapsUtils.Angle2D(userTile, markerTile); if (Mathf.Abs(userTile.x - markerTile.x) > maxX / 2) { angle = 360 - angle; } Debug.Log("Angle: " + angle); // Calculate relative angle between locations. float relativeAngle = angle - compassTrueHeading; Debug.Log("Relative angle: " + relativeAngle); } }
private void Update() { if (pointIndex == -1) { return; } // Start point Vector3 p1 = points[pointIndex]; // End point Vector3 p2 = points[pointIndex + 1]; // Total step distance float stepDistance = OnlineMapsUtils.DistanceBetweenPoints(p1, p2).magnitude; // Total step time float totalTime = stepDistance / speed * 3600; // Current step progress progress += Time.deltaTime / totalTime; if (progress < 1) { marker.position = Vector2.Lerp(p1, p2, progress); // Orient marker if (orientMarkerOnNextPoint) { marker.rotation = 1.25f - OnlineMapsUtils.Angle2D((Vector2)p1, (Vector2)p2) / 360f; } } else { marker.position = p2; pointIndex++; progress = 0; if (pointIndex >= points.Count - 1) { Debug.Log("Finish"); pointIndex = -1; } else { // Orient marker if (orientMarkerOnNextPoint) { marker.rotation = 1.25f - OnlineMapsUtils.Angle2D((Vector2)p2, (Vector2)points[pointIndex + 1]) / 360; } } } if (lookToMarker) { OnlineMaps.instance.position = marker.position; } OnlineMaps.instance.Redraw(); }
private void Update() { if (!hasTargetPoint) { return; } double dx, dy; OnlineMapsUtils.DistanceBetweenPoints(lng, lat, targetLng, targetLat, out dx, out dy); double distance = Math.Sqrt(dx * dx + dy * dy); float cMaxSpeed = maxSpeed; if (distance < 0.1) { cMaxSpeed = maxSpeed * (float)(distance / 0.1); } speed = Mathf.Lerp(speed, cMaxSpeed, Time.deltaTime); OnlineMapsUtils.GetCoordinateInDistance(lng, lat, speed * Time.deltaTime / 3600, rotation + 180, out lng, out lat); OnlineMapsUtils.DistanceBetweenPoints(lng, lat, targetLng, targetLat, out dx, out dy); if (Math.Sqrt(dx * dx + dy * dy) < 0.001) { hasTargetPoint = false; speed = 0; } marker.rotationY = rotation; marker.SetPosition(lng, lat); if (centerOnMarker) { map.SetPosition(lng, lat); } if (lineRendererProgress < 1) { lineRendererProgress += Time.deltaTime / lineRendererDuration; Vector3 p1 = control.GetWorldPosition(lng, lat); Vector3 p2 = control.GetWorldPosition(targetLng, targetLat); Vector3 p3 = lineRendererProgress > 0.5 ? Vector3.Lerp(p1, p2, (lineRendererProgress - 0.5f) * 2f) : p1; Vector3 p4 = lineRendererProgress < 0.5 ? Vector3.Lerp(p1, p2, lineRendererProgress * 2) : p2; lineRenderer.SetPosition(0, p4); lineRenderer.SetPosition(1, p3); } else { lineRendererProgress = 1; if (lineRenderer.enabled) { lineRenderer.enabled = false; } } }
private double CalculateHeight(Transform transform) { var selfY = transform.localPosition.y; var point1 = OnlineMapsTileSetControl.instance.GetCoordsByWorldPosition(transform.TransformPoint(transform.position)); var point2 = OnlineMapsTileSetControl.instance.GetCoordsByWorldPosition(transform.TransformPoint(transform.position + Vector3.forward)); var planeUnit = OnlineMapsUtils.DistanceBetweenPoints(point1, point2).magnitude; return((selfY / planeUnit + OnlineMapsTileSetControl.instance.elevationMinValue) / 100); }
private void UpdateSize() { // Get distance (km) between corners of map Vector2 distance = OnlineMapsUtils.DistanceBetweenPoints(OnlineMaps.instance.topLeftPosition, OnlineMaps.instance.bottomRightPosition); // Set tileset size OnlineMapsControlBaseDynamicMesh.instance.sizeInScene = distance * 1000; // Redraw map OnlineMaps.instance.Redraw(); }
private void UpdateARButton() { if (gameObject.activeInHierarchy) { Vector2 player = MapManager.Instance.location.position; Vector2 location = new Vector2((float)selectedLocation.Longitude, (float)selectedLocation.Latitude); float distance = OnlineMapsUtils.DistanceBetweenPoints(player, location).magnitude * 1000; FarAwayText.gameObject.SetActive(false); ARMode.gameObject.SetActive(true); } }
public void EnterNewsARMode(Vector2 currentPosition) { foreach (Newsarticle article in articles) { float distance = OnlineMapsUtils.DistanceBetweenPoints(MapManager.Instance.location.position, new Vector2((float)article.Longitude, (float)article.Latitude)).magnitude * 1000; if (distance < 100) { ARNewsArticle arArticle = Instantiate(ArNewsArticlePrefab).GetComponent <ARNewsArticle>(); arArticle.SetArticle(article); } } }
public override void UpdateSpeed() { if (Input.location.status != LocationServiceStatus.Running) { return; } LocationInfo lastData = Input.location.lastData; if (Math.Abs(lastLocationInfoTimestamp - lastData.timestamp) < double.Epsilon) { return; } float longitude = lastData.longitude; float latitude = lastData.latitude; if (OnGetLocation != null) { OnGetLocation(out longitude, out latitude); } lastLocationInfoTimestamp = lastData.timestamp; if (lastPositions == null) { lastPositions = new List <LastPositionItem>(); } lastPositions.Add(new LastPositionItem(longitude, latitude, lastData.timestamp)); while (lastPositions.Count > maxPositionCount) { lastPositions.RemoveAt(0); } if (lastPositions.Count < 2) { _speed = 0; return; } LastPositionItem p1 = lastPositions[0]; LastPositionItem p2 = lastPositions[lastPositions.Count - 1]; double dx, dy; OnlineMapsUtils.DistanceBetweenPoints(p1.lng, p1.lat, p2.lng, p2.lat, out dx, out dy); double distance = Math.Sqrt(dx * dx + dy * dy); double time = (p2.timestamp - p1.timestamp) / 3600; _speed = Mathf.Abs((float)(distance / time)); }
void Update() { if (!triggered) { Vector2 player = MapManager.Instance.location.position; Vector2 location = new Vector2((float)myLocation.Longitude, (float)myLocation.Latitude); float distance = OnlineMapsUtils.DistanceBetweenPoints(player, location).magnitude * 1000; if (distance < myLocation.TriggerDistance) { Trigger(); } } }
private void MoveMap(Vector3 direction) { double px, pz, tlx, tly, brx, bry, dx, dy; OnlineMaps.instance.GetPosition(out px, out pz); OnlineMaps.instance.GetTopLeftPosition(out tlx, out tly); OnlineMaps.instance.GetBottomRightPosition(out brx, out bry); OnlineMapsUtils.DistanceBetweenPoints(tlx, tly, brx, bry, out dx, out dy); double mx = (brx - tlx) / dx; double my = (tly - bry) / dy; double ox = MovementFactor * mx * direction.x * Time.deltaTime; double oy = MovementFactor * my * direction.z * Time.deltaTime; px += ox; pz += oy; OnlineMaps.instance.SetPosition(px, pz); }
public static float GetBestElevationYScale(double tlx, double tly, double brx, double bry) { if (_instance != null) { if (_instance.lockYScale) { return(_instance.yScaleValue); } } else if (control == null) { return(0); } double dx, dy; OnlineMapsUtils.DistanceBetweenPoints(tlx, tly, brx, bry, out dx, out dy); dx = dx / sizeInScene.x * map.width; dy = dy / sizeInScene.y * map.height; return((float)Math.Min(map.width / dx, map.height / dy) / 1000); }
private void UpdateSpeed() { LocationInfo lastData = Input.location.lastData; if (lastLocationInfoTimestamp == lastData.timestamp) { return; } lastLocationInfoTimestamp = lastData.timestamp; if (lastPositions == null) { lastPositions = new List <LastPositionItem>(); } lastPositions.Add(new LastPositionItem(lastData.longitude, lastData.latitude, lastData.timestamp)); while (lastPositions.Count > maxPositionCount) { lastPositions.RemoveAt(0); } if (lastPositions.Count < 2) { _speed = 0; return; } LastPositionItem p1 = lastPositions[0]; LastPositionItem p2 = lastPositions[lastPositions.Count - 1]; double dx, dy; OnlineMapsUtils.DistanceBetweenPoints(p1.lng, p1.lat, p2.lng, p2.lat, out dx, out dy); double distance = Math.Sqrt(dx * dx + dy * dy); double time = (p2.timestamp - p1.timestamp) / 3600; _speed = Mathf.Abs((float)(distance / time)); }
private void Start() { map = OnlineMaps.instance; if (points == null) { points = new List <Point>(); } if (points.Count == 0) { points.Add(new Point(OnlineMaps.instance.position)); } Point p1 = points[0]; Point p2 = points[1]; double p1x, p1y, p2x, p2y; map.projection.CoordinatesToTile(p1.longitude, p1.latitude, map.zoom, out p1x, out p1y); map.projection.CoordinatesToTile(p2.longitude, p2.latitude, map.zoom, out p2x, out p2y); targetRotation = (float)OnlineMapsUtils.Angle2D(p1x, p1y, p2x, p2y) - 90; transform.rotation = Quaternion.Euler(0, targetRotation, 0); distances = new List <double>(); totalDistance = 0; for (int i = 1; i < points.Count; i++) { p1 = points[i - 1]; p2 = points[i]; double dx, dy; OnlineMapsUtils.DistanceBetweenPoints(p1.longitude, p1.latitude, p2.longitude, p2.latitude, out dx, out dy); double stepDistance = Math.Sqrt(dx * dx + dy * dy); distances.Add(stepDistance); totalDistance += stepDistance; } }
// When the location has changed private void UpdateClosestStores(Vector2 gpsCoordinates) { // Sort stores by distance List <MeridianData.Store> distanceSortedStores = new List <MeridianData.Store>(stores.storeList); Debug.Log(distanceSortedStores.Count); distanceSortedStores.Sort(delegate(MeridianData.Store s1, MeridianData.Store s2) { return(Vector2.Distance(gpsCoordinates, s1.coordinates).CompareTo ((Vector2.Distance(gpsCoordinates, s2.coordinates)))); }); for (int i = 0; i < 3; i++) { Vector2 markerCoordinates = distanceSortedStores[i].coordinates; float distance = OnlineMapsUtils.DistanceBetweenPoints(gpsCoordinates, markerCoordinates).magnitude; closestStoreNames[i].text = distanceSortedStores[i].Tienda; closestStoreDistances[i].text = string.Format("{0:0.00} km", distance); } // Redraw map. OnlineMaps.instance.Redraw(); }
void OnDistanceChange(Vector2 userPoint) { if (currentData == null) { return; } Vector2 markerCoordinates = new Vector2((float)currentData.Longitude_User, (float)currentData.Latitude_User); Vector2 userCoordinares = userPoint; // Calculate the distance in km between locations. distanceBetweenPOI = OnlineMapsUtils.DistanceBetweenPoints(userCoordinares, markerCoordinates).magnitude * 1000; if (distanceBetweenPOI > 1000) { GoadRange.text = string.Format("距離 {0} km", (distanceBetweenPOI / 1000).ToString("0.00")); } else { GoadRange.text = string.Format("距離 {0} m", distanceBetweenPOI.ToString("0.0")); } //GoadRange.text = "距離 " + distanceBetweenPOI.ToString("0.0") + " m"; }
private void Update() { if (pointIndex == -1) { return; } // Start point OnlineMapsVector2d p1 = points[pointIndex]; // End point OnlineMapsVector2d p2 = points[pointIndex + 1]; // Total step distance double dx, dy; OnlineMapsUtils.DistanceBetweenPoints(p1.x, p1.y, p2.x, p2.y, out dx, out dy); double stepDistance = Math.Sqrt(dx * dx + dy * dy); // Total step time double totalTime = stepDistance / speed * 3600; // Current step progress progress += Time.deltaTime / totalTime; OnlineMapsVector2d position; if (progress < 1) { position = OnlineMapsVector2d.Lerp(p1, p2, progress); marker.SetPosition(position.x, position.y); // Orient marker if (orientMarkerOnNextPoint) { marker.rotation = 1.25f - OnlineMapsUtils.Angle2D((Vector2)p1, (Vector2)p2) / 360f; } } else { position = p2; marker.SetPosition(position.x, position.y); pointIndex++; progress = 0; if (pointIndex >= points.Length - 1) { Debug.Log("Finish"); pointIndex = -1; } else { // Orient marker if (orientMarkerOnNextPoint) { marker.rotation = 1.25f - OnlineMapsUtils.Angle2D(p2, points[pointIndex + 1]) / 360; } } } if (lookToMarker) { OnlineMaps.instance.SetPosition(position.x, position.y); } OnlineMaps.instance.Redraw(); }
/// <summary> /// Updates marker instance. /// </summary> /// <param name="map">Reference to the map</param> /// <param name="control">Reference to the control</param> /// <param name="bounds">Bounds of the map mesh</param> /// <param name="tlx">Longitude of top-left corner of the map</param> /// <param name="tly">Latitude of top-left corner of the map</param> /// <param name="brx">Longitude of bottom-right corner of the map</param> /// <param name="bry">Latitude of bottom-right corner of the map</param> /// <param name="zoom">Zoom of the map</param> /// <param name="ttlx">Tile X of top-left corner of the map</param> /// <param name="ttly">Tile Y of top-left corner of the map</param> /// <param name="tbrx">Tile X of bottom-right corner of the map</param> /// <param name="tbry">Tile Y of bottom-right corner of the map</param> /// <param name="bestYScale">Best y scale for current map view</param> public void Update(OnlineMaps map, OnlineMapsControlBase3D control, Bounds bounds, double tlx, double tly, double brx, double bry, int zoom, double ttlx, double ttly, double tbrx, double tbry, float bestYScale) { if (!enabled) { return; } if (instance == null) { Init(map.transform); } if (!range.InRange(zoom)) { visible = false; } else if (OnCheckMapBoundaries != null) { visible = OnCheckMapBoundaries(); } else if (checkMapBoundaries) { if (latitude > tly || latitude < bry) { visible = false; } else if (tlx < brx && (longitude < tlx || longitude > brx)) { visible = false; } else if (tlx > brx && longitude < tlx && longitude > brx) { visible = false; } else { visible = true; } } else { visible = true; } if (!visible) { return; } if (_prefab != prefab) { Reinit(tlx, tly, brx, bry, zoom); } double mx, my; map.projection.CoordinatesToTile(longitude, latitude, zoom, out mx, out my); int maxX = 1 << zoom; double sx = tbrx - ttlx; double mpx = mx - ttlx; if (sx < 0) { sx += maxX; } if (checkMapBoundaries) { if (mpx < 0) { mpx += maxX; } else if (mpx > maxX) { mpx -= maxX; } } else { double dx1 = Math.Abs(mpx - ttlx); double dx2 = Math.Abs(mpx - tbrx); double dx3 = Math.Abs(mpx - tbrx + maxX); if (dx1 > dx2 && dx1 > dx3) { mpx += maxX; } } double px = mpx / sx; double pz = (ttly - my) / (ttly - tbry); _relativePosition = new Vector3((float)px, 0, (float)pz); OnlineMapsTileSetControl tsControl = control as OnlineMapsTileSetControl; if (tsControl != null) { px = -tsControl.sizeInScene.x / 2 - (px - 0.5) * tsControl.sizeInScene.x; pz = tsControl.sizeInScene.y / 2 + (pz - 0.5) * tsControl.sizeInScene.y; } else { Vector3 center = bounds.center; Vector3 size = bounds.size; px = center.x - (px - 0.5) * size.x / map.transform.lossyScale.x - map.transform.position.x; pz = center.z + (pz - 0.5) * size.z / map.transform.lossyScale.z - map.transform.position.z; } Vector3 oldPosition = instance.transform.localPosition; float y = 0; if (altitude.HasValue) { float yScale = OnlineMapsElevationManagerBase.GetBestElevationYScale(tlx, tly, brx, bry); y = altitude.Value; if (altitudeType == OnlineMapsAltitudeType.relative && tsControl != null) { y += OnlineMapsElevationManagerBase.GetUnscaledElevation(px, pz, tlx, tly, brx, bry); } y *= yScale; if (tsControl != null) { if (OnlineMapsElevationManagerBase.instance.bottomMode == OnlineMapsElevationBottomMode.minValue) { y -= OnlineMapsElevationManagerBase.instance.minValue * bestYScale; } y *= OnlineMapsElevationManagerBase.instance.scale; } } else if (tsControl != null) { y = OnlineMapsElevationManagerBase.GetElevation(px, pz, bestYScale, tlx, tly, brx, bry); } Vector3 newPosition = new Vector3((float)px, y, (float)pz); if (sizeType == SizeType.meters) { double dx, dy; OnlineMapsUtils.DistanceBetweenPoints(tlx, tly, brx, bry, out dx, out dy); dx = tsControl.sizeInScene.x / dx / 1000; dy = tsControl.sizeInScene.y / dy / 1000; double d = (dx + dy) / 2 * scale; float fd = (float)d; instance.transform.localScale = new Vector3(fd, fd, fd); } if (oldPosition != newPosition) { instance.transform.localPosition = newPosition; } }
void Update() { const float maxTilt = 50; OnlineMaps api = OnlineMaps.instance; OnlineMapsTileSetControl control = OnlineMapsTileSetControl.instance; if (Input.GetKey(KeyCode.LeftArrow) || Input.GetKey(KeyCode.A)) { tilt -= Time.deltaTime * tiltSpeed * maxTilt; } else if (Input.GetKey(KeyCode.RightArrow) || Input.GetKey(KeyCode.D)) { tilt += Time.deltaTime * tiltSpeed * maxTilt; } else if (tilt != 0) { float tiltOffset = Time.deltaTime * tiltSpeed * maxTilt; if (Mathf.Abs(tilt) > tiltOffset) { tilt -= tiltOffset * Mathf.Sign(tilt); } else { tilt = 0; } } tilt = Mathf.Clamp(tilt, -maxTilt, maxTilt); container.transform.localRotation = Quaternion.Euler(tilt, 0, 0); if (tilt != 0) { transform.Rotate(Vector3.up, tilt * rotateSpeed * Time.deltaTime); } double tlx, tly, brx, bry, dx, dy; api.GetTopLeftPosition(out tlx, out tly); api.GetBottomRightPosition(out brx, out bry); OnlineMapsUtils.DistanceBetweenPoints(tlx, tly, brx, bry, out dx, out dy); double mx = (brx - tlx) / dx; double my = (tly - bry) / dy; double v = (double)speed * Time.deltaTime / 3600.0; double ox = mx * v * Math.Cos(transform.rotation.eulerAngles.y * OnlineMapsUtils.Deg2Rad); double oy = my * v * Math.Sin((360 - transform.rotation.eulerAngles.y) * OnlineMapsUtils.Deg2Rad); px += ox; py += oy; api.SetPosition(px, py); Vector3 pos = transform.position; pos.y = altitude * control.GetBestElevationYScale(tlx, tly, brx, bry) * control.elevationScale; transform.position = pos; Camera.main.transform.position = transform.position - transform.rotation * cameraOffset; Camera.main.transform.LookAt(transform); }
private void Update() { if (pointIndex == -1) { return; } // Start point OnlineMapsVector2d p1 = points[pointIndex]; // End point OnlineMapsVector2d p2 = points[pointIndex + 1]; double p1x, p1y, p2x, p2y; map.projection.CoordinatesToTile(p1.x, p1.y, map.zoom, out p1x, out p1y); map.projection.CoordinatesToTile(p2.x, p2.y, map.zoom, out p2x, out p2y); // Total step distance double dx, dy; OnlineMapsUtils.DistanceBetweenPoints(p1.x, p1.y, p2.x, p2.y, out dx, out dy); double stepDistance = Math.Sqrt(dx * dx + dy * dy); // Total step time double totalTime = stepDistance / speed * 3600; // Current step progress progress += Time.deltaTime / totalTime; OnlineMapsVector2d position; if (progress < 1) { position = OnlineMapsVector2d.Lerp(p1, p2, progress); marker.SetPosition(position.x, position.y); // Orient marker targetRotation = (float)OnlineMapsUtils.Angle2D(p1x, p1y, p2x, p2y) - 90; } else { position = p2; marker.SetPosition(position.x, position.y); pointIndex++; progress = 0; if (pointIndex >= points.Length - 1) { Debug.Log("Finish"); pointIndex = -1; } else { OnlineMapsVector2d p3 = points[pointIndex + 1]; map.projection.CoordinatesToTile(p2.x, p2.y, map.zoom, out p1x, out p1y); map.projection.CoordinatesToTile(p3.x, p3.y, map.zoom, out p2x, out p2y); targetRotation = (float)OnlineMapsUtils.Angle2D(p1x, p1y, p2x, p2y) - 90; } } marker.rotationY = Mathf.LerpAngle(marker.rotationY, targetRotation, Time.deltaTime * 10); marker.GetPosition(out lng, out lat); map.SetPosition(lng, lat); }
private void Update() { if (_borderWeight != borderWeight) { _borderWeight = borderWeight; if (polygon != null) { polygon.borderWeight = borderWeight; map.Redraw(); } } // Check the position of the markers. CheckMarkerPositions(); // If nothing happens, then return. if (!changed) { return; } changed = false; // If the number of points is less than 3, then return. if (markers.Count < 3) { map.Redraw(); return; } // If the polygon is not created, then create. if (polygon == null) { // For points, reference to markerPositions. // If you change the values in markerPositions, value in the polygon will be adjusted automatically. polygon = new OnlineMapsDrawingPoly(markerPositions, Color.black, borderWeight, new Color(1, 1, 1, 0.3f)); // Add an element to the map. map.AddDrawingElement(polygon); } // Calculates area of the polygon. // Important: this algorithm works correctly only if the lines do not intersect. float area = 0; // Triangulate points. int[] indexes = OnlineMapsUtils.Triangulate(markerPositions).ToArray(); // Calculate the area of each triangle. for (int i = 0; i < indexes.Length / 3; i++) { // Get the points of the triangle. Vector2 p1 = markerPositions[indexes[i * 3]]; Vector2 p2 = markerPositions[indexes[i * 3 + 1]]; Vector2 p3 = markerPositions[indexes[i * 3 + 2]]; // Calculate the distance between points. float d1 = OnlineMapsUtils.DistanceBetweenPoints(p1, p2).magnitude; float d2 = OnlineMapsUtils.DistanceBetweenPoints(p2, p3).magnitude; float d3 = OnlineMapsUtils.DistanceBetweenPoints(p3, p1).magnitude; // Calculate the area. float p = (d1 + d2 + d3) / 2; area += Mathf.Sqrt(p * (p - d1) * (p - d2) * (p - d2)); } Debug.Log("Area: " + area + " km^2"); map.Redraw(); }