看不到MKPolyline

时间:2012-10-11 04:01:31

标签: ios mkmapview mkannotation mkoverlay

我正在尝试添加连接两个MKPinAnnotationViews的简单MKPolyline。我阅读了本教程并观看了苹果公司的WWDC 2010视频。 (http://spitzkoff.com/craig/?p=136)。我只是想绘制一些简单的东西,所以在viewDidLoad中,我这样做:

MKMapPoint *pointArray = malloc(sizeof(CLLocationCoordinate2D) * 2);
pointArray[0] = MKMapPointForCoordinate(CLLocationCoordinate2DMake(32, -115));
pointArray[1] = MKMapPointForCoordinate(CLLocationCoordinate2DMake(32, -130));

MKPolyline *line = [MKPolyline polylineWithPoints:pointArray count:2];
[self.mapView addOverlay:line];
    free(pointArray);

然后我的代表回调是:

- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay {
    if ([overlay isKindOfClass:[MKCircle class]]) {
        MKCircleView *view = [[MKCircleView alloc] initWithOverlay:overlay];
        view.fillColor = [UIColor redColor];
        view.strokeColor = [[UIColor redColor] colorWithAlphaComponent:0.5];
        return view;
    }
    else {
        MKPolylineView *view = [[MKPolylineView alloc] initWithOverlay:overlay];
        view.fillColor = [UIColor redColor];
        view.lineWidth = 20;

    return view;
    }
}

如果我用MKCircle做一个简单的例子,我确实看到了圆圈。使用我在viewDidLoad中添加的MKPolyline,我确实得到了mapView:viewForOverlay:callback,但屏幕上没有任何内容。我错过了什么吗?感谢。

1 个答案:

答案 0 :(得分:0)

MKPolylineView使用strokeColor来绘制,而不是fillColor如此更改:

MKPolylineView *view = [[MKPolylineView alloc] initWithOverlay:overlay];
view.fillColor = [UIColor redColor];
view.lineWidth = 20;

为:

MKPolylineView *view = [[MKPolylineView alloc] initWithOverlay:overlay];
view.strokeColor = [UIColor redColor];  // <-- stroke instead of fill
view.lineWidth = 20;