如何在MKMapView上绘制UIBezierPath叠加层? [英] How to draw UIBezierPath overlay on MKMapView?

查看:113
本文介绍了如何在MKMapView上绘制UIBezierPath叠加层?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I try to subclass from MKOverlayPathRenderer and implement -createPath

- (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];

然后在此处创建渲染器:

And then create renderer here:

- (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;
}

但是我在地图上看不到任何线条.我该怎么办? 谢谢.

But I can't see any lines on map. What should I do? Thanx.

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

P.S. CGPathAddLineToPoint is for tests now, in production I need curves.

推荐答案

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

According to Anna's answer you should use [self pointForMapPoint:points[i]] instead of 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];
}

这篇关于如何在MKMapView上绘制UIBezierPath叠加层?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