首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在UIBezierPath上绘制MKMapView覆盖图?

如何在UIBezierPath上绘制MKMapView覆盖图?
EN

Stack Overflow用户
提问于 2015-02-08 12:46:23
回答 1查看 1.8K关注 0票数 1

我尝试从MKOverlayPathRenderer中子类并实现-createPath

代码语言:javascript
复制
- (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];
}

我在这里创建覆盖:

代码语言:javascript
复制
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];

然后在这里创建渲染器:

代码语言:javascript
复制
- (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;
}

但我看不见地图上的任何线条。我该怎么办?谢谢。

P.S. CGPathAddLineToPoint现在用于测试,在生产中我需要曲线。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-02-20 09:46:10

根据安娜的回答,你应该使用[self pointForMapPoint:points[i]]而不是points[i]

代码语言:javascript
复制
- (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];
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28394074

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档