2015-09-22 1 views
0

Я добавляю 2 аннотации в mapview с помощью leftcalloutaccessoryview (с изображением) и rightcalloutacessoryview (с кнопкой раскрытия подробностей). Проблема в том, что она работает нормально, но только для одной аннотации, а не для обоих. Он показывает одну аннотацию с кнопкой раскрытия изображения и подробностей, которую можно прослушать, а вторая аннотация показывает только заголовок, изображение без кнопки раскрытия подробностей и его нельзя использовать. Вот мой код.Аннотации MapView, не отображающей кнопку «Изображения и детализация раскрытия» - iOS Mapkit Xcode 6 Objective C

-(void)viewDidLoad{ 

[super viewDidLoad]; 

myLocationManager=[[CLLocationManager alloc] init]; 
geocoder = [[CLGeocoder alloc] init]; 

myLocationManager.delegate=self; 
myLocationManager.desiredAccuracy=kCLLocationAccuracyNearestTenMeters; 
myLocationManager.distanceFilter=10.0; 
[myLocationManager startUpdatingLocation];  

mapView = [[MKMapView alloc]initWithFrame: 
      CGRectMake(10, 255, 300, 205)]; 
mapView.delegate = self;  
mapView.centerCoordinate = CLLocationCoordinate2DMake(37.32, -122.03);  
mapView.mapType = MKMapTypeStandard; 

[self startAddingAnnottaions]; 

[self.view addSubview:mapView]; 
} 

// startAddingAnnotations Теперь

-(void)startAddingAnnottaions{ 

//first annotation 
CLLocationCoordinate2D location; 
location.latitude = (double) 37.331768; 
location.longitude = (double) -122.020039; 
MapAnnotation *newAnnotation = [[MapAnnotation alloc] 
           initWithTitle:@"Apple Head quaters" subtitle:@"subtitle" andCoordinate:location]; 
[mapView addAnnotation:newAnnotation]; 

//second annotation 
CLLocationCoordinate2D location2; 
location2.latitude = (double) 37.35239; 
location2.longitude = (double) -122.025919; 
MapAnnotation *newAnnotation2 = [[MapAnnotation alloc] 
           initWithTitle:@"Test Annotation" subtitle:@"parse" andCoordinate:location2]; 
[mapView addAnnotation:newAnnotation2]; 
} 

// didAddAnnotationViews

- (void)mapView:(MKMapView *)mv didAddAnnotationViews:(NSArray *)views 
{ 
MKAnnotationView *annotationView = [views objectAtIndex:0]; 
id <MKAnnotation> mapPoint = [annotationView annotation]; 

annotationView.canShowCallout = YES; 
annotationView.calloutOffset = CGPointMake(0, 32); 

UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; 
annotationView.rightCalloutAccessoryView = rightButton; 

UIImageView *iconView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"orageSquare.png"]]; 
annotationView.leftCalloutAccessoryView = iconView; 

MKCoordinateRegion region; 
region.center = [mapPoint coordinate]; 
MKCoordinateSpan span; 
span.latitudeDelta = 0.0144927536; // 0.0144927536 is equivalent to 1 mile 
span.longitudeDelta = 0.0144927536; 
region.span = span; 

[mv setRegion:region animated:YES]; 
[mv selectAnnotation:mapPoint animated:YES]; 

} 

// выноска Аксессуар управления резьбовыми

-(void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control { 
id <MKAnnotation> annotation = [view annotation]; 

NSLog(@"Right Button Pressed"); 
[self performSegueWithIdentifier:@"mapToOtherInfo" sender:self]; 
} 

Что я делаю неправильно? Он должен содержать несколько аннотаций с кнопкой раскрытия изображения и подробностей. Почему он показывает только одну аннотацию с ними? иногда первый работает хорошо, иногда второй, но оба не работают хорошо вместе.

ответ

0

Вы должны установить leftCalloutAccessoryView и rightCalloutAccessoryView недвижимость в viewForAnnotation метод, а не didAddAnnotationViews. Затем вы можете показать обе аннотации.

enter image description here

пример:

- (MKAnnotationView *)mapView:(MKMapView*)mapView 
     viewForAnnotation:(id <MKAnnotation>)annotation 
{ 
    if (annotation == mapView.userLocation) { 
     return nil; 
    } 

    MKPinAnnotationView *annotationView; 
    NSString *identifier = @"Pin"; 
    annotationView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:identifier]; 
    if (nil == annotationView) { 
     annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier]; 
    } 

    UIImageView *iconView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"frame_blue.png"]]; 
    annotationView.leftCalloutAccessoryView = iconView; 

    annotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; 

    return annotaionView; 
} 
+0

Он работал и решить мою проблему. Большое спасибо. –

Смежные вопросы