简体   繁体   中英

MKPolyline only shown when map is moved

I have a MKMapView with some pins on it. I connect the pins with a MKPolyline view. But the MKPolyline is only shown when I move the map (when the MapView is updated?). I want to see the MKPolyline from the beginning on.

Please inspect the following code:

-(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;
}

For testing issues I have set the color of the MKPolyline in the method -(void)plotSnapPosition to red. In the delegate I set it to blue. Only the blue one is shown, after moving the map around.

can someone help me out of this? I think it is only a small mistake. Thank you.

here are the screenshots:

the two pins

after moving the map:

the path after moving the map

Make sure you set the mapView's delegate before adding the overlay. So, in your case

mapView.delegate = self;
[self plotSnapPosition];

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM