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

时间:2022-11-15 19:56:56

Upon the app opening, I'd like to draw a route between user's current location to a specific location using MapKit in Swift.

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

I know how to request/get a hold of the users current location:

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

let locationManager = CLLocationManager()

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

What I don't know is, where does the code to actually calculate and display the route belong? Would it be in this function?

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

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

I've read the Ray Wenderlich Mapkit tutorials and many, many other sites, I'm having a tough time finding an answer to my specific question. Everything I've found so far has useful information, but doesn't quite have what I'm looking for.

我已经阅读了Ray Wenderlich Mapkit教程以及许多其他网站,我很难找到我的具体问题的答案。到目前为止我发现的所有内容都有有用的信息,但并不完全符合我的要求。

1 个解决方案

#1


1  

I haven't done this in a long time - but what you want to do is use the MKDirections API.

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

Basically you create an MKDirectionsRequest object and you can use this to eventually get a list of steps to reach your destination. You can also use this to get an MKPolyLine object to render this on your map etc.

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

Here is a link to a tutorial. It is in objective-C but it should be relatively easy to translate the code.http://www.devfright.com/mkdirections-tutorial/

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

#1


1  

I haven't done this in a long time - but what you want to do is use the MKDirections API.

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

Basically you create an MKDirectionsRequest object and you can use this to eventually get a list of steps to reach your destination. You can also use this to get an MKPolyLine object to render this on your map etc.

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

Here is a link to a tutorial. It is in objective-C but it should be relatively easy to translate the code.http://www.devfright.com/mkdirections-tutorial/

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