Manos Chorianopoulos

velocity fixes

......@@ -337,16 +337,42 @@ import CoreMotion
}
private func calculateSpeed(lat1: Double, lon1: Double, lat2: Double, lon2: Double, timeDifferenceInSeconds: Double) -> Double {
let distance = calculateDistance(lat1: lat1, lon1: lon1, lat2: lat2, lon2: lon2)
// let distance = calculateDistance(lat1: lat1, lon1: lon1, lat2: lat2, lon2: lon2)
let distance = distanceInKmBetweenEarthCoordinates(lat1: lat1, lon1: lon1, lat2: lat2, lon2: lon2)
// print("=== distance: ",distance)
return (distance / timeDifferenceInSeconds) * 3.6 // Convert to km/h
}
private func calculateDistance(lat1: Double, lon1: Double, lat2: Double, lon2: Double) -> Double {
let x = (lon2 - lon1).toRadians() * cos(((lat1 + lat2) / 2).toRadians())
let y = (lat2 - lat1).toRadians()
return sqrt(x * x + y * y) * EARTH_RADIUS
// MARK: distance implementation no1 ===>
// private func calculateDistance(lat1: Double, lon1: Double, lat2: Double, lon2: Double) -> Double {
// let x = (lon2 - lon1).toRadians() * cos(((lat1 + lat2) / 2).toRadians())
// let y = (lat2 - lat1).toRadians()
// return sqrt(x * x + y * y) * EARTH_RADIUS
// }
// <===
// MARK: distance implementation no2 ===>
func degreesToRadians(degrees: Double) -> Double {
return degrees * Double.pi / 180
}
func distanceInKmBetweenEarthCoordinates(lat1: Double, lon1: Double, lat2: Double, lon2: Double) -> Double {
let earthRadiusKm: Double = 6371
let dLat = degreesToRadians(degrees: lat2 - lat1)
let dLon = degreesToRadians(degrees: lon2 - lon1)
let lat1 = degreesToRadians(degrees: lat1)
let lat2 = degreesToRadians(degrees: lat2)
let a = sin(dLat/2) * sin(dLat/2) +
sin(dLon/2) * sin(dLon/2) * cos(lat1) * cos(lat2)
let c = 2 * atan2(sqrt(a), sqrt(1 - a))
return earthRadiusKm * c
}
// <===
// Orientation Observer
func setupOrientationChangeDetection() {
......@@ -510,6 +536,26 @@ import CoreMotion
mLatitude = location.coordinate.latitude
mLongitude = location.coordinate.longitude
}
// MARK: distance implementation no3 ===>
// Get the current location
// if let currentLocation = locationManager.location {
// // Get the speed in meters per second
// let speed = currentLocation.speed
// // Convert the speed to kilometers per hour
// let speedInKmPerHour = speed * 3.6
//
// if (speedInKmPerHour >= 0.0) {
// mSpeed = speedInKmPerHour
// } else {
// mSpeed = 0.0
// }
//
// avgVelocityLabel.text = String(format: "%.1f", floor(mSpeed)) + " km/h"
//
//// print("=== speedInKmPerHour: ",String(format: "%.1f", floor(speedInKmPerHour)) + " km/h")
// }
// <===
}
public func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
......