【发布时间】: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将注释创建为MKPlacemark。MKPlacemark也符合MKAnnotation。然后,要获取路线,您可以将注释对象(这将是一个 MKPlacemark)传递给 MKMapItem 的 initWithPlacemark 方法。
标签: ios objective-c mkmapview mkmapitem mkpointannotation