2015-02-08 74 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]; 
}