Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7941117
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T23:34:15+00:00 2026-06-03T23:34:15+00:00

I’M trying to use an overlay to draw a route on a MKMapView. This

  • 0

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T23:34:16+00:00Added an answer on June 3, 2026 at 11:34 pm

    First off, you are attempting to call a setter on self before it has been set up (i.e. 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.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I'm trying to create an if statement in PHP that prevents a single post
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.