0

I overrode the drawMapRect method in my .m file, but it seems that some lines of code within the rewritten method are giving me errors that I'm not sure how to solve. I made sure that I set to mkmapview delegate to self, all the correct packages were imported. Here is the code for the drawMapRect method,

-(void)drawMapRect:(MKMapRect)mapRect zoomScale:(MKZoomScale)zoomScale inContext:(CGContextRef)context{
CGRect rect = [self rectForMapRect:mapRect];
CGContextSaveGState(context);
CGContextAddRect(context, rect);
CGContextSetFillColorWithColor(context, [UIColor blackColor].CGColor);
CGContextFillRect(context, rect);
CGContextRestoreGState(context);

[super drawMapRect:mapRect zoomScale: zoomScale inContext:context];
}

The 2nd line of code is returning an error, "No visible @interface for 'MapViewClass' declares the selector for 'rectForMapRect:'

The last line of code is returning an error. "No visible @interface for 'UIViewController' declares the selector 'drawMapRect: zoomScale: inContext:'

EDIT After looking at Daji's answer, I made the following changes to my code. I made a new class that is a subclass of MKOverlayRenderer, and moved the drawMapRect method to that class. The previous errors I received before are gone now, but the overlay isn't being drawn. Here is additional code from my MapViewClass .m file as well as the MKOverlayRenderer subclass.

#import "MapOverlayRenderer.h"

 @implementation MapOverlayRenderer
-(void)drawMapRect:(MKMapRect)mapRect zoomScale:(MKZoomScale)zoomScale inContext:(CGContextRef)context{
CGRect rect = [self rectForMapRect:mapRect];
CGContextSaveGState(context);
CGContextAddRect(context, rect);
CGContextSetFillColorWithColor(context, [UIColor blackColor].CGColor);
CGContextFillRect(context, rect);
CGContextRestoreGState(context);

[super drawMapRect:mapRect zoomScale: zoomScale inContext:context];
}
@end

MapViewClass .m

 - (void)viewDidLoad {
 mapView = [[MKMapView alloc]initWithFrame:CGRectMake(0, 75, 320, 493)];
 mapView.delegate = self;
 mapView.mapType = MKMapTypeStandard;
[self.view addSubview: mapView];
 MKMapRect worldRect = MKMapRectWorld;
 MKCoordinateRegion worldRegion =   MKCoordinateRegionForMapRect(worldRect);

  CLLocationCoordinate2D worldPoints[4];
//Creates corners of the whole map
  worldPoints[0] = CLLocationCoordinate2DMake(worldRegion.center.latitude +    worldRegion.span.latitudeDelta/2.0, worldRegion.center.longitude - worldRegion.span.longitudeDelta/2.0);
        worldPoints[1]= CLLocationCoordinate2DMake(worldRegion.center.latitude - worldRegion.span.latitudeDelta/2.0, worldRegion.center.longitude - worldRegion.span.longitudeDelta/2.0);
        worldPoints[2] = CLLocationCoordinate2DMake(worldRegion.center.latitude + worldRegion.span.latitudeDelta/2.0, worldRegion.center.longitude + worldRegion.span.longitudeDelta/2.0);
        worldPoints[3]= CLLocationCoordinate2DMake(worldRegion.center.latitude - worldRegion.span.latitudeDelta/2.0, worldRegion.center.longitude + worldRegion.span.longitudeDelta/2.0);

        MKPolygon *worldSquare = [MKPolygon polygonWithCoordinates:worldPoints count:4];

        [mapView addOverlay:worldSquare];
}

-(MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id<MKOverlay>)overlay{
if([overlay isKindOfClass:[MKPolygon class]]){
    MapOverlayRenderer *polygonView = [[MapOverlayRenderer alloc]initWithOverlay:overlay];

    return polygonView;
}
}
Chris Gong
  • 8,031
  • 4
  • 30
  • 51
  • Try swapping worldPoints[2] and [3]. Also see http://stackoverflow.com/questions/28721497/mkpolygon-to-cover-entire-earth. To draw a polygon, you could also just use the built-in MKPolygonRenderer (custom one not needed). –  Mar 21 '15 at 20:36
  • Thank you anna, this solved the problem, and I also added two more coordinates to my array. The link you sent me was helpful, and another link with your answer that was similar also helped as well. Here it is for others viewing. http://stackoverflow.com/questions/8679499/showing-specific-region-using-mapkit/8683569#8683569 – Chris Gong Mar 21 '15 at 22:29

1 Answers1

1

1.

you put the code into your ViewController -- the view controller isn't a map view though :)

Your VC may HAVE a map view but it doesn't ISNT a mapview

2.

BUT EVEN MORE CRITICAL:

the code doesn't belong in a mapView either. you try to use a map overlay. look into using MKOverlayRenderer


Thats what both error messages say basically:

  1. says your SELF(ViewController) doesn't have a method named rectForMapRect
  2. says your SUPER(UIViewController) doesn't have a method named drawMapRect

==> only a MKOverlayRenderer provides them (that is visible from the docs if you search for the two methods)

Community
  • 1
  • 1
Daij-Djan
  • 49,552
  • 17
  • 113
  • 135
  • Thank you for your answer! I have taken into account the use of MKOverlayRenderer in my code. However, and there are no errors but the overlay isn't being drawn. Please help if you can. – Chris Gong Mar 21 '15 at 19:01
  • looks much better ;) -- now try to put breakpoints at addOverlay, rendererForOverlay and drawMapRect to figure out what doesn't work – Daij-Djan Mar 21 '15 at 21:35
  • thanks for responding again! I decided to abandon the custom mkoverlayrenderer and just stuck with the built-in mkpolygonrenderer instead. The problem had to do with my coordinate array as addressed in anna's reply. – Chris Gong Mar 21 '15 at 22:29