Search code examples
objective-cannotationsmkmapviewmkannotationview

How can i customize the MKMapview Annotation With Next and Previous Functionality?


enter image description here

HI I have requirement to Implement Next and Previous functionality in the MKMapAnnotationView as shown in the Image.I have got reference from Here for making custom annotation but how can i change the next or previous annotation by clicking Next and Previous button.Please provide some Hint or idea regarding this.I am stack at this point.


Solution

  • For that you can use the rightCalloutAccessoryView and leftCalloutAccessoryView.

    Here an example how to use the rightCalloutAccessoryView.

        -(MKAnnotationView*)mapView:(MKMapView *)mapView viewForAnnotation:  (id<MKAnnotation>)annotation {
    
     if ([annotation isKindOfClass: [MKUserLocation class]]) {
        return nil;
    }
    
    static NSString* AnnotationIdentifier = @"AnnotationIdentifier";
    MKPinAnnotationView *pinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationIdentifier];
    
    [pinView setCanShowCallout:YES];
    if([annotation isKindOfClass:[ASPinAnnotation class]])
    {
        [pinView setPinColor:MKPinAnnotationColorRed];
    }
    else if([annotation isKindOfClass:[ASAddAutomatPin class]])
    {
        UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
        [rightButton setTitle:[annotation title] forState:UIControlStateNormal];
        [pinView setRightCalloutAccessoryView:rightButton];
        [pinView setPinColor:MKPinAnnotationColorPurple];
    }
    

    And to get notified when a calloutAccessoryView is tapped you have to use this function in your delegate:

       -(void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
       {
    
    if([(UIButton*)control buttonType] == UIButtonTypeDetailDisclosure)
    {
        ASAddViewController *addController = [[ASAddViewController alloc] init];
        UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:addController];
        [addController setDelegate:self];
        [self.controller presentViewController:navController animated:YES completion:nil];
    }
    
    }