できることは、View Controller でMKMapViewDelegate
プロトコルを実装することです。これにより、オーバーレイが追加されたときにコールバックが提供されます。1 つの線分 (2 つの点) を描画するだけのメソッドを実装し、別の線オーバーレイが追加されたことを通知されるたびにそのメソッドをトリガーできます。
ビュー コントローラーでいくつかのインスタンス変数を宣言します。
@interface ViewController : UIViewController<MKMapViewDelegate> {
@private
MKMapView* mapView;
CLLocationCoordinate2D coordinates[4];
int lineNumber;
BOOL isPaused;
}
@property (strong, nonatomic) IBOutlet MKMapView *mapView;
次に、View Controller .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;
}