地图首次加载时如何自动打开地图上的注释标注?
我正在开发一个基于 iPhone 的导航应用程序,它允许用户在地图上查看表格中的选择。我有一个注释可以精确定位用户在地图上选择的位置。按照正常行为,如果用户单击注释,则会出现一个标注,其中包含有关位置的详细信息。这里没有问题。
我的问题是,一旦用户被带到包含地图的屏幕,我希望标注自动显示在注释中,这样用户就不必单击注释来查看有关位置的详细信息,但我不知道该怎么做。我的“MapViewController”类中有以下方法,其中执行大部分地图显示工作:
- (void)viewDidLoad {
[super viewDidLoad];
MKCoordinateRegion region;
MKCoordinateSpan span;
NavButtonAppDelegate *delegate = [[UIApplication sharedApplication] delegate];
userCoord = delegate.userLocation.coordinate;
region.center = userCoord;
span.latitudeDelta = 0.4;
span.longitudeDelta = 0.4;
region.span = span;
[mapView setMapType:MKMapTypeStandard];
[mapView setZoomEnabled:YES];
[mapView setScrollEnabled:YES];
mapView.showsUserLocation = YES;
[mapView setRegion:region animated:YES];
RestaurantAnnotation *rAnnotation = [[RestaurantAnnotation alloc] init];
rAnnotation.title = restaurantObj.name;
rAnnotation.subtitle = restaurantObj.address;
rAnnotation.subtitle = [restaurantObj.address stringByAppendingFormat:@" %@", restaurantObj.hours];
rAnnotation.subtitle = [restaurantObj.address stringByAppendingFormat:@" %@", restaurantObj.phoneNumber];
CLLocationCoordinate2D newCoord = {restaurantObj.latitude, restaurantObj.longitude};
rAnnotation.coordinate = newCoord;
[mapView addAnnotation:rAnnotation];
}
从上一屏幕中的以下方法调用 MapViewController:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
Restaurant *rLocation = [restaurantList objectAtIndex:indexPath.row];
MapViewController *mapController = [[MapViewController alloc] initWithRestaurant:rLocation];
[self.navigationController pushViewController:mapController animated:YES];
[mapController release];
}
我意识到我必须使用以下方法来完成此操作:
- (void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views
{
id<MKAnnotation> myAnnotation = [mapView.annotations objectAtIndex:0];
[mapView selectAnnotation:myAnnotation animated:YES];
}
但是,我不确定如何。我没有同时使用很多注释,我只有一个需要使用它的注释。
我应该将此方法放在我的应用程序中的什么位置,以及从哪里调用它?< br> 我是否从 viewDidLoad 方法调用此方法并将实际方法放入我的 MapViewController 类中?
I have a navigation based application for the iPhone that I am working that allows the user to view a selection from a table, on a map. I have an annotation that pinpoints the user's selected location on the map. As per normal behaviour, if the user clicks on the annotation, a callout appears with the details about the location. No problems here.
My question is, I'd like the callout to appear automatically from the annotation, once the user is taken to the screen containing the map, so that the user does not have to click on the annotation in order to see the details about the location, but I'm not sure how to do this. I have the following method in my "MapViewController" class, where the bulk of the map display work is performed:
- (void)viewDidLoad {
[super viewDidLoad];
MKCoordinateRegion region;
MKCoordinateSpan span;
NavButtonAppDelegate *delegate = [[UIApplication sharedApplication] delegate];
userCoord = delegate.userLocation.coordinate;
region.center = userCoord;
span.latitudeDelta = 0.4;
span.longitudeDelta = 0.4;
region.span = span;
[mapView setMapType:MKMapTypeStandard];
[mapView setZoomEnabled:YES];
[mapView setScrollEnabled:YES];
mapView.showsUserLocation = YES;
[mapView setRegion:region animated:YES];
RestaurantAnnotation *rAnnotation = [[RestaurantAnnotation alloc] init];
rAnnotation.title = restaurantObj.name;
rAnnotation.subtitle = restaurantObj.address;
rAnnotation.subtitle = [restaurantObj.address stringByAppendingFormat:@" %@", restaurantObj.hours];
rAnnotation.subtitle = [restaurantObj.address stringByAppendingFormat:@" %@", restaurantObj.phoneNumber];
CLLocationCoordinate2D newCoord = {restaurantObj.latitude, restaurantObj.longitude};
rAnnotation.coordinate = newCoord;
[mapView addAnnotation:rAnnotation];
}
The MapViewController is called from the following method in the previous screen:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
Restaurant *rLocation = [restaurantList objectAtIndex:indexPath.row];
MapViewController *mapController = [[MapViewController alloc] initWithRestaurant:rLocation];
[self.navigationController pushViewController:mapController animated:YES];
[mapController release];
}
I realize that I have to use the following method to accomplish this:
- (void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views
{
id<MKAnnotation> myAnnotation = [mapView.annotations objectAtIndex:0];
[mapView selectAnnotation:myAnnotation animated:YES];
}
However, I'm not sure how. I don't have many annotations that I am using all at once, I only have one annotation that I need this to work with.
Where do I put this method in my app, and where do I call it from?
Do I call this method from the viewDidLoad method and put the actual method inside my MapViewController class?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您必须添加
到您设置为 MKMapView 委托的类。
You have to add
to the class that you set as the delegate for the MKMapView.