【问题标题】:How to open callout of annotation on map automatically, when map first loads?首次加载地图时,如何自动打开地图上的注释标注?
【发布时间】:2011-06-23 20:34:49
【问题描述】:

我有一个基于导航的 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 类中?

【问题讨论】:

    标签: iphone objective-c annotations mapkit callouts


    【解决方案1】:

    你必须添加

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

    到您设置为 MKMapView 委托的类。

    【讨论】:

    • 非常感谢您的及时回复。然后我会从 viewDidLoad 方法调用该方法吗?
    • 不,您根本不必直接调用它。只要您设置了委托,它就会被地图视图自动调用。
    • 非常感谢您的回答。你的解决方案奏效了!保重。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-21
    • 1970-01-01
    • 1970-01-01
    • 2021-02-01
    • 2012-02-27
    • 1970-01-01
    相关资源
    最近更新 更多