1

I am trying to detect when a certain amount of distance has been travelled in an automobile. For example, I am trying to call a function every 1 miles I am currently using NavigationMapViewDelegate but my delegate is not being called and I am not receiving any information in the console:

import MapboxMaps
import MapboxCoreNavigation
import MapboxNavigation
import MapboxDirections
import Turf
import SwiftUI
import UIKit
import MapboxSpeech
import AVFoundation
import MapboxSpeech
import Foundation
import CoreLocation

class MapViewController: UIViewController, NavigationMapViewDelegate {

    var mapView: NavigationMapView!
    var navigationViewController: NavigationViewController!
    var previousLocation: CLLocation?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        mapView = NavigationMapView(frame: view.bounds)
        mapView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
        view.addSubview(mapView)
        mapView.delegate = self
        mapView.userLocationStyle = .puck2D()
    }
    
       func navigationViewController(_ navigationViewController: NavigationViewController, didUpdate progress: RouteProgress, with location: CLLocation, rawLocation: CLLocation) {
           let previousLocation = self.previousLocation
               // Store the initial location as the previous location
               self.previousLocation = location
        
           // Calculate the distance between the previous location and the current location
           let distance = location.distance(from: previousLocation!)
           
           // Check if the distance is greater than or equal to 1 mile (1609.34 meters)
           if distance >= 1609.34  {
               // Do something when the user has moved at least one mile
               print("User has moved at least 1 mile")
               
               // Update the previous location to the current location
               self.previousLocation = location
           }
       }
cyo
  • 69
  • 1
  • 14

1 Answers1

0

Creating an extension solved my issue, I will post my solution and please feel free to suggest optimizations/refactoring of my code:

extension MapViewController: NavigationViewControllerDelegate {

func navigationViewController(_ navigationViewController: NavigationViewController, didUpdate progress: RouteProgress, with location: CLLocation, rawLocation: CLLocation) {



    // Check if initial eta has been set yet
           if initialEta == nil {
               // Set the initial eta value
               initialEta = Double(progress.durationRemaining).rounded(.towardZero)
               print("Initial estimated time of arrival: \(initialEta!) seconds")
           }
         
        let timeRemaining = progress.durationRemaining.rounded(.towardZero)
        if timeRemaining == initialEta! - 10.0 {
            print("You've been traveling for more than 10 seconds!")
        }
        
        if initialDistance == nil {
            // Set the initial eta value
            initialDistance = 
        Double(progress.distanceRemaining).rounded(.towardZero)
            print("Initial Distance: \(initialDistance!) ft")
        }
      
     let distanceRemaining = progress.distanceRemaining.rounded(.towardZero)
     if distanceRemaining == initialDistance! - 1.0 {
        
         print("You've been traveling for more than 1 meter!")
     }
        // Get the current step index and progress fraction
        let currentStepIndex = progress.currentLegProgress
        let currentStepProgress = progress.fractionTraveled
        let distanceProgress = progress.distanceTraveled
        
//        Print some information about the current step
        print("Initial ETA: \(initialEta)")
        print("Initial Dist: \(initialDistance)")
        print("Time Remaing: \(timeRemaining)")
        print("Travelled Progress: \(distanceProgress)")
        print("Current step index: \(currentStepIndex)")
        print("Current step progress: \(currentStepProgress)")
    }
cyo
  • 69
  • 1
  • 14