当我尝试在地图视图中单击折线叠加层时,以下代码可以工作,但需要了解渲染器的绘图坐标中折线的确切宽度。例如,650.0可以在某个缩放比例下工作,但是如果我进一步放大,则正确的宽度可能约为150。是否有办法在渲染器坐标中找到折线的线宽以使复制功能可行?
我尝试了polylineRenderer.lineWidth,但是它返回相对于其他坐标系的宽度。
@objc func didTapGesture(_ sender: UITapGestureRecognizer) {
let touchPoint = sender.location(in: mapView)
let touchCoordinate = mapView.convert(touchPoint, toCoordinateFrom: mapView)
let mapPoint = MKMapPoint(touchCoordinate)
for overlay in mapView.overlays {
if overlay is MKPolyline {
if let polylineRenderer = mapView.renderer(for: overlay) as? MKPolylineRenderer {
let polylinePoint = polylineRenderer.point(for: mapPoint)
let width: CGFloat = 650.0
let copiedPath: CGPath = polylineRenderer.path.copy(strokingWithWidth: width, lineCap: .butt, lineJoin: .miter, miterLimit: 1.0)
if copiedPath.contains(polylinePoint) {
polylineRenderer.strokeColor = polylineRenderer.strokeColor?.withAlphaComponent(1.0)
}
}
}
}
}