【发布时间】:2012-07-10 16:44:44
【问题描述】:
我正在尝试将 MKPolyLine 添加到我的 mapView。但出现错误:NSInvalidArgumentException', reason: '-[MKPolylineView boundingMapRect]
我的代码:
- (void)addRoad {
[self loadRoude];
[self.mapView addOverlay:self.routeLine];
}
- (void)loadRoude{
MKMapPoint northEastPoint = MKMapPointForCoordinate(self.mapView.userLocation.coordinate);
MKMapPoint southWestPoint = MKMapPointMake(52.142391, 21.055641);
MKMapPoint* pointArr = malloc(sizeof(CLLocationCoordinate2D) * 2);
pointArr[0] = northEastPoint;
pointArr[1] = southWestPoint;
self.routeLine = [MKPolyline polylineWithPoints:pointArr count:2];
_routeRect = MKMapRectMake(southWestPoint.x, southWestPoint.y, northEastPoint.x - southWestPoint.x, northEastPoint.y - southWestPoint.y);
free(pointArr);
}
- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay
{
MKOverlayView* overlayView = nil;
if(overlay == self.routeLine)
{
if(nil == self.routeLineView)
{
self.routeLineView = [[MKPolylineView alloc] initWithPolyline:self.routeLine];
self.routeLineView.fillColor = [UIColor redColor];
self.routeLineView.strokeColor = [UIColor redColor];
self.routeLineView.lineWidth = 3;
}
overlayView = self.routeLineView;
}
return overlayView;
}
有我的问题吗?
编辑
self.routeLineView 的 NSLog 显示:outeLiveView = <MKPolylineView: 0x7c956d0; frame = (0 0; 0 0); opaque = NO; layer = <MKOverlayClusterProxyLayer: 0x7c97eb0>> 这正常吗?
EDIT2
NSLog(@"pointArr[0]x = %f",pointArr[0].x);
NSLog(@"pointArr[0]y = %f",pointArr[0].y);
NSLog(@"pointArr[1]x = %f",pointArr[1].x);
NSLog(@"pointArr[1]y = %f",pointArr[1].y);
显示:
pointArr[0]x = 149891376.014222
pointArr[0]y = 88510604.996837
pointArr[1]x = 149917951.870020
pointArr[1]y = 88495551.883086
这意味着我的指针数组没有 nil 对象。我真的很震惊
【问题讨论】:
-
MKMapPointMake(52.142391, 21.055641)应该是MKMapPointForCoordinate(CLLocationCoordinate2DMake(52.142391, 21.055641))(因为 MKMapPoint 与纬度/经度坐标不同)但这不会导致您看到的异常。 -
@AnnaKarenina 是的,重写了,但仍然有同样的错误
-
@AnnaKarenina 添加了 nslog 的视图
-
出错后是否显示“无法识别的选择器”?
boundingMapRect是叠加层 (MKPolyline) 的属性,而不是叠加层视图 (MKPolylineView) 的属性。 routeLineView 属性是如何声明的,还有哪些其他代码使用它? -
@AnnaKarenina 是的,
boundingMapRect]: unrecognized selector sent to instance 0x7d55ff0'。routeLineView在loadView中声明为self.routeLineView = nil;。我上面写的所有使用routeLineView的代码。
标签: iphone objective-c ios mkmapview