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