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

  • SEARCH
  • Home
  • 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 6182957
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T01:17:06+00:00 2026-05-24T01:17:06+00:00

I am using a custom MKOverlay to draw weather data over a MKMapView. The

  • 0

I am using a custom MKOverlay to draw weather data over a MKMapView. The drawing is being done in CoreGraphics. For this particular case, it is not sufficient to do the drawing in the drawMapRect:zoomScale:inContext: method because of how it handles tiling. I need the image entire to be drawn at once instead of tiled like the drawMapRect method.

Before, I had the radar image in a .gif so I just added an imageView to it and resize the imageView frame in drawMapRect.

My plan was to do something similar with this. Add a custom UIView and call setNeedsDisplay on it in drawMapRect.

Here is the relevant code.

The boundingMapRect property of the MKOverlay object:

- (MKMapRect)boundingMapRect
{
    CLLocationCoordinate2D upperLeftCoord = 
    CLLocationCoordinate2DMake(weatherData.radarArray.connectedRadar.latitude + 2.5,
                           weatherData.radarArray.connectedRadar.longitude - 2.5);

    MKMapPoint upperLeft = MKMapPointForCoordinate(upperLeftCoord);

    CLLocationCoordinate2D lowerRightCoord = 
    CLLocationCoordinate2DMake(weatherData.radarArray.connectedRadar.latitude - 2.5,
                           weatherData.radarArray.connectedRadar.longitude + 2.5);

    MKMapPoint lowerRight = MKMapPointForCoordinate(lowerRightCoord);

    double width = lowerRight.x - upperLeft.x;
    double height = lowerRight.y - upperLeft.y;

    MKMapRect bounds = MKMapRectMake(upperLeft.x, upperLeft.y, width, height);

    return bounds;
}

The working drawMapRect:zoomScale:inContext: code (that is too slow).

