0

I am creating an app that tracks a drive, so as you drive it records location and at the end, it shows a map of where you have been. I am using swiftUI's MapKit to display map data. In the process of displaying the information, I ran into a problem.

I plan to use MapKit polyline (MKPolyline) to show the location points but to initialize an MKPolyline, you must first have an unsafe pointer of either MKMapPoints or CLLocationCoordinate2Ds.

init(points: UnsafePointer<MKMapPoint>, count: Int)
init(coordinates: UnsafePointer<CLLocationCoordinate2D>, count: Int)

I have an array of coordinates but need an unsafe pointer instead. How do I go about converting them?

a1cd
  • 17,884
  • 4
  • 8
  • 28
  • 2
    AFAIK you can simply pass your array of MKMapPoint `[MKMapPoint]` `(points: points, count: points.count)` – Leo Dabus Aug 09 '21 at 23:36
  • Yep, no need to create the unsafe pointer. Swift will take care of that for you. – Rob Aug 10 '21 at 00:29
  • I'm going to suggest that this duplicates https://stackoverflow.com/questions/68028656/why-can-i-pass-uint8-type-to-the-unsafepointeruint8-type-parameter – matt Aug 10 '21 at 03:23

1 Answers1

0

Thanks to Leo and Rob. They told me how. I just put the array right into the coordinates and swift handled the rest for me.

let Line = MKPolyline(coordinates: Locations, count: Locations.count)

Check out this thread for a more in depth answer

a1cd
  • 17,884
  • 4
  • 8
  • 28
  • You didn't "figure it out". You were told the answer. Twice. – matt Aug 10 '21 at 03:23
  • As a stylistic matter, in Swift, names of variables (such as your array) should always start with a lowercase letter. We capitalize types (names of classes, structs, enums, etc.), but variables should be in camelCase, starting with a lowercase letter. – Rob Aug 10 '21 at 04:45
  • @matt So sorry, i feel bad, i didn't mean that. yes Leo and Rob told me. – a1cd Aug 10 '21 at 15:42