Manos Chorianopoulos

velocity fixes

...@@ -337,16 +337,42 @@ import CoreMotion ...@@ -337,16 +337,42 @@ import CoreMotion
337 } 337 }
338 338
339 private func calculateSpeed(lat1: Double, lon1: Double, lat2: Double, lon2: Double, timeDifferenceInSeconds: Double) -> Double { 339 private func calculateSpeed(lat1: Double, lon1: Double, lat2: Double, lon2: Double, timeDifferenceInSeconds: Double) -> Double {
340 - let distance = calculateDistance(lat1: lat1, lon1: lon1, lat2: lat2, lon2: lon2) 340 +// let distance = calculateDistance(lat1: lat1, lon1: lon1, lat2: lat2, lon2: lon2)
341 + let distance = distanceInKmBetweenEarthCoordinates(lat1: lat1, lon1: lon1, lat2: lat2, lon2: lon2)
341 // print("=== distance: ",distance) 342 // print("=== distance: ",distance)
343 +
342 return (distance / timeDifferenceInSeconds) * 3.6 // Convert to km/h 344 return (distance / timeDifferenceInSeconds) * 3.6 // Convert to km/h
343 } 345 }
344 346
345 - private func calculateDistance(lat1: Double, lon1: Double, lat2: Double, lon2: Double) -> Double { 347 + // MARK: distance implementation no1 ===>
346 - let x = (lon2 - lon1).toRadians() * cos(((lat1 + lat2) / 2).toRadians()) 348 +// private func calculateDistance(lat1: Double, lon1: Double, lat2: Double, lon2: Double) -> Double {
347 - let y = (lat2 - lat1).toRadians() 349 +// let x = (lon2 - lon1).toRadians() * cos(((lat1 + lat2) / 2).toRadians())
348 - return sqrt(x * x + y * y) * EARTH_RADIUS 350 +// let y = (lat2 - lat1).toRadians()
351 +// return sqrt(x * x + y * y) * EARTH_RADIUS
352 +// }
353 + // <===
354 +
355 + // MARK: distance implementation no2 ===>
356 + func degreesToRadians(degrees: Double) -> Double {
357 + return degrees * Double.pi / 180
358 + }
359 +
360 + func distanceInKmBetweenEarthCoordinates(lat1: Double, lon1: Double, lat2: Double, lon2: Double) -> Double {
361 +
362 + let earthRadiusKm: Double = 6371
363 +
364 + let dLat = degreesToRadians(degrees: lat2 - lat1)
365 + let dLon = degreesToRadians(degrees: lon2 - lon1)
366 +
367 + let lat1 = degreesToRadians(degrees: lat1)
368 + let lat2 = degreesToRadians(degrees: lat2)
369 +
370 + let a = sin(dLat/2) * sin(dLat/2) +
371 + sin(dLon/2) * sin(dLon/2) * cos(lat1) * cos(lat2)
372 + let c = 2 * atan2(sqrt(a), sqrt(1 - a))
373 + return earthRadiusKm * c
349 } 374 }
375 + // <===
350 376
351 // Orientation Observer 377 // Orientation Observer
352 func setupOrientationChangeDetection() { 378 func setupOrientationChangeDetection() {
...@@ -510,6 +536,26 @@ import CoreMotion ...@@ -510,6 +536,26 @@ import CoreMotion
510 mLatitude = location.coordinate.latitude 536 mLatitude = location.coordinate.latitude
511 mLongitude = location.coordinate.longitude 537 mLongitude = location.coordinate.longitude
512 } 538 }
539 +
540 + // MARK: distance implementation no3 ===>
541 + // Get the current location
542 +// if let currentLocation = locationManager.location {
543 +// // Get the speed in meters per second
544 +// let speed = currentLocation.speed
545 +// // Convert the speed to kilometers per hour
546 +// let speedInKmPerHour = speed * 3.6
547 +//
548 +// if (speedInKmPerHour >= 0.0) {
549 +// mSpeed = speedInKmPerHour
550 +// } else {
551 +// mSpeed = 0.0
552 +// }
553 +//
554 +// avgVelocityLabel.text = String(format: "%.1f", floor(mSpeed)) + " km/h"
555 +//
556 +//// print("=== speedInKmPerHour: ",String(format: "%.1f", floor(speedInKmPerHour)) + " km/h")
557 +// }
558 + // <===
513 } 559 }
514 560
515 public func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) { 561 public func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
......