iosswift3locationmapkitmkpolyline

Draw a line on Mapkit using list of coordinates


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.


Solution

  • 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)