2013-11-04 2 views
26

私はgoogleマップが最高の地図であることが知られていることを知っていますが、私は余分なライブラリやそのすべてをダウンロードする必要はありません。私は、ポイントAからBへの迅速なルートを取得し、それを完了するために何か素早く簡単にすることを好むでしょう。組み込みの関数/ライブラリでこれを行う方法はありますか?誰かが私を正しい方向に向けることができますか?は、Apple APIを使用してmkmapviewで方向を取得する手段ですか?

EDIT

私は私の場合には、ターン方向か何かで回し取得しようとしていないよ、私は最初から最後まで線を描画します。多分、どのルートを取るかについてのオプションを挙げることができます。それをする方法はありますか?

答えて

64

iOS 7では、MKDirectionsRequestを使用してルートを取得し表示することができます。ここで

は、別のマップアイテムに現在の場所から指示を表示するためのいくつかのサンプルコードです:

MKDirectionsRequest *request = [[MKDirectionsRequest alloc] init]; 
[request setSource:[MKMapItem mapItemForCurrentLocation]]; 
[request setDestination:myMapItem]; 
[request setTransportType:MKDirectionsTransportTypeAny]; // This can be limited to automobile and walking directions. 
[request setRequestsAlternateRoutes:YES]; // Gives you several route options. 
MKDirections *directions = [[MKDirections alloc] initWithRequest:request]; 
[directions calculateDirectionsWithCompletionHandler:^(MKDirectionsResponse *response, NSError *error) { 
    if (!error) { 
     for (MKRoute *route in [response routes]) { 
      [myMapView addOverlay:[route polyline] level:MKOverlayLevelAboveRoads]; // Draws the route above roads, but below labels. 
      // You can also get turn-by-turn steps, distance, advisory notices, ETA, etc by accessing various route properties. 
     } 
    } 
}]; 

あなたはiOSの7に新しいしている場合は、表示されるすべてのオーバーレイのためmapView:rendererForOverlay:メソッドを実装する必要があります。次のようなものがあります。

- (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id<MKOverlay>)overlay 
{ 
    if ([overlay isKindOfClass:[MKPolyline class]]) { 
     MKPolylineRenderer *renderer = [[MKPolylineRenderer alloc] initWithOverlay:overlay]; 
     [renderer setStrokeColor:[UIColor blueColor]]; 
     [renderer setLineWidth:5.0]; 
     return renderer; 
    } 
    return nil; 
} 
+0

そのデリゲートは 'MKMapItem'は座標スウィフトバージョンコアデータ。これらの座標を 'MKMapItem'に戻すにはどうしたらいいですか? – Adrian

+1

@AdrianB遅れて申し訳ありませんが、座標からインスタンス化できるMKPlacemarkを使用してMKMapItemを初期化できます。 – Kamaros

1

もう1つの可能性は、Apple Mapsアプリにアドレスを送信することです。私はちょうどこれがプロの設定で行われたことを見て、それが選択された方法でした。

-3

あなたがピンでタップすると、この作る警告ダイアログを表示したい場合:

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view  calloutAccessoryControlTapped:(UIControl *)control { 
[mapView deselectAnnotation:view.annotation animated:YES]; 

    if ([view.annotation isKindOfClass:[PinOfProject class]]) 
    { 
     CLLocationCoordinate2D coordinate = [view.annotation coordinate]; 
     MKPlacemark *placemark = [[MKPlacemark alloc] initWithCoordinate:coordinate addressDictionary:nil]; 
     MKMapItem *mapitem = [[MKMapItem alloc] initWithPlacemark:placemark]; 
     self.mapItem = mapitem; 

     CGPoint pin = [mapView convertCoordinate:view.annotation.coordinate toPointToView:self.view]; 
     CGRect rec = CGRectMake(pin.x-13, pin.y-14,view.frame.size.width,view.frame.size.height); 


     [self showAlertInformationForTrash:rec]; 

    } 
} 

-(void)showAlertInformationForTrash:(CGRect)rec{ 

    UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"Show Route?" delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:@"Route", @"Cancel", nil]; 
    actionSheet.tag = 1; 
    [actionSheet showFromRect:rec inView:self.view animated:YES]; 
} 

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex { 
    if (buttonIndex == 0) { 
     [self showRouteToAnnotation]; 
    } 
} 

-(void)showRouteToAnnotation{ 
    MKMapItem *myMapItem = self.mapItem; 
    MKDirectionsRequest *request = [[MKDirectionsRequest alloc] init]; 
    [request setSource:[MKMapItem mapItemForCurrentLocation]]; 
    [request setDestination:myMapItem]; 
    [request setTransportType:MKDirectionsTransportTypeAutomobile]; // This can be limited to automobile and walking directions. 
    [request setRequestsAlternateRoutes:NO]; // Gives you several route options. 
    MKDirections *directions = [[MKDirections alloc] initWithRequest:request]; 
    [directions calculateDirectionsWithCompletionHandler:^(MKDirectionsResponse *response, NSError *error) { 
     if (!error) { 
      for (MKRoute *route in [response routes]) { 
       [self.mapView addOverlay:[route polyline] level:MKOverlayLevelAboveRoads]; // Draws the route above roads, but below labels. 
       for (int i = 0; i < route.steps.count; i++) { 
        MKRouteStep *step = [route.steps objectAtIndex:i]; 
        NSString *newStep = step.instructions; 
        NSLog(@"%@", newStep); 
       }                  
      } 
     } 
    }]; 
} 

- (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id<MKOverlay>)overlay 
{ 
    if ([overlay isKindOfClass:[MKPolyline class]]) { 
     MKPolylineRenderer *renderer = [[MKPolylineRenderer alloc] initWithOverlay:overlay]; 
     [renderer setStrokeColor:[UIColor blueColor]]; 
     [renderer setLineWidth:5.0]; 
     return renderer; 
    } 
    return nil; 
} 
  • ああ、しかし、私は私の.h @property(強い、非アトミック)でプロパティを作ることに注意してくださいMKMapItem * mapItem;
2

 let request = MKDirectionsRequest(); 
     request.source = MKMapItem.mapItemForCurrentLocation(); 
     let locationPlacemark = MKPlacemark(coordinate: CLLocationCoordinate2DMake(13.724362, 100.515342), addressDictionary: nil); 
     request.destination = MKMapItem(placemark: locationPlacemark); 
     request.transportType = MKDirectionsTransportType.Any; 
     request.requestsAlternateRoutes = true; 
     let directions = MKDirections(request: request); 

     directions.calculateDirectionsWithCompletionHandler ({ 
      (response: MKDirectionsResponse?, error: NSError?) in 
      print(response?.description) 
      print(error?.description) 
      guard let response = response else { 
       //handle the error here 
       return; 
      } 
      self.myRoute = response.routes[0] 
      self.mkMapView.addOverlay(self.myRoute!.polyline) 
     }); 

と私はMKMapItem`sは `NSCoder`準拠していないので、私が保存されてきた`発見した

 func mapView(mapView: MKMapView, rendererForOverlay overlay: MKOverlay) -> MKOverlayRenderer { 
     let myLineRenderer = MKPolylineRenderer(polyline: (self.myRoute?.polyline)!) 
     myLineRenderer.strokeColor = UIColor.redColor() 
     myLineRenderer.lineWidth = 3 
     return myLineRenderer 
     } 
関連する問題