如何在mapView中添加折线

时间:2013-03-25 09:43:12

标签: iphone objective-c xcode cllocation polyline

我会在mapview中插入折线! 我这样做了

 CLLocationCoordinate2D coord[2];
coord[1].latitude = 45.42207;
coord[1].longitude = 9.123888;
coord[2].latitude = 45.422785;
coord[2].longitude = 9.12377;

MKPolyline *polyline = [[MKPolyline alloc] init];
polyline = [MKPolyline polylineWithCoordinates:coord count:2];

[self.mapView addAnnotation:polyline];

但是不起作用并且出现此错误:EXC_BAD_ACCESS。 怎么了?

2 个答案:

答案 0 :(得分:1)

C中的数组索引从索引0开始,而不是1:

CLLocationCoordinate2D coord[2];
coord[0].latitude = 45.42207;
coord[0].longitude = 9.123888;
coord[1].latitude = 45.422785;
coord[1].longitude = 9.12377;

答案 1 :(得分:0)

试试这个

CLLocationCoordinate2D coord[2];
coord[0].latitude = 45.42207;
coord[0].longitude = 9.123888;
coord[1].latitude = 45.422785;
coord[1].longitude = 9.12377;

MKPolyline *polyline = [[MKPolyline alloc] init];
polyline = [MKPolyline polylineWithCoordinates:coord count:2];


- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay {

    MKPolylineView *polyLineView = [[MKPolylineView alloc] initWithPolyline:polyline];
    polyLineView.fillColor = [UIColor blueColor];
    polyLineView.strokeColor = [UIColor greenColor];
    polyLineView.lineWidth = 7;
    return polyLineView;
}