【问题标题】:How to get directions to a MKPointAnnotation如何获取到 MKPointAnnotation 的路线
【发布时间】:2014-05-06 16:41:48
【问题描述】:

我创建了一个具有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,但我需要访问CLPlacemark 上的setDestination MKDirectionsRequest

所以问题是我如何从MKPointAnnotation 获得CLPacemark,或者是否有不同的方法可以在没有该要求的情况下获得指向单点的方向?谢谢

【问题讨论】:

  • 在 geocodeAddressString 中,使用 this answer 而不是简单的 MKPointAnnotation 将注释创建为 MKPlacemarkMKPlacemark 也符合 MKAnnotation。然后,要获取路线,您可以将注释对象(这将是一个 MKPlacemark)传递给 MKMapItem 的 initWithPlacemark 方法。

标签: ios objective-c mkmapview mkmapitem mkpointannotation


【解决方案1】:

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

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

例如:

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

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

【讨论】:

  • 感谢@Anna 我在 CLPLacemark/MKPlacemark 混淆方面的错误
【解决方案2】:

MKPointAnnotation 会给你坐标,你可以把它放入CLPlacemarklocation.coordinate。我认为 MKPointAnnotation 中不会有任何其他现成的信息可以在 CLPlacemark 中使用。

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

placemark.location.coordinate = annotation.coordinate;

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

【讨论】:

  • 是的,您不能创建 CLPlacemarks...它们需要从 CLGeocoder 获取。原因是它们必须使用 MapKit 投影系统创建,因此地理编码实际上相当重要。
  • 是的,在看到@StuartM 的评论并了解了反向地理编码后意识到这一点。我几乎更喜欢一开始我错了的答案,因为这样我也学到了一些东西!
【解决方案3】:

您需要使用 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 是一个协议,没有什么说您不能将属性添加到支持模型)。

【讨论】:

  • 对于路线请求,MKMapItem 需要一个 MKPlacemark(不是 CLPlacemark)。您可以使用其 initWithCoordinate:addressDictionary: 方法直接从坐标创建 MKPlacemark。所以这种反向地理编码是不必要的。
  • 确认,你是对的,我把 CLPlacemark 和 MKPlacemark 弄糊涂了。它仍然需要进行反向地理编码,但看起来 MKPlacemark 会为您处理它。
  • 其实这样更好,因为 MKPlacemark 继承自 CLPlacemark,所以可以将 MKPlacemark 类型的注解对象直接传递给 MKMapItem 的 initWithPlacemark。
  • @Anna - 您能否按照您的建议提交答案,我可以接受。我可以按照你的建议使用[request setDestination:[[MKMapItem alloc] initWithPlacemark:[[MKPlacemark alloc] initWithCoordinate:pointAnnotation.coordinate addressDictionary:nil]]];。谢谢
猜你喜欢
  • 1970-01-01
  • 2019-07-15
  • 2016-04-08
  • 1970-01-01
  • 1970-01-01
  • 2016-08-03
  • 2016-05-09
  • 2020-04-02
  • 1970-01-01
相关资源
最近更新 更多