Domanda

I am testing out some basic drawing functionality on a MKMapView and i am getting some strange things happening. You can refer to the dropbox link that is below. This is being run and tested on an actual device - not the simulator.

You will notice that some of the MKPolyLines are jagged as they are connected and some are not. What is the cause of this and can this be corrected? I didnt see any option for setting up anti-aliasing. The far right polygon you can see it the most i think. But in general it seems some lines are smoother than others.

The other thing that happens that this screenshot doesn't show is that every time i tap on the view to add a new overlay all the overlays on the map "blink" as if they are getting redrawn or perhaps not being redrawn fast enough. Was wondering what the cause of this was or perhaps i should be doing something else to prevent this from happening that i do not know about.

Also a question about the delegate. What if you have 100 different overlays on your map? are you supposed to do a check for each one in the delegate as to tell it what kind of drawing to perform? In my case there is only 3 cases but I can imagine this method could get very large with what i have set up here. I know this is an aside question but thought I would insert it here.

https://www.dropbox.com/s/dbordqnml33urmu/Screenshot%202013.03.28%2011.26.36.png

here is the relevant code:

-(void)addCoordinateToList:(UITapGestureRecognizer*)recognizer{
    CGPoint tappedPoint = [recognizer locationInView:map];
    CLLocationCoordinate2D coord = [map convertPoint:tappedPoint toCoordinateFromView:map];
    NSLog(@"Coordinate Info: Lat %f, Lon %f", coord.latitude, coord.longitude);

    CLLocation *newLocation = [[CLLocation alloc] initWithLatitude:coord.latitude longitude:coord.longitude];
    //Extension NSValue new for iOS6
    //[path addObject:[NSValue valueWithMKCoordinate:coord]];
    [path addObject:newLocation];

    [self drawPolygon];

    clearPolygonFromMap.enabled = YES;
    clearPolygonFromMap.alpha = 1.0;

    //add annotations
    [self setPolygonAnnotationWithCoordinate:coord andWithTitle:@"default title"];

}

//draw polyline. set up polygon but dont add as overlay until you are done drawing your shape. This code is not included because theres lots of button states here that arent relevant.

-(void)drawPolygon{

NSInteger numberOfCoordinates = path.count;
if(coords != NULL)
    free(coords);
coords = malloc(sizeof(CLLocationCoordinate2D) * numberOfCoordinates);

CLLocationCoordinate2D coordinates[numberOfCoordinates];
for(int pathIndex = 0; pathIndex < numberOfCoordinates; pathIndex++){
    CLLocation *location = [path objectAtIndex:pathIndex];
    coords[pathIndex] = location.coordinate;

    coordinates[pathIndex] = coords[pathIndex];
}

  polyLine = [MKPolyline polylineWithCoordinates:coordinates count:numberOfCoordinates];
  polygon = [MKPolygon polygonWithCoordinates:coords count:path.count];
  [map addOverlay:polyLine];
}

//set up the circle overlay

-(void)setPolygonAnnotationWithCoordinate:(CLLocationCoordinate2D)coord andWithTitle:(NSString *)polygonDescription{

    circle = [MKCircle circleWithCenterCoordinate:coord radius:15];
    circle.title = polygonDescription;
    [map addOverlay:circle];
    //[map addAnnotation:circle];
}

//MapView overlay delegate method. is there a better way to check overlays? what if i had 100 overlays

-(MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id<MKOverlay>)overlay{

    if(overlay == circle){
        pointCircleView = [[MKCircleView alloc]initWithOverlay:overlay];
        pointCircleView.strokeColor = [UIColor greenColor];
        pointCircleView.fillColor = [UIColor redColor];
        //circleView.alpha = 0.5;
        pointCircleView.lineWidth = 4.0;
        return pointCircleView;
    }
    else if(overlay == polyLine){
        borderView = [[MKPolylineView alloc] initWithPolyline:overlay];
        borderView.strokeColor = [UIColor blackColor];
        borderView.lineWidth = 1.0;
        return borderView;
    }
    else if(overlay == polygon){
        polygonView = [[MKPolygonView alloc] initWithPolygon:polygon];
        polygonView.strokeColor = [UIColor blackColor];
        polygonView.lineWidth = 1.0;
        polygonView.fillColor = [UIColor colorWithRed:1.0 green:1.0 blue:0.0 alpha:0.4];
        return polygonView;
    }

}
È stato utile?

Soluzione

For the blinking, see this post - it may be a bug in the overlay system.

With the jagged lines, they do appear to be antialiased in your screenshot, but may have been scaled up. From experimentation, I believe that overlays are rendered to a separate bitmap for discrete zoom levels and then rendered over the map view scaled up or down to suit the current zoom level. So it may be that your overlay looks a bit rough at that level but zooming in or out a bit improves it.

Regarding the delegate, you could check the class of the overlay that is passed in to cut down the complexity of your mapView:viewForOverlay function rather than checking for specific overlays. So:

if ([overlay isKindOfClass:[MKPolyline class]])
{
    borderView = [[MKPolylineView alloc] initWithPolyline:overlay];
    borderView.strokeColor = [UIColor blackColor];
    borderView.lineWidth = 1.0;
    return borderView;
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top