我在MKMapView
上画了一条路线。我保存了位置数据以绘制路线。每天存储的位置数据为数十兆字节。因此,我正在尝试减少存储的数据,我所需要的只是一张带有过去路线的地图。
除了数百个存储的位置数据以外,还有什么简单的数据可以使路线如屏幕快照所示显示?
您可以使用MKMapSnapshotter
。不幸的是,您必须手动使用MKMapSnapshotter
路径,使用stroke
将point(for:)
转换为point(for:)
中的CLLocationCoordinate2D
:
CGPoint
产生:
MKMapSnapshotter.Snapshot
现在,以上内容显然只是在单个let options = MKMapSnapshotter.Options()
options.region = mapView.region
options.size = mapView.bounds.size
MKMapSnapshotter(options: options).start { snapshot, _ in
guard let snapshot = snapshot else { return }
let image = UIGraphicsImageRenderer(size: options.size).image { _ in
snapshot.image.draw(at: .zero)
let count = route.polyline.pointCount
let points = route.polyline.points()
guard count > 1 else { return }
let path = UIBezierPath()
path.move(to: snapshot.point(for: points[0].coordinate))
for i in 1 ..< count {
path.addLine(to: snapshot.point(for: points[i].coordinate))
}
path.lineWidth = 4
path.lineCapStyle = .round
path.lineJoinStyle = .round
UIColor.blue.withAlphaComponent(0.75).setStroke()
path.stroke()
}
guard let data = image.pngData() else { return }
// you can now write this `data` to persistent storage
}
中抚摸与相关的route.polyline
,并且您将遍历模型,大概抚摸每个单独的那些有色部分MKDirections
,但希望可以说明这个想法。