Ios 在Swift中点击didSelectRow时未获取路由器方向?

Ios 在Swift中点击didSelectRow时未获取路由器方向?,ios,swift,mapkit,Ios,Swift,Mapkit,我正在创建一个应用程序,其中有一个detailViewController,其中有一个地址,当用户单击该地址时,它会打开我想要的苹果地图,但不会显示该路由器作为前往该地址的目的地。我的电话号码是:-118.277。。。。从我的位置上看,除了to部分之外,这是一个很好的位置&它还告诉我们没有的方向。所以,我想要的是当点击时,按下didSelectRow以在apple maps中显示目的地方向的地址。谢谢你的帮助。 更新我刚刚调试并创建了一个place.location?的手表。坐标我看到了一些结果

我正在创建一个应用程序,其中有一个detailViewController,其中有一个地址,当用户单击该地址时,它会打开我想要的苹果地图,但不会显示该路由器作为前往该地址的目的地。我的电话号码是:-118.277。。。。从我的位置上看,除了to部分之外,这是一个很好的位置&它还告诉我们没有的方向。所以,我想要的是当点击时,按下didSelectRow以在apple maps中显示目的地方向的地址。谢谢你的帮助。 更新我刚刚调试并创建了一个place.location?的手表。坐标我看到了一些结果,但它们只是经度和纬度。但是,我想显示地址,而不是lat&long

这是我的密码:

import UIKit
import Social
import CoreLocation

class DetailsViewController: BaseViewController, UITableViewDelegate, UITableViewDataSource{

  @IBOutlet weak var tableView: UITableView!

  var nearMeIndexSelected = NearMeIndexTitle()

  var place: Place!

  var coordinate :CLLocationCoordinate2D?

  var nearMeRequest : NearMeRequest!

  var locationManager = CLLocationManager()

  let infoCellId = "info-cell"
  let interactiveCellId = "interactive-cell"
  let directionsSegueId = "directions-segue"
  let gallerySegueId = "gallery-segue"
  @IBOutlet weak var activityIndicator: UIActivityIndicatorView!
  var annotation: ARAnnotation!

  override func viewDidLoad() {
    super.viewDidLoad()

  if self.place != nil {
      self.title = place.placeName


      self.tableView.delegate = self
      self.tableView.dataSource = self
      self.loadDetailInfo()
      print("Place is not nil")
    } else {
      print("Place is nil")
    }

  }

