6

私が作業している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];
}

しかし、どうすればよいかわかりません。一度に使用する注釈は多くありません。これを操作するために必要な注釈は 1 つだけです。

このメソッドをアプリのどこに配置し、どこから呼び出すのですか?
このメソッドを viewDidLoad メソッドから呼び出して、実際のメソッドを MapViewController クラス内に配置する必要がありますか?

4

1 に答える 1

12

追加する必要があります

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

MKMapView のデリゲートとして設定したクラスに。

于 2011-02-09T02:03:33.597 に答える