簡體   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