title: Google maps navigation (Swift)
body: <p>i'm a beginner and i have created this VC </p>
  
  <pre><code>import UIKit
  import CoreLocation
  import GoogleMaps
  import GooglePlaces
  
  class FinalClass: UIViewController {
  
      @IBOutlet weak var containerView: UIView!
      @IBOutlet weak var bottomInfoView: UIView!
      @IBOutlet weak var descriptionLabel: UILabel!
      @IBOutlet weak var distanceLabel: UILabel!
  
  
      var userLocation:CLLocationCoordinate2D?
      var places:[QPlace] = []
      var index:Int = -1
  
      var mapView:GMSMapView!
      var marker:GMSMarker?
  
      override func loadView() {
          super.loadView()
      }
  
      override func viewDidLoad() {
          super.viewDidLoad()
  
          guard index &gt;= 0, places.count &gt; 0 else {
              return
  
          }
  
          let place = places[index]
          let lat = place.location?.latitude ?? 1.310844
          let lng = place.location?.longitude ?? 103.866048
  
          // Google map view
          let camera = GMSCameraPosition.camera(withLatitude: lat, longitude: lng, zoom: 12.5)
          mapView = GMSMapView.map(withFrame: self.view.bounds, camera: camera)
          mapView.autoresizingMask = [.flexibleHeight, .flexibleWidth, .flexibleTopMargin, .flexibleBottomMargin, .flexibleLeftMargin, .flexibleRightMargin]
          self.containerView.addSubview(mapView)
  
          // Add gesture
          addSwipeGesture()
  
          didSelect(place: place)
          if userLocation != nil {
              addMarkerAtCurrentLocation(userLocation!)
          }
      }
  
      func addSwipeGesture() {
          let directions: [UISwipeGestureRecognizerDirection] = [.right, .left]
          for direction in directions {
              let gesture = UISwipeGestureRecognizer(target: self, action: #selector(handleSwipe(sender:)))
              gesture.direction = direction
              self.bottomInfoView.addGestureRecognizer(gesture)
          }
      }
  
      func addMarkerAtCurrentLocation(_ userLocation: CLLocationCoordinate2D)  {
          let marker = GMSMarker()
          marker.position = userLocation
          marker.title = "Your location"
          marker.map = mapView
      }
  
      func didSelect(place:QPlace) {
  
          guard let coordinates = place.location else {
              return
          }
  
          // clear current marker
          marker?.map = nil
  
          // add marker
          marker = GMSMarker()
          marker?.position = coordinates
          marker?.title = place.name
          marker?.map = mapView
          mapView.selectedMarker = marker
          moveToMarker(marker!)
  
          // update bottom info panel view
          let desc = place.getDescription()
          descriptionLabel.text = desc.characters.count &gt; 0 ? desc : "-"
          distanceLabel.text = "-"
  
          // update distance
          if userLocation != nil {
              let dist = distance(from: userLocation!, to: coordinates)
              distanceLabel.text = String.init(format: "Distance %.2f meters", dist)
          }
  
          title = place.name
      }
  
      func moveToMarker(_ marker: GMSMarker) {
          let camera = GMSCameraPosition.camera(withLatitude: marker.position.latitude,
                                                longitude: marker.position.longitude,
                                                zoom: 12.5)
          self.mapView.animate(to: camera)
      }
  
      // distance between two coordinates
      func distance(from: CLLocationCoordinate2D, to: CLLocationCoordinate2D) -&gt; CLLocationDistance {
          let from = CLLocation(latitude: from.latitude, longitude: from.longitude)
          let to = CLLocation(latitude: to.latitude, longitude: to.longitude)
          return from.distance(from: to)
      }
  
      func handleSwipe(sender: UISwipeGestureRecognizer) {
  
          guard index &gt;= 0, places.count &gt; 0 else {
              return
          }
  
          if sender.direction == .left {
              if index &lt; places.count - 2 {
                  index += 1
                  didSelect(place: places[index])
              }
          } else if sender.direction == .right {
              if index &gt; 1 {
                  index -= 1
                  didSelect(place: places[index])
              }
          }
      }
  
      override func didReceiveMemoryWarning() {
          super.didReceiveMemoryWarning()
          // Dispose of any resources that can be recreated.
      }
  
      @IBAction func lastBack(_ sender: Any) {
  
          dismiss(animated: true, completion: nil)
  
      }
  }
  </code></pre>
  
  <p>where (with the help of user localization in the previous VC) is shown the goole map with two markers, one on the user position and one on the position of the place searched in the previous VC (with also some info like distance ecc.) What i'm trying to do is to add the navigation between the two points, so i would like that when the view load, automatically the navigation start. The problem is i really do not know how to implement this feature, i looked for some tutorial but no one who fits well with my code.
  this is the code in the previous VC to find the user location</p>
  
  <pre><code>extension NearbyPlacesViewController: CLLocationManagerDelegate {
  
      func determineMyCurrentLocation() {
          locationManager = CLLocationManager()
          locationManager?.delegate = self
          locationManager?.desiredAccuracy = kCLLocationAccuracyBest
          locationManager?.requestWhenInUseAuthorization()
      }
  
      func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
          let userLocation:CLLocation = locations[0] as CLLocation
  
          manager.stopUpdatingLocation()
  
          print("user latitude = \(userLocation.coordinate.latitude)")
          print("user longitude = \(userLocation.coordinate.longitude)")
  
          didReceiveUserLocation(userLocation)
      }
  
      func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
          print("Error \(error)")
          errorGettingCurrentLocation(error.localizedDescription)
      }
  
      public func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
          if status == .authorizedWhenInUse || status == .authorizedAlways {
              locationManager?.startUpdatingLocation()
              //locationManager.startUpdatingHeading()
          } else if status == .denied || status == .restricted {
              errorGettingCurrentLocation("Location access denied")
          }
      }
  
      func errorGettingCurrentLocation(_ errorMessage:String) {
          let alert = UIAlertController.init(title: "Error", message: errorMessage, preferredStyle: .alert)
          alert.addAction(UIAlertAction.init(title: "Cancel", style: .cancel, handler: nil))
          present(alert, animated: true, completion: nil)
      }
  }
  </code></pre>
  
  <p>So the start point for navigation is the user location, the end is represented by <code>let coordinates = place.location</code></p>