  override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
  }

  func numberOfSections(in tableView: UITableView) -> Int {
    return 4
  }

  func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if section == 0 {
      return 4
    }
    else if section == 1 {
      return 0
    }
    else if section == 2  {
      return 1

    }
    else {
      return 1
    }

  }

  func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    var cellId = infoCellId
    if indexPath.section != 0 {
      cellId = interactiveCellId
    }

    let cell: UITableViewCell = self.tableView.dequeueReusableCell(withIdentifier: cellId)! as UITableViewCell
    var key = "Not Available"
    var value = "Not Available"

    if indexPath.section == 0  {
      if indexPath.row == 0 {
        key = "Name"
        if self.place.placeName.characters.count > 0 {
          value = self.place.placeName
        }
      } else if indexPath.row == 1 {
        key = "Address"
        if let address = self.place.address {
          if address.characters.count > 0 {
            value = address

          }
        }

      } else if indexPath.row == 2 {
        key = "Phone number"


        if let phoneNumber = self.place.phoneNumber {



          if phoneNumber.characters.count > 0 {
            value = phoneNumber



          }
        }

      } else if indexPath.row == 3 {
        key = "Website"
        if let website = self.place.website {
          if website.characters.count > 0 {
            value = website
          }
        }
      }
    }


    else if indexPath.section == 2 {
      key = "Get Directions"
    } else {
      key = "Photos of \(self.place.placeName)"
    }

    if indexPath.section == 0 {
      cell.textLabel?.text = key
      cell.detailTextLabel?.text = value
    } else {
      cell.textLabel?.text = key



    }

    return cell
  }



  func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

     if indexPath.section == 0 {

      if indexPath.row == 1 {
        print("it works!")
        if let coordinate = place.location?.coordinate {
          if let url = URL(string:"http://maps.apple.com/maps?daddr=\(coordinate.longitude),\(coordinate.latitude)") {
            UIApplication.shared.open(url, options: [:], completionHandler: nil)
          }
        }

        }







     if indexPath.row == 2 {



        guard let phoneNumber = self.place.phoneNumber,
          phoneNumber.count > 0,



        let url = URL(string: "tel:\(phoneNumber.addingPercentEncoding(withAllowedCharacters: .urlPathAllowed)!)")

          else { return }

        if UIApplication.shared.canOpenURL(url) {
          UIApplication.shared.open(url, options: [:], completionHandler: nil)

          }

      } else { return }





    }





   else if indexPath.section == 2 {
      self.performSegue(withIdentifier: directionsSegueId, sender: self)
    } else {
      if let photoReferences = self.place.photoReferences {
        if photoReferences.count != 0 {
          self.performSegue(withIdentifier: gallerySegueId, sender: self)

          return

        }
      }
      // View Photo button
      let alertController = UIAlertController(title: "No photos", message: "Sorry, but there are no photos found for this place.", preferredStyle: .alert)

      let cancelAction = UIAlertAction(title: "Ok", style: .cancel) { action in
        // ...
      }
      alertController.addAction(cancelAction)
      self.present(alertController, animated: true, completion: nil)
    }
  }

  func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    if section == 0 {
      return "    Info"
    }
    else if section == 1{
     //return "    Request a Uber"
      return ""
    }
    else if section == 2 {
      return "    Navigation"
    } else {
      return "    Photo"
    }
  }


  override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == directionsSegueId {
      let controller = segue.destination as! DirectionsViewController
      controller.place = self.place
    } else if segue.identifier == gallerySegueId {
      let navController = segue.destination as! UINavigationController
      let galleryController = navController.viewControllers[0] as! GalleryViewController
      galleryController.place = self.place
    }
  }



  func generatePlaceUrl() -> URL {
    let placeNamePlus = self.place.placeName.replacingOccurrences(of: " ", with: "+")
    var urlString = "https://www.google.com/maps/place/" + placeNamePlus
    if let addressPlus = self.place.address?.replacingOccurrences(of: " ", with: "+") {
      urlString =  urlString + "+" + addressPlus
    }

    let url = URL.init(string: urlString)
    if let finalUrl = url {
      return finalUrl
    }

    return URL.init(string: "https://www.maps.google.com")!
  }

  func loadDetailInfo() {
    let loader = PlacesLoader()
    self.activityIndicator.startAnimating()
    self.tableView.isHidden = true
    loader.loadDetailInformation(forPlace: self.place) { (dictionary, error) in
      guard dictionary != nil else {
        DispatchQueue.main.async {
          self.activityIndicator.stopAnimating()
          self.tableView.isHidden = false
          self.tableView.reloadData()

        }

        return
      }

      if let resultDictionary = dictionary!["result"] as? NSDictionary {
        self.place.address = (resultDictionary["formatted_address"] as? String)!
        self.place.phoneNumber = resultDictionary["formatted_phone_number"] as? String
        self.place.website = resultDictionary["website"] as? String

        if let photosArr = resultDictionary["photos"] as? [NSDictionary] {
          for photoDict in photosArr {
            let photoReference = photoDict["photo_reference"] as? String
            if self.place.photoReferences == nil {
              self.place.photoReferences = [String]()
            }

            self.place.photoReferences?.append(photoReference!)
          }
        }
      }

      DispatchQueue.main.async {
        self.activityIndicator.stopAnimating()
        self.tableView.isHidden = false
        self.tableView.reloadData()

      }
    }
  }
}

您的纬度和经度值是错误的,请尝试以下方法:

if let url = URL(string:"http://maps.apple.com/maps?daddr=\(coordinate.latitude),\(coordinate.longitude)") {
    UIApplication.shared.open(url, options: [:], completionHandler: nil)
}

我还应该发布什么代码-以便您能提供帮助?我知道您在那里做了什么。。老兄,小事情就这么发生了。天才谢谢@Matthew。一点问题都没有,小事情总有一天会把我们都弄明白的:你能帮我把我的答案标记为正确吗?谢谢,没问题,兄弟!你明白了