ユーザーの場所に応じて移動するアプリにマップがあります。ソースと目的地のポリラインを正常に描画しました。次のコードを使用する
- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id < MKOverlay >)overlay{
MKPolylineView *view1 = [[MKPolylineView alloc] initWithOverlay:overlay];
view1.lineWidth = 27.0;
view1.strokeColor = [UIColor colorWithRed:55.0/255.0 green:168.0/255.0 blue:219.0/255.0 alpha:1];
return view1;
}
しかし、私の問題は、マップが移動しているときです。つまり、ユーザーの移動に応じてマップの領域を変更しているため、下の画像でわかるように、ポリラインが実際のサイズよりも太く表示されないことがあります。
下の画像を添付します。マップが移動しているときに滑らかなポリラインを作成するにはどうすればよいか教えてください。
編集
Matt が提案したように、MKPolylineRenderer のサブクラスを作成し、以下のように drawMapRect メソッドを実装します。
-(void)drawMapRect:(MKMapRect)mapRect zoomScale:(MKZoomScale)zoomScale inContext:(CGContextRef)context{
CGMutablePathRef fullPath = CGPathCreateMutable();
BOOL pathIsEmpty = YES;
//merging all the points as entire path
for (int i=0;i< self.polyline.pointCount;i++){
CGPoint point = [self pointForMapPoint:self.polyline.points[i]];
if (pathIsEmpty){
CGPathMoveToPoint(fullPath, nil, point.x, point.y);
pathIsEmpty = NO;
} else {
CGPathAddLineToPoint(fullPath, nil, point.x, point.y);
}
}
//get bounding box out of entire path.
CGRect pointsRect = CGPathGetBoundingBox(fullPath);
CGRect mapRectCG = [self rectForMapRect:mapRect];
//stop any drawing logic, cuz there is no path in current rect.
if (!CGRectIntersectsRect(pointsRect, mapRectCG))return;
UIColor *darker = [UIColor blackColor];
CGFloat baseWidth = 10 / zoomScale;
// draw the dark colour thicker
CGContextAddPath(context, self.path);
CGContextSetStrokeColorWithColor(context, darker.CGColor);
CGContextSetLineWidth(context, baseWidth * 1.5);
CGContextSetLineCap(context, self.lineCap);
CGContextStrokePath(context);
// now draw the stroke color with the regular width
CGContextAddPath(context, self.path);
CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor);
CGContextSetLineWidth(context, baseWidth);
CGContextSetLineCap(context, self.lineCap);
CGContextStrokePath(context);
[super drawMapRect:mapRect zoomScale:zoomScale inContext:context];
}
しかし、同じ問題 は下の画像を参照してください。
OK、問題が発生したと思います。ポリラインは一度追加されますが、ユーザーの速度として MKMapView のズーム レベルを変更すると、ズーム レベルが変更されますが、ポリラインの幅は更新されません。
どうすればmkpolylineのlineWidthを動的に変更できますか??