2015-02-08 104 views
0

我試着從MKOverlayPathRenderer子類和實施-createPath如何在MKMapView上繪製UIBezierPath覆蓋圖?

- (void)createPath 
{ 
    MKPolyline *line = (id)self.overlay; 

    MKMapPoint *points = line.points; 
    NSUInteger pointCount = line.pointCount; 

    CGMutablePathRef path = CGPathCreateMutable(); 
    CGPathMoveToPoint(path, NULL, points[0].x, points[0].y); 

    for (int i = 1; i < pointCount; i++) { 
     CGPathAddLineToPoint(path, NULL, points[i].x, points[i].y); 
    } 
    [self setPath:path]; 
} 

創建覆蓋這裏:

CLLocationCoordinate2D coordinates[events.count]; 
for (int i; i < events.count; i++) { 
    coordinates[i] = [events[i] coordinate]; 
} 

MKPolyline *line = [MKPolyline polylineWithCoordinates:coordinates count:events.count]; 
[mapView addOverlay:line]; 

然後在這裏創建渲染:

- (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(MKPolyline *)overlay 
{ 
    MKBezierPathRenderer *r = [[MKBezierPathRenderer alloc] initWithOverlay:overlay]; 
    r.lineWidth = 8.f; 
    r.strokeColor = [UIColor redColor]; 
    r.fillColor = [UIColor redColor]; 

    return r; 
} 

,但我看不到任何在地圖上的線。我該怎麼辦? Thanx。

P.S. CGPathAddLineToPoint現在用於測試,在生產中我需要曲線。

+0

路徑的點值不應該是MKMapPoints但在一組不同的單位。使用pointForMapPoint方法進行轉換。例如,請參閱http://stackoverflow.com/questions/19941200/looking-for-an-mkoverlaypathrenderer-example。 – Anna 2015-02-08 17:11:57

+0

@安娜謝謝你!我想念它。你能發表你的評論作爲答案嗎? – AlKozin 2015-02-10 14:28:22

+0

謝謝,但請繼續並使用更新的代碼發佈答案。你可以在一段時間後接受它。 – Anna 2015-02-10 14:50:43

回答

2

據安娜的回答,你應該使用[self pointForMapPoint:points[i]]代替points[i]

- (void)createPath 
{ 
    MKPolyline *line = (id)self.overlay; 

    MKMapPoint *points = line.points; 
    NSUInteger pointCount = line.pointCount; 

    CGMutablePathRef path = CGPathCreateMutable(); 
    CGPoint point = [self pointForMapPoint:points[0]]; 
    CGPathMoveToPoint(path, NULL, point.x, point.y); 

    for (int i = 1; i < pointCount; i++) { 
     point = [self pointForMapPoint:points[i]]; 
     CGPathAddLineToPoint(path, NULL, point.x, point.y); 
    } 
    [self setPath:path]; 
}