2012-01-07 81 views
0

我有一个MKMapView,其上有一些引脚。我用MKPolyline视图连接引脚。但是MKPolyline仅在我移动地图时(MapView更新时)才会显示。我想从一开始就看到MKPolyline仅在移动地图时显示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; 
} 

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

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

这里是截图:

the two pins

移动地图后:

the path after moving the map

回答

2

确保您在之前设置了mapView的代理,并添加了叠加层。所以,在你的情况下,

mapView.delegate = self; 
[self plotSnapPosition]; 
+0

非常感谢。这是解决方案。 – Holger 2012-01-08 21:39:29

0

您是否尝试过加入[OverlayView的setNeedsDisplay]呼叫完成在画什么?

+0

谢谢你的回复。但它没有帮助我。对不起,setNeedsDisplay方法不适用于mapView或polylineView。根本没有显示路径。只有在移动地图后。 – Holger 2012-01-07 23:51:28