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 7804657
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T01:59:11+00:00 2026-06-02T01:59:11+00:00

I’ve been looking at Apple’s iOS Class Reference documentation, and am unfortunately none the

  • 0

I’ve been looking at Apple’s iOS Class Reference documentation, and am unfortunately none the wiser. I have downloaded their sample code KMLViewer but they’ve overcomplicated it… All I really want to know is how to generate a path and add it to the MKMapView. The documentation talks of using a CGPathRef, but doesn’t really explain how.

  • 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-02T01:59:13+00:00Added an answer on June 2, 2026 at 1:59 am

    Here’s how to generate a path and add it as an overlay to an MKMapView. I’m going to use an MKPolylineView, which is a subclass of MKOverlayPathView and shields you from having to refer to any CGPath since you instead create an MKPolyline (containing the data of the path) and use that to create the MKPolylineView (the visual representation of the data on the map).

    The MKPolyline has to be created with a C array of points (MKMapPoint), or a C array of coordinates (CLLocationCoordinate2D). It’s a shame that MapKit doesn’t use more advanced data structures such as NSArray, but so be it! I’m going to assume that you have an NSArray or NSMutableArray of CLLocation objects to demonstrate how to convert to a C array of data suitable for the MKPolyline. This array is called locations and how you fill it would be determined by your app – e.g. filled in by processing touch locations by the user, or filled with data downloaded from a web service etc.

    In the view controller that is in charge of the MKMapView:

    int numPoints = [locations count];
    if (numPoints > 1)
    {
        CLLocationCoordinate2D* coords = malloc(numPoints * sizeof(CLLocationCoordinate2D));
        for (int i = 0; i < numPoints; i++)
        {
             CLLocation* current = [locations objectAtIndex:i];
             coords[i] = current.coordinate;
        }
    
        self.polyline = [MKPolyline polylineWithCoordinates:coords count:numPoints];
        free(coords);
    
        [mapView addOverlay:self.polyline];
        [mapView setNeedsDisplay];
    }
    

    Note that self.polyline is declared in the .h as:

    @property (nonatomic, retain) MKPolyline* polyline;
    

    This view controller should also implement the MKMapViewDelegate method:

    - (MKOverlayView*)mapView:(MKMapView*)theMapView viewForOverlay:(id <MKOverlay>)overlay
    {
        MKPolylineView* lineView = [[[MKPolylineView alloc] initWithPolyline:self.polyline] autorelease];
        lineView.fillColor = [UIColor whiteColor];
        lineView.strokeColor = [UIColor whiteColor];
        lineView.lineWidth = 4;
        return lineView;
    }
    

    You can play with the fillColor, strokeColor and lineWidth properties to ensure that they are appropriate for your app. I’ve just gone with a simple, moderately wide plain white line here.

    If you want to remove the path from the map, e.g. to update it with some new coordinates, then you would do:

    [mapView removeOverlay:self.polyline];
    self.polyline = nil;
    

    and then repeat the above process to create a new MKPolyline and add it to the map.

    Although on first glance MapKit can look a bit scary and complex, it can be easy to do some things as illustrated in this example. The only scary bit – for non-C programmers at least – is the use of malloc to create a buffer, copy the CLLocationCoordinates into it using array syntax, and then freeing the memory buffer afterwards.

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

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
this is what i have right now Drawing an RSS feed into the php,
I am doing a simple coin flipping experiment for class that involves flipping a
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into
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

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.