我正在使用MKMapView MKPolyline在地图上绘制叠加层。假设我有4个点(a,b,c,d)。 我试图在a-b,b-c,c-d之间显示叠加。我试图逐步显示这些叠加,这意味着我想首先显示a-b叠加然后b-c叠加和最后的c-d叠加。但是这些全部叠加都是单拍的。我如何防止这种情况,以便我可以逐个显示这些叠加?
修改
我要添加的其他功能:我添加了两个按钮“开始”和“暂停”。当我点击开始按钮时,它开始绘图,当我点击暂停按钮时,它应该在我们点击暂停按钮的某个时刻暂停。我们可以这样做吗?
答案 0 :(得分:1)
您可以在视图控制器中执行MKMapViewDelegate
协议,该协议可在添加叠加层时为您提供回调。您可以实现一个只绘制一个线段(两个点)的方法,并在每次通知您添加了另一个线路覆盖时触发该方法。
在视图控制器中声明一些实例变量:
@interface ViewController : UIViewController<MKMapViewDelegate> {
@private
MKMapView* mapView;
CLLocationCoordinate2D coordinates[4];
int lineNumber;
BOOL isPaused;
}
@property (strong, nonatomic) IBOutlet MKMapView *mapView;
然后在你的视图控制器.m文件中:
- (void)addNextLine {
if (!isPaused) {
// move to the next line
lineNumber++;
MKPolyline* line = [MKPolyline polylineWithCoordinates: &coordinates[lineNumber - 1]
count: 2];
[self.mapView addOverlay: line];
}
}
- (void)drawPolylines {
isPaused = NO;
// initialize the list of coordinates for the line segments
coordinates[0] = CLLocationCoordinate2DMake(47.8, -122.0);
coordinates[1] = CLLocationCoordinate2DMake(47.9, -122.0);
coordinates[2] = CLLocationCoordinate2DMake(47.9, -122.1);
coordinates[3] = CLLocationCoordinate2DMake(48.0, -122.1);
lineNumber = 0;
self.mapView.region = MKCoordinateRegionMake(coordinates[0], MKCoordinateSpanMake(0.2, 0.2));
// start adding lines one at a time
[self addNextLine];
}
// MKMapViewDelegate callback when an overlay has been added
- (void)mapView:(MKMapView *)theMap didAddOverlayViews:(NSArray *)overlayViews {
if (lineNumber < 3) {
// schedule the next line segment to be drawn after a 2 second delay:
[self performSelector: @selector(addNextLine) withObject:nil afterDelay: 2.0f];
}
}
// MKMapViewDelegate callback when an overlay's view needs to be generated
- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id < MKOverlay >)overlay {
if ([[overlay class] isSubclassOfClass: [MKPolyline class]]) {
MKPolylineView* view = [[MKPolylineView alloc] initWithPolyline: overlay];
view.strokeColor = [UIColor redColor];
view.lineWidth = 2;
view.fillColor = [UIColor redColor];
return view;
}
return nil;
}
您可以通过调用来启动整个过程:
[self drawPolylines];
在我的代码中,每行添加之间有2秒的延迟。如果您愿意,可以删除/更改。
修改:要使用按钮启动和暂停流程,请将UIButton
与这些操作相关联:
-(IBAction)onStart: (id)sender {
if (isPaused) {
isPaused = NO;
[self addNextLine];
} else {
[self drawPolylines];
}
}
-(IBAction)onPause: (id)sender {
isPaused = YES;
}