我已经创建了一个具有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,但需要访问MKDirectionsRequest上的setDestination的CLPlacemark。
因此,问题是我如何从一个CLPacemark获得一个MKPointAnnotation,或者,在没有这个要求的情况下,是否有一种不同的方法来获得指向某个点的方向?谢谢
发布于 2014-05-06 23:14:09
对于方向请求,MKMapItem需要一个MKPlacemark (而不是CLPlacemark)。
您可以使用MKPlacemark的initWithCoordinate:addressDictionary:方法直接从坐标创建它。
例如:
MKPlacemark *mkDest = [[MKPlacemark alloc]
initWithCoordinate:pointAnnotation.coordinate
addressDictionary:nil];
[request setDestination:[[MKMapItem alloc] initWithPlacemark:mkDest]];发布于 2014-05-06 16:49:55
MKPointAnnotation会给你坐标,你可以把它放进CLPlacemark的location.coordinate里。我不认为在MKPointAnnotation中会有任何其他可以在CLPlacemark中可用的信息。
MKPointAnnotation *annotation = ...;
CLPlacemark *placemark = ...;
placemark.location.coordinate = annotation.coordinate;编辑:对不起,我没有意识到CLPlacemark基本上是只读的。尽管如此,您可以在MKPointAnnotation的坐标上使用反向地理代码来获得CLPlacemark。这个链接有关于如何反转地理代码以从CLLocation (用annotation.coordinate填充location.coordinate )获取CLPlacemark以查找方向的信息。
发布于 2014-05-06 16:57:52
您需要使用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是一种协议,没有什么可以说明您不能向支持模型添加属性)。
https://stackoverflow.com/questions/23500339
复制相似问题