想在MapView中添加Polyline。代碼如下:
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.
試試:
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;
}