Im trying to draw MKpolyline using following code.
var points: [CLLocationCoordinate2D] = []
let locationDetails = myArray as! [Location]
for each in locationDetails{
print("Latitude \(each.latitude) - Longitude \(each.longitude)")
points.append(CLLocationCoordinate2DMake(each.latitude, each.longitude))
}
let centerLocation : Location = myArray.anyObject() as! Location
let centerPoint = CLLocationCoordinate2DMake(centerLocation.latitude, centerLocation.longitude);
print("POINTS \(points)")
let polyline = MKPolyline(coordinates: points, count: points.count)
mapView.add(polyline)
let region = MKCoordinateRegionMakeWithDistance(centerPoint, 5000.0, 7000.0)
mapView.setRegion(region, animated: true)
but im getting messy polyline. see attached Screenshot. first i thought problem with coordinates. but when i tried those lat and long with this online tool it gave me perfect line with pins. print
log the cooridnates perfectly.
6.8517432035942099,79.905121140256398
6.8475310038839003,79.902849560677893
6.8581436667655202,79.908119598477995
6.8374054133955697,79.893569033667305
6.8566833715949702,79.907449213862293
6.8406604416732097,79.898999668732202
6.83810886461925,79.892979702055399
6.8542092852358296,79.906306928098104
6.8402581522304899,79.895430151447599
6.8368830112804098,79.894324662238304
6.8440497480396596,79.900090321972897
6.8493188219208703,79.902743613421805
6.8484064936701703,79.902548063620799
6.85723423027141,79.908170560449307
6.8450230546359396,79.899919834062402
6.84973481577528,79.903571158721903
6.8459125422004998,79.899705760255401
6.8551684683252603,79.906455958336494
6.8414650205586502,79.901498481705701
6.8405259121273101,79.899981692507794
6.8513044109631798,79.904290326014106
6.8373792317329203,79.895165984713401
6.8372333821392299,79.895255911941504
6.8466924363811001,79.900271203443296
6.8506548972864199,79.903621031045802
6.8390314607013396,79.892975427284696
6.8469577236164803,79.901166977435295
6.83748895475187,79.895160402688802
6.8398749735270101,79.896293487474196
6.8407466076378203,79.893662072792594
6.8422778556187103,79.900998165905406
6.8398627359483797,79.893336938768499
6.8408240983326403,79.894646443501003
6.8560738395963403,79.906751504242294
6.8469752417941097,79.902100302353404
6.8374541960720299,79.895048104300997
6.8374381558016104,79.895128792781804
6.8524381890956798,79.905752716660402
6.8407501699466602,79.900907306074998
6.8401687173236496,79.897176772430399
6.8405627925012604,79.898056201711199
6.8430614378366998,79.900337923392598
6.8532910058338699,79.906087154597003
It was a problem with the data array. since im getting the coordinates from coredata those werent in order. so thats why it didnt draw the perfect line one after another. once i sorted the array i got it perfectly.
locationDetails = locationDetails.sorted(by: { $0.id < $1.id })