- (void)drawMapRect:(MKMapRect)mapRect zoomScale:(MKZoomScale)zoomScale inContext:(CGContextRef)context {

    int numPaths = parser.dataPaths.size();

    // We have to pad the map rect a lot to allow for visibility testing that works well.
    MKMapRect testMapRect = MKMapRectMake(mapRect.origin.x - 40000, mapRect.origin.y - 40000, mapRect.size.width + 40000, mapRect.size.height + 40000);;

    // Only draw inside the area we are suppose to
    //CGRect rect = [self rectForMapRect:mapRect];
    //CGContextClipToRect(context, rect);

    // How see through is the radar data. 1 = opaque, 0 = completely transparent
    CGContextSetAlpha(context, 1);
    for (int i = 0; i < numPaths; i++) {
        // Make sure the bin is actually visible in this region before drawing it
        if (MKMapRectContainsPoint(testMapRect, parser.dataPaths[i]->points[0]) ||
            MKMapRectContainsPoint(testMapRect, parser.dataPaths[i]->points[1]) ||
            MKMapRectContainsPoint(testMapRect, parser.dataPaths[i]->points[2]) ||
            MKMapRectContainsPoint(testMapRect, parser.dataPaths[i]->points[3])) {
            CGMutablePathRef path = CGPathCreateMutable();
            CGPoint currentP = [self pointForMapPoint:parser.dataPaths[i]->points[0]];
            CGContextBeginPath(context);
            CGPathMoveToPoint(path, NULL, currentP.x, currentP.y);
            currentP = [self pointForMapPoint:parser.dataPaths[i]->points[1]];
            CGPathAddLineToPoint(path, NULL, currentP.x, currentP.y);
            currentP = [self pointForMapPoint:parser.dataPaths[i]->points[2]];
            CGPathAddLineToPoint(path, NULL, currentP.x, currentP.y);
            currentP = [self pointForMapPoint:parser.dataPaths[i]->points[3]];
            CGPathAddLineToPoint(path, NULL, currentP.x, currentP.y);
            currentP = [self pointForMapPoint:parser.dataPaths[i]->points[0]];
            CGPathAddLineToPoint(path, NULL, currentP.x, currentP.y);
            CGPathCloseSubpath(path);
            CGContextSetFillColorWithColor(context, colors[parser.dataPaths[i]->dataVal]);
            CGContextAddPath(context, path);
            CGContextFillPath(context);
            CGPathRelease(path);
        }
}

The new drawMapRect:zoomScale:inContext: code

- (void)drawMapRect:(MKMapRect)mapRect zoomScale:(MKZoomScale)zoomScale inContext:(CGContextRef)context {

    // We have to pad the map rect a lot to allow for visibility testing that works well.
    radarImageView.testMapRect = MKMapRectMake(mapRect.origin.x - 40000, mapRect.origin.y - 40000, mapRect.size.width + 40000, mapRect.size.height + 40000);

    radarImageView.frame = [self rectForMapRect:self.overlay.boundingMapRect];
    [radarImageView setNeedsDisplay];

}

The drawRect method of the custom UIView.

- (void)drawRect:(CGRect)rect {


    CGContextRef context = UIGraphicsGetCurrentContext();

    int numPaths = parser.dataPaths.size();

    CGContextSetAlpha(context, 1);
    for (int i = 0; i < numPaths; i++) {

        // Make sure the bin is actually visible in this region before drawing it
        if (MKMapRectContainsPoint(testMapRect, parser.dataPaths[i]->points[0]) ||
            MKMapRectContainsPoint(testMapRect, parser.dataPaths[i]->points[1]) ||
            MKMapRectContainsPoint(testMapRect, parser.dataPaths[i]->points[2]) ||
            MKMapRectContainsPoint(testMapRect, parser.dataPaths[i]->points[3])) {

            CGMutablePathRef path = CGPathCreateMutable();
            CGPoint currentP = [(RadarImageOverlayView *)self.superview pointForMapPoint:parser.dataPaths[i]->points[0]];

            CGContextBeginPath(context);
            CGPathMoveToPoint(path, NULL, currentP.x, currentP.y);

            currentP = [(RadarImageOverlayView *)self.superview pointForMapPoint:parser.dataPaths[i]->points[1]];
            CGPathAddLineToPoint(path, NULL, currentP.x, currentP.y);

            currentP = [(RadarImageOverlayView *)self.superview pointForMapPoint:parser.dataPaths[i]->points[2]];
            CGPathAddLineToPoint(path, NULL, currentP.x, currentP.y);

            currentP = [(RadarImageOverlayView *)self.superview pointForMapPoint:parser.dataPaths[i]->points[3]];
            CGPathAddLineToPoint(path, NULL, currentP.x, currentP.y);

            currentP = [(RadarImageOverlayView *)self.superview pointForMapPoint:parser.dataPaths[i]->points[0]];
            CGPathAddLineToPoint(path, NULL, currentP.x, currentP.y);

            CGPathCloseSubpath(path);
            CGContextSetFillColorWithColor(context, colors[parser.dataPaths[i]->dataVal]);
            CGContextAddPath(context, path);
            CGContextFillPath(context);
            CGPathRelease(path);
        }
    }
}

Thanks!

EDIT

I am thinking that the problem has something to do with the context of the RadarImageView. Is there a problem with the way I am getting the context in the drawRect: method maybe?

  • 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-05-24T01:17:07+00:00Added an answer on May 24, 2026 at 1:17 am

    Couldn’t you prepare your path(s) before drawMapRect is called. For example when the visible region changes. You would just have to add the path to the drawing context in drawMapRect.I think maybe you could even prepare the paths for a given scale and then pan and scale the context (CGContextScaleCTM) when the region changes.

    If the data don’t change often. Another optimization would be to prepare images in png format for the lower zoom level as soon as you get the data. For the higher zoom level you could continue to draw like you do.

    To reduce the number of iteration you could use tiling with your data: instead of having one big array with all your data, you could have one array for each tile. In a first step, you retrieve the arrays corresponding to the tile intersecting the current visible region and then you would loop only on these arrays. Of course that would only work for higher zoom level.

    If you don’t want to optimize, you can improve the user experience for the cases where a lot of paths are displayed. To let the user interact with the map while building the paths, you should not process all the elements in one loop. You could process 1000 paths at a time then use performSelector:afterDelay: to delay processing of the next batch. That way you can display a progress bar and let the user interact with the map.

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

Sidebar

Related Questions

I've done this on android using custom layouts for menus. How do I create
I am using custom javascript functions provided at this link (http://km0.la/js/mozXPath/) to implement particular
I am using custom model binder in ASP.NET MVC 2 that looks like this:
I am displaying video over panel using custom allocator sample, for some file it
I am using custom permissions in my Django models like this: class T21Turma(models.Model): class
I am using custom UITableview concept to show data in cell of tableview. My
I'm having a problem using custom components at runtime. I have this custom FlowLayoutPanel
Currently I am using custom made Java search engine. This Java engine uses Lucene
I'm using custom widget in my crud form like this: {{=form.custom.begin}} {{if condition:}} {{=form.custom.widget.field1}}
I am using custom bullet points on a website. Now this site will also

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.