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)); }
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)); }