简体   繁体   中英

Big problems with drawing/understanding a MKOverlayView on a MKMapView

I'M trying to use an overlay to draw a route on a MKMapView. This is my code:

Create the view (gets invoked upon a button press):

NSArray *route = [UCGeoConverter generateCoordinatesFromKML:self.kmlResponse];
    if (!self.routeView) {
        NSLog(@"create route");
        self.routeView = [[UCRouteView alloc] initWithRoute:route mapView:self.mapview];
        [self.mapview addOverlay:self.routeView];
    }

Now this is my UCRouteView.h file. It inherits from MKOverlayView and implements MKOverlay.

#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
#import "UCLocation.h"

@interface UCRouteView : MKOverlayView <MKOverlay>
{
    MKMapView* _mapView;
    NSArray* _points;
    UIColor* _lineColor;
}

-(id) initWithRoute:(NSArray*)routePoints mapView:(MKMapView*)mapView;

@property (strong, nonatomic) id delegate;
@property (nonatomic) MKCoordinateRegion region;
@property (nonatomic, retain) NSArray* points;
@property (nonatomic, retain) MKMapView* mapView;
@property (nonatomic, retain) UIColor* lineColor;
@end

Now for the implementation. Here I have big troubles. First off, it seems like the delegation does not work, as none of the methods get called (except the init.. obviously) I set the delegate to self. I have troubles understanding how this works, so I would appreciate if you guys could help me out here, it's pretty important! THANKS so much

#import "UCRouteView.h"

@implementation UCRouteView

@synthesize mapView     = _mapView;
@synthesize points      = _points;
@synthesize lineColor   = _lineColor; 
@synthesize delegate    = _delegate;
@synthesize region      = _region;

-(id) initWithRoute:(NSArray*)routePoints mapView:(MKMapView*)mapView
{
    self.delegate = self;
    self = [super initWithFrame:CGRectMake(0, 0, mapView.frame.size.width, mapView.frame.size.height)];

    [self setMapView:mapView];
    [self setPoints:routePoints];

    // determine the extents of the route points and zoom to that area. 
    CLLocationDegrees maxLat = -90;
    CLLocationDegrees maxLon = -180;
    CLLocationDegrees minLat = 90;
    CLLocationDegrees minLon = 180;

    for(int idx = 0; idx < self.points.count; idx++)
    {
        UCLocation* currentLocation = [self.points objectAtIndex:idx];
        if(currentLocation.coordinates.latitude > maxLat)
            maxLat = currentLocation.coordinates.latitude;
        if(currentLocation.coordinates.latitude < minLat)
            minLat = currentLocation.coordinates.latitude;
        if(currentLocation.coordinates.longitude > maxLon)
            maxLon = currentLocation.coordinates.longitude;
        if(currentLocation.coordinates.longitude < minLon)
            minLon = currentLocation.coordinates.longitude;
    }

    _region.center.latitude     = (maxLat + minLat) / 2;
    _region.center.longitude    = (maxLon + minLon) / 2;
    _region.span.latitudeDelta  = maxLat - minLat;
    _region.span.longitudeDelta = maxLon - minLon;

    [self.mapView setRegion:_region];
    return self;
}
- (MKMapRect)boundingMapRect
{
    // 1. Problem: this returns the MapRect where the overlay is in, right? 
    // So why not just return the MapRect which is visible right now?
    return self.mapView.visibleMapRect;
}

-(BOOL)canDrawMapRect:(MKMapRect)mapRect zoomScale:(MKZoomScale)zoomScale
{
   // 2. just return YES to make sure it draws
    return YES;
}

-(MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id<MKOverlay>)overlay
{
    // 3. this never gets called? what's wrong?
    NSLog(@"mapview viewForOverlay");
    return self;
}

-(void)drawMapRect:(MKMapRect)mapRect zoomScale:(MKZoomScale)zoomScale inContext:(CGContextRef)context
{    
    // does not get called either. I know the drawing code as is works to draw a route,
    // but I think I'm missing something here aswell..
    NSLog(@"draw route");
    // only draw lines if not in the middle of a transition and we 
    // acutally have some points to draw. 
    if(self.points && self.points.count > 0)
    {
        // set the color to draw the route
        if(nil == self.lineColor)
            self.lineColor = [UIColor blueColor];

        CGContextSetStrokeColorWithColor(context, self.lineColor.CGColor);
        CGContextSetRGBFillColor(context, 0.0, 0.0, 1.0, 1.0);

        // Draw them with a 2.0 stroke width
        CGContextSetLineWidth(context, 2.0);

        for(int idx = 0; idx < self.points.count; idx++)
        {
            UCLocation* location = [self.points objectAtIndex:idx];
            CGPoint point = [_mapView convertCoordinate:location.coordinates toPointToView:self];

            // set starting point and 'move' to it
            if(idx == 0)
            {
                // TODO: set special annotation for start/endpoint
                CGContextMoveToPoint(context, point.x, point.y);
            }
            else
            {
                CGContextAddLineToPoint(context, point.x, point.y);
            }
        }
        CGContextStrokePath(context);
    }
}
    @end

First off, you are attempting to call a setter on self before it has been set up (ie your self.delegate = self comes before [super init...] ). That won't work anywhere in any object.

More significantly, though, which delegate are you intending to set? Your UCRouteView class is a subclass of MKOverlayView , which doesn't have a delegate property (nor do its superclasses). Your methods, in fact, are from <MKMapViewDelegate> , suggesting that you intended to set the mapView 's delegate. So the proper call would be [[self mapView] setDelegate:self] (or, if you prefer dot syntax, self.mapView.delegate = self .

I haven't gone through the rest of your code in detail yet... see if making the above changes at least causes your methods to be called. (I'm not entirely sure your class architecture is optimal here, but we needn't deal with that at this stage.)

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