如何在地图首次加载时自动在地图上打开注释标注? [英] How to open callout of annotation on map automatically, when map first loads?

查看:182
本文介绍了如何在地图首次加载时自动在地图上打开注释标注?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用基于导航的iPhone应用程序,它允许用户在地图上查看表格中的选择。我有一个注释,可以精确定位用户在地图上的选定位置。按照正常行为,如果用户单击注释,则会显示一个标注,其中包含有关该位置的详细信息。这里没问题。

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.

我的问题是,一旦用户进入包含地图的屏幕,我希望标注自动从注释中出现,这样用户无需单击注释即可查看有关位置的详细信息,但我不确定如何执行此操作。我的MapViewController类中有以下方法,其中执行大部分地图显示工作:

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];

}

在上一个屏幕中通过以下方法调用MapViewController :

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.

我在哪里把这个方法放在我的应用程序中,我在哪里调用它?

我是否从viewDidLoad方法调用此方法并将实际方法放入我的MapViewController类中?

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?

推荐答案

你必须添加

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

到您设置为MKMapView委托的类。

to the class that you set as the delegate for the MKMapView.

这篇关于如何在地图首次加载时自动在地图上打开注释标注?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