How to get directions to a 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. } } }]; |
问题
问题是以后我似乎只能访问
所以问题是如何从
对于路线请求,
您可以使用
例如:
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是一种协议,没有什么说不能向支持模型添加属性的)。
1 2 3 4 | MKPointAnnotation *annotation = ...; CLPlacemark *placemark = ...; placemark.location.coordinate = annotation.coordinate; |
编辑:抱歉,我没有意识到