현재 MKMapView에서 내 위치를 추적 중입니다. 내 목표는 추적 된 위치에서 만든 MKPolyline과 동일한 베 지어 경로를 그립니다.UIBezierPath를 UIView의 MKPolyline과 동일하게 그리는 방법
내가 시도한 것은 : 모든 위치 좌표를 CLLocation 배열에 저장하십시오. 해당 배열을 반복하고 lat/lng 좌표를 CLLocationCoordinate2D 배열에 저장합니다. 그런 다음 폴리선이 화면 뷰에 있는지 확인한 다음 CGPoint의 모든 좌표를 변환합니다.
현재 시도 :
@IBOutlet weak var bezierPathView: UIView!
var locations = [CLLocation]() // values from didUpdateLocation(_:)
func createBezierPath() {
bezierPathView.isHidden = false
var coordinates = [CLLocationCoordinate2D]()
for location in locations {
coordinates.append(location.coordinate)
}
let polyline = MKPolyline(coordinates: coordinates, count: coordinates.count)
fitPolylineInView(polyline: polyline)
let mapPoints = polyline.points()
var points = [CGPoint]()
for point in 0...polyline.pointCount
{
let coordinate = MKCoordinateForMapPoint(mapPoints[point])
points.append(mapView.convert(coordinate, toPointTo: polylineView))
}
print(points)
let path = UIBezierPath(points: points)
path.lineWidth = 2.0
path.lineJoinStyle = .round
let layer = CAShapeLayer(path: path, lineColor: UIColor.red, fillColor: UIColor.black)
bezierPathView.layer.addSublayer(layer)
}
extension UIBezierPath {
convenience init(points:[CGPoint])
{
self.init()
//connect every points by line.
//the first point is start point
for (index,aPoint) in points.enumerated()
{
if index == 0 {
self.move(to: aPoint)
}
else {
self.addLine(to: aPoint)
}
}
}
}
extension CAShapeLayer
{
convenience init(path:UIBezierPath, lineColor:UIColor, fillColor:UIColor)
{
self.init()
self.path = path.cgPath
self.strokeColor = lineColor.cgColor
self.fillColor = fillColor.cgColor
self.lineWidth = path.lineWidth
self.opacity = 1
self.frame = path.bounds
}
}
나는 변환 (_ :) 방법 (가) 올바른지 확실하지에서 저장된 콘솔에 포인트를 출력 할 수 있어요. 그러나 BezierPathView에는 출력이 없으므로 빈 흰색 배경보기 컨트롤러가됩니다.
당신은 아마 UIView의 좌표에 좌표 어떻게 GPS를 변환 표시해야합니다. – MirekE
@MirekE 조정되었습니다! CLLocationManagerDelegate 메서드에서 좌표를 가져오고있다 didUpdateLocations. 나는 MKPolyline을 성공적으로 만들 수있다. 나는 UIView 나 찍은 경로를 표현할 수 있도록 폴리 라인과 동일한 UIBezierPath를 만드는 법을 모른다. – lifewithelliott
MKPolyline을 이미지로 캡처 http://stackoverflow.com/questions/4334233/how-to-capture-uiview-to-uiimage-with-loss-of-quality-on-retina-display –