1

I have an array of coordinate objects, and want to connect these (in the order they are) as a Polyline in Mapkit in Swift 3. How do I go about doing this? Here is my data structure

List<Location> (
    [0] Location {
        lat = 37.33477977;
        long = -122.03369603;
    },
    [1] Location {
        lat = 37.33477977;
        long = -122.03369603;
    },
    [2] Location {
        lat = 37.33477977;
        long = -122.03369603;
    },
    [3] Location {
        lat = 37.33305632;
        long = -122.05318781;
    },
    [4] Location {
        lat = 37.33298105;
        long = -122.0535463;
    }
)

The previous discussions on this topic are now outdated.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Sam
  • 6,616
  • 8
  • 35
  • 64

1 Answers1

2

You have to convert it to an array of [CLLocation] have a look at this similar question:

var locations = [CLLocation(latitude: 37.582691, longitude: 127.011186), CLLocation(latitude: 37.586112,longitude: 127.011047), CLLocation(latitude: 37.588212, longitude: 127.010438)]
        var coordinates = locations.map({(location: CLLocation) -> CLLocationCoordinate2D in return location.coordinate})
        var polyline = MKPolyline(coordinates: &coordinates, count: locations.count)
Community
  • 1
  • 1
pesch
  • 1,976
  • 2
  • 17
  • 29
  • Swift has been updated since that answer, and the code in the question doesn't work anymore. – Sam May 16 '17 at 07:38
  • I've submitted an updated edit to the linked question with Swift 3 compatible code. – Sam May 16 '17 at 07:44
  • The documentation makes reference to `init(coordinates coords: UnsafePointer, count: Int)`. Seems to be the same as this question: http://stackoverflow.com/questions/25560751/unsafemutablepointer-in-swift-as-replacement-for-properly-sized-c-array-in-obj-c – pesch May 16 '17 at 07:45