LoginSignup
60
60

More than 5 years have passed since last update.

iOS 7から追加されたMKDirectionsで経路検索を試してみる

Posted at

この記事は iOS Second Stage Advent Calendar 2013 用にストックしておいたものです。すべて枠が埋まったようなので、通常記事として投稿します。


iOS 7 で追加された MKDirections が面白そうだったので、試しに六本木から渋谷へのルートを検索してみました。

まずは、出発点と到着点を CLLocationCoordinate2D で作成します。

// 六本木
CLLocationCoordinate2D fromCoordinate = CLLocationCoordinate2DMake(35.665213, 139.730011);

// 渋谷
CLLocationCoordinate2D toCoordinate = CLLocationCoordinate2DMake(35.658987, 139.702776);

ここで作成した CLLocationCoordinate2D をセットした MKPlacemark を生成します。

// CLLocationCoordinate2D から MKPlacemark を生成
MKPlacemark *fromPlacemark = [[MKPlacemark alloc] initWithCoordinate:fromCoordinate
                                                   addressDictionary:nil];
MKPlacemark *toPlacemark   = [[MKPlacemark alloc] initWithCoordinate:toCoordinate
                                                   addressDictionary:nil];

MKPlacemark から MKMapItem を生成します。

// MKPlacemark から MKMapItem を生成
MKMapItem *fromItem = [[MKMapItem alloc] initWithPlacemark:fromPlacemark];
MKMapItem *toItem   = [[MKMapItem alloc] initWithPlacemark:toPlacemark];

MKMapItem をセットして MKDirectionsRequest を生成します。

requestsAlternateRoutesYES を設定すると、複数のルートを検索できるようです。

// MKMapItem をセットして MKDirectionsRequest を生成
MKDirectionsRequest *request = [[MKDirectionsRequest alloc] init];
request.source = fromItem;
request.destination = toItem;
request.requestsAlternateRoutes = YES;

MKDirectionsRequest から MKDirections を生成します。

// MKDirectionsRequest から MKDirections を生成
MKDirections *directions = [[MKDirections alloc] initWithRequest:request];

ここでようやく経路検索を実行できます。

// 経路検索を実行
[directions calculateDirectionsWithCompletionHandler:^(MKDirectionsResponse *response, NSError *error)
 {
     if (error) return;

     if ([response.routes count] > 0)
     {
         MKRoute *route = [response.routes objectAtIndex:0];
         NSLog(@"distance: %.2f meter", route.distance);

         // 地図上にルートを描画
         [self.mapView addOverlay:route.polyline];
     }
 }];

ルート情報は MKRoute クラスで返ってくるようです。このクラスには経路情報の他、移動にかかる予測時間や距離なども含まれるようです。

最後に、地図上に表示されるルートの色や線の太さなどを設定します。

// 地図上に描画するルートの色などを指定(これを実装しないと何も表示されない)
- (MKOverlayRenderer *)mapView:(MKMapView *)mapView
            rendererForOverlay:(id<MKOverlay>)overlay
{
    if ([overlay isKindOfClass:[MKPolyline class]])
    {
        MKPolyline *route = overlay;
        MKPolylineRenderer *routeRenderer = [[MKPolylineRenderer alloc] initWithPolyline:route];
        routeRenderer.lineWidth = 5.0;
        routeRenderer.strokeColor = [UIColor redColor];
        return routeRenderer;
    }
    else {
        return nil;
    }
}

実行結果

▼こちらは複数のルートを検索してみた結果

今回は特に指定していませんが、経路検索条件に「徒歩」や「自動車」などの指定もできるようです。

サンプルソース

[https://github.com/koogawa/MKDirectionsSample]

60
60
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
60
60