2017-01-31 2 views
2

현재 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에는 출력이 없으므로 빈 흰색 배경보기 컨트롤러가됩니다.

+0

당신은 아마 UIView의 좌표에 좌표 어떻게 GPS를 변환 표시해야합니다. – MirekE

+0

@MirekE 조정되었습니다! CLLocationManagerDelegate 메서드에서 좌표를 가져오고있다 didUpdateLocations. 나는 MKPolyline을 성공적으로 만들 수있다. 나는 UIView 나 찍은 경로를 표현할 수 있도록 폴리 라인과 동일한 UIBezierPath를 만드는 법을 모른다. – lifewithelliott

+0

MKPolyline을 이미지로 캡처 http://stackoverflow.com/questions/4334233/how-to-capture-uiview-to-uiimage-with-loss-of-quality-on-retina-display –

답변

1

확장 프로그램이 정상적으로 작동합니다. 문제는보기에 레이어를 추가하는 코드 (표시되지 않음)에있을 수 있습니다.

프로젝트를 단순화하는 것이 좋습니다. 예를 들어보기에 꼭 맞는 지점의 미리 정의 된 배열을 사용하는 것이 좋습니다. 스트로크 및 채우기 위해 검정색과 노란색 같은, 명확하게 볼 수 있습니다

let points = [ 
    CGPoint(x: 10, y: 10), 
    CGPoint(x: 490, y: 10), 
    CGPoint(x: 490, y: 290), 
    CGPoint(x: 10, y: 290), 
    CGPoint(x: 10, y: 10) 
] 

사용 색상 : 예를 들어, 폭 500 픽셀, 300 픽셀 높은 뷰를 들어, 같은 것을 사용할 수 있습니다.

는 예를 들어, 경로가 올바르게 뷰에 추가되어 있는지 확인합니다 :

let path = UIBezierPath(points: points) 

let shapeLayer = CAShapeLayer(path: path, lineColor: UIColor.blue, fillColor: UIColor.lightGray) 

view.layer.addSublayer(shapeLayer) 

이 엑스 코드의 인터페이스 빌더에서 뷰가 들어있는 컨트롤러를 검사합니다. 디버그 뷰 계층 기능에서 :

enter image description here

+0

내가 무엇을 바꿀 수 있었는지 확실하지 않습니다. 그러나 진보가 확실히 이루어졌습니다. 더 나은 표현을 위해 UIView의 배경을 투명하게 만들었습니다. 이제 전체 폴리선이 뷰 범위 내에서 그려지는 방법을 알아 내려고 노력했습니다. BezierPath의 크기와 중심/크기를 특정보기에 맞출 수 있습니까? 폴리 라인의 점을 뷰 내부에서 재배치시킬 수 있다고 생각했습니다. 2000 x + 포인트는 이전에 말했던 것처럼 화면에서 완전히 벗어납니다! 내 토끼 구멍으로 끌고 가서 미안해! – lifewithelliott

+0

https://developer.apple.com/reference/uikit/uibezierpath/1624340-apply 특허의 경계 외부인 인 경우, Xcode의 인터페이스 빌더에서 뷰가 포함 된 컨트롤러를 "디버그 뷰 계층 구조"기능 검사 – MirekE

관련 문제