gpt4 book ai didi

ios - 如何在 MKMapView 上绘制 UIBezierPath 覆盖?

转载 作者:行者123 更新时间:2023-11-29 10:33:20 25 4
gpt4 key购买 nike

我尝试从 MKOverlayPathRenderer 继承并实现 -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];

然后在这里创建渲染器:

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

但是我在 map 上看不到任何线条。我应该怎么办?谢谢。

附言CGPathAddLineToPoint 现在用于测试,在生产中我需要曲线。

最佳答案

根据 Anna 的回答,您应该使用 [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];
}

关于ios - 如何在 MKMapView 上绘制 UIBezierPath 覆盖?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28394074/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com