Mapbox Basic Navigation App: How to set route origin to User's Location?

240 views Asked by At

I got some Code from the Mapbox Tutorials for a basic Navigation App in a Turn -by- Turn View. Everything is working fine, and I've already added a Waypoint.

In the example the origin of the route is set with fixed coordinates. That's not compatible with my Use Case. The Waypoints and the Destination are also fixed by coordinates, which is good. But the origin has to be the "Location of the User", which is obviously variable.

Maybe someone could help me, would be much appreciated. :)


import Foundation
import UIKit
import MapboxCoreNavigation
import MapboxNavigation
import MapboxDirections

class PlacemarktestViewController: UIViewController {
    override func viewDidLoad() {
    super.viewDidLoad()
     
    let origin = CLLocationCoordinate2DMake(37.77440680146262, -122.43539772352648)

    let waypoint = CLLocationCoordinate2DMake(27.76556957793795, -112.42409811526268)

    let destination = CLLocationCoordinate2DMake(37.76556957793795, -122.42409811526268)
***strong text***
    let options = NavigationRouteOptions(coordinates: [origin, waypoint, destination])
     
     
        
        
        
    Directions.shared.calculate(options) { [weak self] (session, result) in
    switch result {
    case .failure(let error):
    print(error.localizedDescription)
    case .success(let response):
    guard let route = response.routes?.first, let strongSelf = self else {
    return
    }
     
    // For demonstration purposes, simulate locations if the Simulate Navigation option is on.
    // Since first route is retrieved from response `routeIndex` is set to 0.
    let navigationService = MapboxNavigationService(route: route, routeIndex: 0, routeOptions: options)
    let navigationOptions = NavigationOptions(navigationService: navigationService)
    let navigationViewController = NavigationViewController(for: route, routeIndex: 0, routeOptions: options, navigationOptions: navigationOptions)
    navigationViewController.modalPresentationStyle = .fullScreen
    // Render part of the route that has been traversed with full transparency, to give the illusion of a disappearing route.
    navigationViewController.routeLineTracksTraversal = true
     
    strongSelf.present(navigationViewController, animated: true, completion: nil)
    }
    }
    }
    }

1

There are 1 answers

0
Bogdan Belogurov On

@zeno, to set user location you can use

mapView.setUserTrackingMode(.follow, animated: true)

don't forget to add NSLocationWhenInUseUsageDescription key to info plist.

Or you can get coordinates and set them manually using CoreLocation

import CoreLocation

let locationManager = CLLocationManager()
locationManager.delegate = self

locationManager.requestLocation() // Request a user’s location

Documentation is here requestLocation

And then implement the CLLocationManagerDelegate method locationManager(:, didUpdateLocations:) to handle the requested user location. This method will be called once after using locationManager.requestLocation().

func locationManager(
    _ manager: CLLocationManager, 
    didUpdateLocations locations: [CLLocation]
) {
    if let location = locations.first {
        let latitude = location.coordinate.latitude
        let longitude = location.coordinate.longitude

    }
}