Reputation: 185
I'm new to iOS programming and am not sure what is wrong with this code:
CLLocationCoordinate2D *locations = malloc(sizeof(CLLocationCoordinate2D) * points.count/2);
int count = 0;
for (int i = 0; i < points.count; i++)
{
CLLocationCoordinate2D point = CLLocationCoordinate2DMake([[points objectAtIndex:i] doubleValue], [[points objectAtIndex:++i] doubleValue]);
// Fill the array.
locations[count] = point;
count++;
NSLog(@"%@", locations[count-1].latitude);
NSLog(@"%@", locations[count-1].longitude);
}
// Create the polyline based on the array of points.
MKPolyline *routeLine = [MKPolyline polylineWithCoordinates:locations count:points.count/2];
MKPolylineView *routeLineView = [[MKPolylineView alloc] initWithPolyline:routeLine];
routeLineView.fillColor = [UIColor blueColor];
routeLineView.strokeColor = [UIColor blueColor];
routeLineView.lineWidth = 5;
// Add overlay to map.
[mapOutlet addOverlay:routeLine];
[mapOutlet setVisibleMapRect:routeLine.boundingMapRect];
// clear the memory allocated earlier for the points.
free(locations);
I get an EXC_BAD_ACCESS error on the first call to NSLOG(). Any thoughts?
FYI: 'points' is an array of strings containing latitude and longitude values.
Upvotes: 1
Views: 2691
Reputation: 77191
When you use %@
to print a value, NSLog tries to use the argument as an object pointer but these are double float values. Use the %f
to print doubles:
NSLog(@"%f", locations[count-1].latitude);
NSLog(@"%f", locations[count-1].longitude);
Upvotes: 3