I am making an attempt to attract a route on my Map View in Swift.
The under is what I drew with MKMapSnapshotter
.
My downside is that the primary and the final factors of my route(coordinate) are all the time linked with straight line they usually appears like a closed loop.
I do not need them to not be linked.
The next is my code, and please give me some answer for the issue.
func saveMapViewAsImage(area: MKCoordinateRegion, dimension: CGSize) {
let choices = MKMapSnapshotter.Choices()
choices.area = area
choices.dimension = dimension
choices.mapType = .commonplace
MKMapSnapshotter(choices: choices).begin { snapshot, error in
guard let snapshot = snapshot else { return }
let mapImage = snapshot.picture
let finalImage = UIGraphicsImageRenderer(dimension: dimension).picture { _ in
mapImage.draw(at: .zero)
// get [CLLocationCoordinate2D]
let coordinates = self.getTrackPointForPolyline()
guard coordinates.depend > 1 else { return }
// [CLLocationCoordinate2D] -> [CGPoint]
let factors = coordinates.map { coordinate in
snapshot.level(for: coordinate)
}
// draw traces
let path = UIBezierPath()
path.lineWidth = Ok.Map.routeLineWidth * 0.5
path.lineCapStyle = .spherical
path.lineJoinStyle = .spherical
path.transfer(to: factors[0])
for index in 1..<factors.depend {
path.addLine(to: factors[index])
}
Ok.Map.routeLineColor.withAlphaComponent(0.75).setStroke()
path.stroke()
}
// save as picture
self.saveRenderedImageToDocumentDirectory(picture: finalImage)
}
}