[英]iOS MKMapView and MKPolyLine
I'm new to iOS programming and am not sure what is wrong with this code: 我是iOS编程的新手,不确定此代码出了什么问题:
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(). 在第一次调用NSLOG()时收到EXC_BAD_ACCESS错误。 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. 当您使用
%@
打印值时,NSLog会尝试将该参数用作对象指针,但这些是双浮点值。 Use the %f
to print doubles: 使用
%f
打印双打:
NSLog(@"%f", locations[count-1].latitude);
NSLog(@"%f", locations[count-1].longitude);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.