繁体   English   中英

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

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

我有一个基于导航的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];
}

但是,我不确定如何。 我没有很多我一次使用的注释,我只有一个注释,我需要这个注释。

我在哪里将此方法放在我的应用程序中,我从哪里调用它?
我是否从viewDidLoad方法调用此方法并将实际方法放在我的MapViewController类中?

你必须添加

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

到您设置为MKMapView的委托的类。

暂无
暂无

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

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