我有一个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

最佳答案

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

mapView.delegate = self;
[self plotSnapPosition];

关于ios - MKPolyline仅在移动 map 时显示,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/8771562/

10-08 23:39