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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T20:49:29+00:00 2026-06-09T20:49:29+00:00

I am implementing an iOS application, and I want to draw a polyline between

  • 0

I am implementing an iOS application, and I want to draw a polyline between several given coordinates on the map.

I wrote the code and got the polylines being drawn from my point reaching an infinite point. In other words the beginning point of the line starts from the my given lat and long point, but the end of the line is infinite and not the other point.

This is my code…

I filled the coordinates in a NSMutableArray called routeLatitudes. The array cells are being filled one for latitude and one for longitude.

MKMapPoint* pointArr = malloc(sizeof(CLLocationCoordinate2D) * [routeLatitudes count]); 

for(int idx = 0; idx < [routeLatitudes count]; idx=idx+2)
{
    CLLocationCoordinate2D workingCoordinate;       
    workingCoordinate.latitude=[[routeLatitudes objectAtIndex:idx] doubleValue];
    workingCoordinate.longitude=[[routeLatitudes objectAtIndex:idx+1] doubleValue];  
    MKMapPoint point = MKMapPointForCoordinate(workingCoordinate);
    pointArr[idx] = point;      
}   

// create the polyline based on the array of points. 
routeLine = [MKPolyline polylineWithPoints:pointArr count:[routeLatitudes count]];
[mapView addOverlay:self.routeLine];
free(pointArr);

and overlay delegate

- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay
{
   MKOverlayView* overlayView = nil;

  if(overlay == routeLine)
  {
    self.routeLineView = [[[MKPolylineView alloc] initWithPolyline:self.routeLine]        autorelease];
    self.routeLineView.fillColor = [UIColor colorWithRed:51 green:51 blue:255  alpha:1];
    self.routeLineView.strokeColor = [UIColor colorWithRed:204 green:0 blue:0 alpha:1];
    self.routeLineView.lineWidth = 3;

    overlayView = routeLineView;
  }
return overlayView;
}

So I need the lines to be drawn between the points over the map. The beginning of the line is the first dropped pin, and the end is on the last dropped pin.

  • 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-09T20:49:30+00:00Added an answer on June 9, 2026 at 8:49 pm

    According to the code, the routeLatitudes array has objects listed like this:

    index 0: latitude for point 1
    index 1: longitude for point 1
    index 2: latitude for point 2
    index 3: longitude for point 2
    index 4: latitude for point 3
    index 5: longitude for point 3
    …

    So if routeLatitudes.count is 6, it actually has only 3 points.

    This means the malloc is allocating the wrong number of points and the polylineWithPoints call is also specifying the wrong number of points for the overlay.

    The other problem is that since pointArr will contain only half the objects that routeLatitudes has, you can’t use the same index value for both arrays.

    The for loop index counter idx is being incremented by 2 at each iteration because that’s how the routeLatitudes points are layed out but then the same idx value is used to set pointArr.

    So for idx=0, pointArr[0] is set but then for idx=2, pointArr[2] is set (instead of pointArr[1]), and so on. This means every other position in pointArr is left uninitialized resulting in the lines “going to infinity”.

    So the corrected code might look like this:

    int pointCount = [routeLatitudes count] / 2;
    MKMapPoint* pointArr = malloc(sizeof(MKMapPoint) * pointCount);
    
    int pointArrIndex = 0;  //it's simpler to keep a separate index for pointArr
    for (int idx = 0; idx < [routeLatitudes count]; idx=idx+2)
    {
        CLLocationCoordinate2D workingCoordinate;       
        workingCoordinate.latitude=[[routeLatitudes objectAtIndex:idx] doubleValue];
        workingCoordinate.longitude=[[routeLatitudes objectAtIndex:idx+1] doubleValue];  
        MKMapPoint point = MKMapPointForCoordinate(workingCoordinate);
        pointArr[pointArrIndex] = point;
        pointArrIndex++;
    }   
    
    // create the polyline based on the array of points. 
    routeLine = [MKPolyline polylineWithPoints:pointArr count:pointCount];
    [mapView addOverlay:routeLine];
    free(pointArr); 
    

    Also note in the malloc line, I corrected sizeof(CLLocationCoordinate2D) to sizeof(MKMapPoint). This technically wasn’t causing a problem because those two structs happen to be the same length but it’s correct to use sizeof(MKMapPoint) since that’s what the array is going to contain.

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

Sidebar

Related Questions

I am implementing an iphone application (iOS 4.2) from where I would like to
I am developing an application for iOS 4.x devices. I am implementing a communication
I am an iOS development newbie. I am implementing some sample code, but try
I'm implementing an iOS application with the Facebook SDK which switches to the Safari
I am new to iOS development. Now I am implementing an UIPanGestureRecognizer model application.
I'm implementing a connection between ios devices by using GameKit's GKSession server and client
I'm implementing Apple's Reachibility class into my application. The app's base SDK is iOS
I am implementing one iPhone animal application. In which I want to add face
I am implementing an iOS app for a customer and i want to include
I am implementing a simple iOS solitaire game that allows the user to drag

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.