Teitoku
Teitoku

Reputation: 21

How to detect another MKAnnotaion pin when touch

I have multiple annotation pin in map(place1,place2)

I want user touch pin to go to new Viewcontroller to get description of place

it have function to detect in pin to touch it to go to descriptionViewController?

This is my code

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.


MKCoordinateRegion place1 = { {0.0, 0.0} , {0.0, 0.0} };
place1.center.latitude = 34.7037755;
place1.center.longitude = 137.7345882;
place1.span.longitudeDelta = 0.02f;
place1.span.latitudeDelta = 0.02f;
[mapView setRegion:place1 animated:YES];

Newclass *ann1 = [[Newclass alloc] init];
ann1.title = @"Place1";
ann1.subtitle = @"subtitle";
ann1.coordinate = place1.center;
[mapView addAnnotation: ann1];

MKCoordinateRegion place2 = { {0.0, 0.0} , {0.0, 0.0} };
place2.center.latitude = 34.7024461;
place2.center.longitude = 137.7297572;
place2.span.longitudeDelta = 0.02f;
place2.span.latitudeDelta = 0.02f;
[mapView setRegion:place2 animated:YES];

Newclass *ann2 = [[Newclass alloc] init];
ann2.title = @"place2";
ann2.subtitle = @"subtitle";
ann2.coordinate = place2.center;
[mapView addAnnotation:ann2];


}

-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation{
MKPinAnnotationView *myPin = [[MKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:@"current"];
myPin.pinColor = MKPinAnnotationColorGreen;

UIButton *advertButtom = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[advertButtom addTarget:self action:@selector(button:) forControlEvents:UIControlEventTouchUpInside];

myPin.rightCalloutAccessoryView = advertButtom;
myPin.draggable =NO;
myPin.animatesDrop =true;
myPin.canShowCallout=YES;

return myPin;

}

-(void)button:(id)sender{
NSUserDefaults *defults = [NSUserDefaults standardUserDefaults];
UIStoryboard *storyboard = [self storyboard];
descriptionViewController *description = [storyboard instantiateViewControllerWithIdentifier:@"des"];
[self presentModalViewController:description animated:YES];


NSLog(@"touched");
NSLog(@"data %@");
}

Upvotes: 0

Views: 28

Answers (1)

Svetoslav Bramchev
Svetoslav Bramchev

Reputation: 433

You need to use mapView method

func mapView(mapView: MKMapView, didSelectAnnotationView view: MKAnnotationView) {
         NSUserDefaults *defults = [NSUserDefaults standardUserDefaults];
         UIStoryboard *storyboard = [self storyboard];
         descriptionViewController *description = [storyboard instantiateViewControllerWithIdentifier:@"des"];
         [self presentModalViewController:description animated:YES];


         NSLog(@"touched");
         NSLog(@"data %@");
    }

Upvotes: 1

Related Questions