简体   繁体   English

如何在地图首次加载时自动打开地图上的注释标注?

[英]How to open callout of annotation on map automatically, when map first loads?

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. 我有一个基于导航的iPhone应用程序,我正在使用它,允许用户在地图上查看表格中的选择。 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: 我在“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];

}

The MapViewController is called from the following method in the previous screen: 从上一个屏幕中的以下方法调用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];
}

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? 我是否从viewDidLoad方法调用此方法并将实际方法放在我的MapViewController类中?

You have to add 你必须添加

- (void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views { 
    id myAnnotation = [mapView.annotations objectAtIndex:0]; 
    [mapView selectAnnotation:myAnnotation animated:YES]; 
}

to the class that you set as the delegate for the MKMapView. 到您设置为MKMapView的委托的类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM