使用swift和Mapkit,显示用户当前位置与特定位置之间的路由

时间:2016-07-20 18:23:39

标签: ios swift mapkit cllocationmanager

应用程序打开后,我想在Swift中使用MapKit在用户的当前位置和特定位置之间绘制一条路线。

我知道如何请求/获取用户当前位置:

let locationManager = CLLocationManager()

override func viewDidLoad() {
  super.viewDidLoad()
  originalTopMargin = topMarginConstraint.constant
   locationManager.delegate = self
  locationManager.requestWhenInUseAuthorization()
   if CLLocationManager.locationServicesEnabled() {
    locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters
    locationManager.requestLocation()
  }
}

我不知道的是,实际计算和显示路线的代码在哪里?它会在这个功能中吗?

func locationManager(manager:CLLocationManager, didUpdateLocations locations:[AnyObject]) 

我已经阅读了Ray Wenderlich Mapkit教程以及许多其他网站,我很难找到我的具体问题的答案。到目前为止我发现的所有东西都有有用的信息,但是我找不到我想要的东西。

1 个答案:

答案 0 :(得分:1)

我很久没有这样做了 - 但你想要做的就是使用MKDirections API。

基本上,您创建了一个MKDirectionsRequest对象,您可以使用它来最终获得到达目的地的步骤列表。您也可以使用它来获取MKPolyLine对象以在地图等上呈现它。

这是教程的链接。它是在Objective-C中,但翻译代码应该相对容易。http://www.devfright.com/mkdirections-tutorial/