0

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

Holger
  • 581
  • 5
  • 10

2 Answers2

2

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

mapView.delegate = self;
[self plotSnapPosition];
Sebastian
  • 2,109
  • 1
  • 20
  • 15
0

Have you tried adding a [overlayView setNeedsDisplay] call when you finish drawing?

user1118321
  • 25,567
  • 4
  • 55
  • 86
  • Thank you for your reply. But it didn't helped me out. Sorry the setNeedsDisplay methode doesn't work either for the mapView or the polylineView. No path is shown at all. Only after moving the map. – Holger Jan 07 '12 at 23:51