我尝试从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;
}但我看不见地图上的任何线条。我该怎么办?谢谢。
P.S. CGPathAddLineToPoint现在用于测试,在生产中我需要曲线。
发布于 2015-02-20 09:46:10
根据安娜的回答,你应该使用[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];
}https://stackoverflow.com/questions/28394074
复制相似问题