MKPolyline仅在移动地图时显示

时间:2012-01-07 17:25:18

标签: ios android-mapview mapkit

我有一个MKMapView,上面有一些引脚。我用MKPolyline视图连接引脚。但MKPolyline仅在我移动地图时显示(当MapView更新时?)。我希望从一开始就看到MKPolyline

请检查以下代码:

-(void)plotSnapPosition {
    for (id<MKAnnotation> annotation in myMapView.annotations) {
        [myMapView removeAnnotation:annotation];
    }
    for (id<MKOverlay> overlay in myMapView.overlays) {
        [myMapView removeOverlay:overlay];
    }
    NSArray *snaps = self.entry.snapsArray;
    CLLocationCoordinate2D *locations = malloc(sizeof(CLLocationCoordinate2D) * snaps.count);
    NSInteger counter = 0;
    for (Snap *snap in snaps) {
        locations[counter] = [snap coordinates];
        CLLocationCoordinate2D c = [snap coordinates];
        CAHAnnotation *annotation = [[CAHAnnotation alloc] initWithDate:snap.timeAsString coordinate:c counter:counter];
        [myMapView addAnnotation:annotation];
        counter++;
    }
    MKPolyline *polyline = [MKPolyline polylineWithCoordinates:locations count:snaps.count];
    MKPolylineView *routeLineView = [[MKPolylineView alloc] initWithPolyline:polyline];
    routeLineView.fillColor = [UIColor redColor];
    routeLineView.strokeColor = [UIColor redColor];
    routeLineView.lineWidth = 5;

    [myMapView setVisibleMapRect:polyline.boundingMapRect];
    [self.myMapView addOverlay:polyline];
}

-(MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id<MKOverlay>)overlay {
    if ([overlay isKindOfClass:[MKPolyline class]]) {
        MKPolylineView *routeLineView = [[MKPolylineView alloc] initWithPolyline:overlay];
        routeLineView.fillColor = [UIColor blueColor];
        routeLineView.strokeColor = [UIColor blueColor];
        routeLineView.lineWidth = 3;
        return routeLineView;
    }

    return nil;
}

对于测试问题,我已将方法MKPolyline-(void)plotSnapPosition的颜色设置为红色。在代表中我将其设置为蓝色。移动地图后,只显示蓝色的那个。

有人可以帮我解决这个问题吗?我认为这只是一个小错误。谢谢。

以下是截图:

the two pins

移动地图后

the path after moving the map

2 个答案:

答案 0 :(得分:2)

确保在添加叠加层之前设置mapView的委托。所以,在你的情况下

mapView.delegate = self;
[self plotSnapPosition];

答案 1 :(得分:0)

完成绘图后,您是否尝试过添加[overlayView setNeedsDisplay]调用?