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

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

问题描述

我有一个基于导航的 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天全站免登陆