Search code examples
swiftnavigationmapkitmkoverlaymkpolyline

swift: display mkdirection instructions in real time


Using mapKit, I would like to guide the user on the road by displaying the instruction for the current step. Hence, once the step is completed, the instruction is updated for the corresponding (next) step.

From what I know, mapKit doesn't allow you to do so straight. I have found this post but it is in Obj-C and I am using swift. So far my code looks like this:

for step in self.carRoute!.steps {
    var laStep: MKRouteStep = step as! MKRouteStep
    var mapPointForEndOfStep: MKMapPoint = laStep.polyline(laStep.polyline.pointCount - 1) as! MKMapPoint
}

But I get this error: '(_) -> _' is not identical to 'MKPolyline'

Anyone knows how to set the last point of each step? Or is there some kind of magic that would tell what step the user is on. Or if he is out of the directions indicated by the app?


Solution

  • I found the solution myself. So if anyone ever encounter the same problem, I translated the Obj-c code found here in swift :

    let thisStep: MKRouteStep = (self.carRoute?.steps[self.stepsCounter])!
    let pointsArray = thisStep.polyline.points()
    let lastPoint = pointsArray[thisStep.polyline.pointCount - 1]
    let theLoc: CLLocationCoordinate2D = MKCoordinateForMapPoint(lastPoint)
    
    self.theRegion = CLCircularRegion(center: theLoc, radius: 50, identifier: "turningPoint")