首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何获得到MKPointAnnotation的方向

如何获得到MKPointAnnotation的方向
EN

Stack Overflow用户
提问于 2014-05-06 16:41:48
回答 3查看 2.4K关注 0票数 0

我已经创建了一个具有MKPointAnnotation的地图,它是地图上的一个点(除了用户位置)。我正在努力想办法修改一些现有的代码,我要把驾驶方向弄到这一点。

这是我在应用程序早期使用的代码。在应用程序的前面这一点上,我有以下内容,它给了我一个CLPlaceMark。

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

收集指示:

代码语言:javascript
复制
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

因此,问题是我如何从一个CLPacemark获得一个MKPointAnnotation,或者,在没有这个要求的情况下,是否有一种不同的方法来获得指向某个点的方向?谢谢

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2014-05-06 23:14:09

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

您可以使用MKPlacemarkinitWithCoordinate:addressDictionary:方法直接从坐标创建它。

例如:

代码语言:javascript
复制
MKPlacemark *mkDest = [[MKPlacemark alloc] 
                          initWithCoordinate:pointAnnotation.coordinate 
                           addressDictionary:nil];

[request setDestination:[[MKMapItem alloc] initWithPlacemark:mkDest]];
票数 6
EN

Stack Overflow用户

发布于 2014-05-06 16:49:55

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

代码语言:javascript
复制
MKPointAnnotation *annotation = ...;
CLPlacemark *placemark = ...;

placemark.location.coordinate = annotation.coordinate;

编辑:对不起,我没有意识到CLPlacemark基本上是只读的。尽管如此,您可以在MKPointAnnotation的坐标上使用反向地理代码来获得CLPlacemark这个链接有关于如何反转地理代码以从CLLocation (用annotation.coordinate填充location.coordinate )获取CLPlacemark以查找方向的信息。

票数 0
EN

Stack Overflow用户

发布于 2014-05-06 16:57:52

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

编辑:一些示例代码。

代码语言:javascript
复制
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是一种协议,没有什么可以说明您不能向支持模型添加属性)。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23500339

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档