如何获取MKPointAnnotation的路线

Stu*_*rtM 0 objective-c mkmapview ios mkmapitem mkpointannotation

我创建了一个地图,MKPointAnnotation其中有一个地图上的单点(除了用户位置).我试图找出如何修改我现有的一些代码来获得这方面的驾驶方向.

这是我在应用程序早期使用的代码.在应用程序的早期阶段,我有以下内容,它给了我一个CLPlaceMark.

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

收集方向:

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.
                                 }
                             }
                         }];
Run Code Online (Sandbox Code Playgroud)

问题
问题是,以后我似乎只能访问self.mapView.annotations.所以,我有机会获得MKPointAnnotation,但我需要访问CLPlacemarksetDestinationMKDirectionsRequest.

所以,问题是如何得到一个CLPacemarkMKPointAnnotation,或有不同的方法来获得方向的单点没有这种要求?谢谢

小智 5

对于方向请求,MKMapItem需要a MKPlacemark(不是a CLPlacemark).

您可以MKPlacemark使用其initWithCoordinate:addressDictionary:方法直接从坐标创建.

例如:

MKPlacemark *mkDest = [[MKPlacemark alloc] 
                          initWithCoordinate:pointAnnotation.coordinate 
                           addressDictionary:nil];

[request setDestination:[[MKMapItem alloc] initWithPlacemark:mkDest]];
Run Code Online (Sandbox Code Playgroud)