0

MKPointAnnotationマップ上の単一のポイント (ユーザーの場所以外)を持つマップを作成しました。この時点までの運転ルートを取得するために必要な既存のコードを修正する方法を見つけようとしています。

これは、アプリケーションの早い段階で使用するコードです。アプリケーションのこの初期の時点で、CLPlaceMark を提供する次のものがあります。

[geocoder geocodeAddressString:location
             completionHandler:^(NSArray* placemarks, NSError* error){
                 if (placemarks && placemarks.count > 0) {
                     CLPlacemark *topResult = [placemarks objectAtIndex:0];

道順の収集:

MKDirectionsRequest *request = [[MKDirectionsRequest alloc] init];
                         [request setSource:[MKMapItem mapItemForCurrentLocation]];
                         MKPlacemark *mkDest = [[MKPlacemark alloc] initWithPlacemark:topResult];
                         [request setDestination:[[MKMapItem alloc] initWithPlacemark:mkDest]];
                         [request setTransportType:MKDirectionsTransportTypeWalking]; // This can be limited to automobile and walking directions.
                         [request setRequestsAlternateRoutes:NO]; 
                         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.
                                     // You can also get turn-by-turn steps, distance, advisory notices, ETA, etc by accessing various route properties.
                                 }
                             }
                         }];

問題
問題は、後で にしかアクセスできないように見えることですself.mapView.annotations。にアクセスできますが、 の には にアクセスするMKPointAnnotation必要があります。CLPlacemarksetDestinationMKDirectionsRequest

CLPacemark問題は、からを取得するにはどうすればよいかMKPointAnnotation、またはその要件なしで単一のポイントへの道順を取得する別の方法はありますか? ありがとう

4

3 に答える 3

0

MKPointAnnotationに入れることができる座標が得られCLPlacemarkますlocation.coordinate。で使用できる MKPointAnnotation 内に、すぐに利用できる情報が他にあるとは思いませんCLPlacemark

MKPointAnnotation *annotation = ...;
CLPlacemark *placemark = ...;

placemark.location.coordinate = annotation.coordinate;

編集: 申し訳ありませんが、CLPlacemarks がほとんど読み取り専用であることに気づきませんでした。そうは言ってもMKPointAnnotationCLPlacemark. このリンクCLPlacemarkには、ルートを検索するためのリバース ジオコーディングCLLocation(location.coordinate に annotation.coordinate を入力) を取得する方法に関する情報が含まれています。

于 2014-05-06T16:49:55.073 に答える
0

MKPointAnnotation の座標プロパティを使用し、リバース ジオコードを使用して CLGeocoder 経由で CLPlacemark を取得する必要があります。

編集: いくつかのサンプル コード。

CLLocation *location = [[CLLocation alloc] initWithLatitude:annotation.coordinate.latitude longitude:annotation.coordinate.longitude];

CLGeocoder *geocoder = [CLGeocoder new];

[geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemark, NSError *error){
    // Grab the placemark   
}];

それ以外の場合は、必要に応じて注釈データソースで実行できる CLPlacemark をキャッシュする必要があります (MKAnnotation はプロトコルであることを思い出してください。バッキング モデルにプロパティを追加できないということは何もありません)。

于 2014-05-06T16:57:52.143 に答える