关于ios:如何获取指向MKPointAnnotation的路线

How to get directions to a MKPointAnnotation

我创建了一个地图,该地图的MKPointAnnotation是该地图上的单点(用户位置旁)。 我正在尝试找出如何修改现有代码的方法,以使驾驶指南到此为止。

这是我在应用程序早期使用的代码。 在应用程序的此较早点,我有以下内容为我提供了CLPlaceMark。

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

收集方向:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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,但需要访问MKDirectionsRequest上的setDestinationCLPlacemark

所以问题是如何从MKPointAnnotation中获取CLPacemark,或者有没有其他方法可以在没有此要求的情况下获得指向单个点的路线? 谢谢


对于路线请求,MKMapItem需要一个MKPlacemark(而不是CLPlacemark)。

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

例如:

1
2
3
4
5
MKPlacemark *mkDest = [[MKPlacemark alloc]
                          initWithCoordinate:pointAnnotation.coordinate
                           addressDictionary:nil];

[request setDestination:[[MKMapItem alloc] initWithPlacemark:mkDest]];


您需要使用MKPointAnnotation的坐标属性,然后使用反向地理编码通过CLGeocoder获取CLPlacemark。

编辑:一些示例代码。

1
2
3
4
5
6
7
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是一种协议,没有什么说不能向支持模型添加属性的)。


MKPointAnnotation将为您提供坐标,您可以将其放入CLPlacemarklocation.coordinate中。我认为MKPointAnnotation中不会存在任何其他可用的信息,这些信息可以在CLPlacemark中使用。

1
2
3
4
MKPointAnnotation *annotation = ...;
CLPlacemark *placemark = ...;

placemark.location.coordinate = annotation.coordinate;

编辑:抱歉,我没有意识到CLPlacemark在很大程度上是只读的。话虽如此,您可以在MKPointAnnotation的坐标上使用反向地理编码以获得CLPlacemark。此链接包含有关如何反向地理编码以从CLLocation获取CLPlacemark的信息(用您的注解.coordinate填充location.coordinate)以查找方向。