1

ランダムな注釈にズームしようとしていますが、バブルも自動的に開きます。

次のように、viewDidLoad に注釈を固定しています。

...arrays...

    for (int i=0; i<22; i++){
    MKPointAnnotation *annot = [[MKPointAnnotation alloc] init];
    annot.title = [wineryName objectAtIndex:i];
    annot.subtitle = [wineryAddress objectAtIndex:i];
    annot.coordinate = CLLocationCoordinate2DMake([[lat objectAtIndex:i]doubleValue],             [[lon objectAtIndex:i]doubleValue]);
    [mapView setCenterCoordinate:annot.coordinate animated:YES];
    [mapView addAnnotation:annot];

次に、次のようにバブルをスタイリングします。

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{


if ([annotation isKindOfClass:[MKUserLocation class]])
    return nil;

//dequeue an existing pin view first
static NSString* AnnotationIdentifier = @"AnnotationIdentifier";
MKPinAnnotationView* pinView = [[[MKPinAnnotationView alloc]
                                 initWithAnnotation:annotation reuseIdentifier:AnnotationIdentifier] autorelease];
pinView.animatesDrop=YES;
pinView.canShowCallout=YES;
pinView.pinColor=MKPinAnnotationColorRed;


UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0, 0, 35, 35);
button.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
[button setImage:[UIImage imageNamed:@"RightArrow.png"] forState:UIControlStateNormal];
[button addTarget:self action:@selector(showDetails:) forControlEvents:UIControlEventTouchUpInside];
pinView.rightCalloutAccessoryView = button;

...arrays...

 for (int i = 0; i < 22; i++) {
        if ([wineryTitle[i] isEqualToString:[annotation title]]) {
            UIImageView *profileIconView = [[UIImageView alloc] init];
            profileIconView.frame = CGRectMake(0, 0, 40, 33);
            profileIconView.image = [UIImage imageNamed:wineryImage[i]];
            pinView.leftCalloutAccessoryView = profileIconView;
            [profileIconView release];
            break;


        }

}

return pinView;

}

次に、次のようにランダムな場所にズームしようとしています:

- (void)zoomToUserLocation:(MKUserLocation *)userLocation
{
if (!userLocation)
    return;

MKCoordinateRegion region;

//zoom to random pin when page loads

int randomNumber = rand() % 22;
switch (randomNumber) {
    case 1:
        region.center = CLLocationCoordinate2DMake(34.642109, -120.440292);

        [self.mapView selectAnnotation:[self.mapView.annotations objectAtIndex:0] animated:TRUE];
        break;
    case 2:
        region.center = CLLocationCoordinate2DMake(34.667408, -120.334781);

        [self.mapView selectAnnotation:[self.mapView.annotations objectAtIndex:1] animated:TRUE];
        break;
    case 3:
    ...etc

}
region.span = MKCoordinateSpanMake(5.0, 5.0);
region = [self.mapView regionThatFits:region];
[self.mapView setRegion:region animated:YES];

}

これはすべて機能します<例外: zoomToUserLocation メソッドでは、マップが 1 つの場所にズームされ、別の場所のバブルが表示されます。ランダム演算子が場所とバブルを別々にランダムに選択しているようです。ランダムに選択された同じ場所にバブルが自動的に表示されるように、これを修正する方法を知っている人はいますか?

4

1 に答える 1