简体   繁体   中英

iOS MKMapView and MKPolyLine

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.

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM