Ios 如何获取指向MKPointAnnotation的方向
我创建了一个地图,它有一个Ios 如何获取指向MKPointAnnotation的方向,ios,objective-c,mkmapview,mkmapitem,mkpointannotation,Ios,Objective C,Mkmapview,Mkmapitem,Mkpointannotation,我创建了一个地图,它有一个MKPointAnnotation,它是地图上的一个点(除了用户位置)。我正在努力解决如何修改一些现有的法规,我必须得到到这一点的驾驶方向 这是我在应用程序早期使用的代码。在应用程序的前面这一点上,我有以下内容,这给了我一个CLPlaceMark [geocoder geocodeAddressString:location completionHandler:^(NSArray* placemarks, NSError* error){
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
,但我需要访问mkdirectionrequest
上setDestination
的CLPlacemark
因此,问题是如何从
MKPointAnnotation
中获取CLPacemark
,或者是否有一种不同的方法来获取单个点的方向,而不需要该要求?谢谢MKPointAnnotation
将为您提供坐标,您可以将坐标放入CLPlacemark
的位置。坐标
。我认为MKPointAnnotation中不会有任何其他现成的信息可以在CLPlacemark
中使用
MKPointAnnotation *annotation = ...;
CLPlacemark *placemark = ...;
placemark.location.coordinate = annotation.coordinate;
编辑:抱歉,我没有意识到
CLPlacemark
s基本上是只读的。也就是说,您可以在MKPointAnnotation
的坐标上使用反向地理编码,以获得CLPlacemark
。包含有关如何反转地理编码以从CLLocation
(使用注释填充location.coordinate)获取CLPlacemark
以查找方向的信息。MKPointAnnotation
将为您提供坐标,您可以将其放入CLPlacemark
的位置。坐标中。我认为MKPointAnnotation中不会有任何其他现成的信息可以在CLPlacemark
中使用
MKPointAnnotation *annotation = ...;
CLPlacemark *placemark = ...;
placemark.location.coordinate = annotation.coordinate;
编辑:抱歉,我没有意识到CLPlacemark
s基本上是只读的。也就是说,您可以在MKPointAnnotation
的坐标上使用反向地理编码,以获得CLPlacemark
。包含有关如何使用反向地理编码从CLLocation
获取CLPlacemark
以查找方向的信息(将location.coordinate与注释.coordinate一起填充)。您需要使用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是一个协议,没有任何规定您不能向备份模型添加属性) 您需要使用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
例如:
MKPlacemark *mkDest = [[MKPlacemark alloc]
initWithCoordinate:pointAnnotation.coordinate
addressDictionary:nil];
[request setDestination:[[MKMapItem alloc] initWithPlacemark:mkDest]];
对于方向请求,MKMapItem
需要一个MKPlacemark
(而不是CLPlacemark
)
您可以使用其initWithCoordinate:addressDictionary:
方法直接从坐标创建MKPlacemark
例如:
MKPlacemark *mkDest = [[MKPlacemark alloc]
initWithCoordinate:pointAnnotation.coordinate
addressDictionary:nil];
[request setDestination:[[MKMapItem alloc] initWithPlacemark:mkDest]];
在geocodeAddressString中,使用而不是简单的MKPointAnnotation
将注释创建为MKPlacemark
MKPlacemark
也符合MKAnnotation
。然后,要获取方向,可以将注释对象(将是MKPlacemark)传递给MKMapItem的initWithPlacemark方法。在geocodeAddressString中,使用而不是简单的MKPointAnnotation
将注释创建为MKPlacemark
MKPlacemark
也符合MKAnnotation
。然后,要获取方向,可以将注释对象(将是一个MKPlacemark)传递给MKMapItem的initWithPlacemark方法。是的,您不能创建CLPlacemarks…它们需要从CLGeocoder获取。原因是它们必须使用MapKit投影系统创建,因此地理编码实际上相当重要。是的,在看到@StuartM的评论并了解了反向地理编码后,意识到了这一点。我几乎更喜欢一开始我错了的答案,因为后来我也学到了一些东西!是的,你不能创建CLPlacemarks…它们需要从CLGeocoder那里获得。原因是它们必须使用MapKit投影系统创建,因此地理编码实际上相当重要。是的,在看到@StuartM的评论并了解了反向地理编码后,意识到了这一点。我几乎更喜欢一开始我错了的答案,因为后来我也学到了一些东西!对于方向请求,MKMapItem需要MKPlacemark(而不是CLPlacemark)。可以使用其initWithCoordinate:addressDictionary:方法直接从坐标创建MKPlacemark。所以这种反向地理编码是不必要的。确认,你是对的,我把CLPlacemark和MKPlacemark搞混了。它仍然需要进行反向地理编码,但看起来MKPlacemark会为您处理它。实际上,它更好,因为MKPlacemark继承自CLPlacemark,所以您可以将MKPlacemark类型的注释对象直接传递给MKMapItem的initWithPlacemark。@Anna-您能按您的建议提交一个答案吗?请接受。我可以使用[requues