4

I have a map view and there are 10 store locations which data comes via webservice. I just want to push to my detail view to show address, telephone and other informations of the clicked store.

I need to pass data to my detailview when user tapped or touch up inside to a annotation on mapkit. There are 10 annotations in my mapview and first I want to know, how can I understand or how can I get the annotationID of which annotation is clicked?

this is the method I return pins

 - (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.animatesDrop=YES;
     pinView.canShowCallout=YES;
     pinView.pinColor=MKPinAnnotationColorPurple;

     UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
     [rightButton setTitle:annotation.title forState:UIControlStateNormal];

     [rightButton addTarget:self
     action:@selector(showDetails:)
     forControlEvents:UIControlEventTouchUpInside];

     pinView.rightCalloutAccessoryView = rightButton;

    return pinView;
}
   /* and my action method for clicked or tapped annotation: */

 - (IBAction)showDetails:(id)sender{

      NSLog(@"Annotation Click");

      [[mtMap selectedAnnotations]objectAtIndex:0];
      magazaDetayViewController *detail = [[magazaDetayViewController 
      alloc]initWithNibName:@"magazaDetayViewController" bundle:nil];

      detail.sehir=@"";
      detail.magazaAdi=@"";
      detail.adres=@"";
      detail.telefon=@"";
      detail.fax=@"";
      [self.navigationController pushViewController:detail animated:YES];

 }

if i can just get the clicked annotation index no i can fill detail properties with my array. if this is impossible is there any other way to do it?

Dinesh Raja
  • 8,501
  • 5
  • 42
  • 81
ercan
  • 825
  • 3
  • 16
  • 27

3 Answers3

9

First in your annotaion view delegat make a button to go in detail view like bellow:

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

MKPinAnnotationView *mypin = [[MKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:@"current"];
mypin.pinColor = MKPinAnnotationColorPurple;
mypin.backgroundColor = [UIColor clearColor];
UIButton *goToDetail = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
mypin.rightCalloutAccessoryView = myBtn;
mypin.draggable = NO;
mypin.highlighted = YES;
mypin.animatesDrop = TRUE;
mypin.canShowCallout = YES;
return mypin;
}

Now use the following delegate whenever the button in annotationView will get tapped the following delegate will be called from where you can easly get which particular annotaion's button is tapped

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view 
 calloutAccessoryControlTapped:(UIControl *)control
{
annotation *annView = view.annotation;
detailedViewOfList *detailView = [[detailedViewOfList alloc]init];
detailView.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
detailView.address = annView.address;
detailView.phoneNumber = annView.phonenumber;
[self presentModalViewController:detailView animated:YES];
}

here annotaion is a class importing MKAnnotaion.h and address and phonenumber are properties of annotaion class you can make many more while the address and phoneNumber properties of detailView class are strong. So that you can pass values. Hope this will help you!

superGokuN
  • 1,399
  • 13
  • 28
  • Thnx for detailed answer i have one question: where and how i fill or set annView.address? – ercan May 22 '12 at 09:34
  • how are you getting your annotations on map? – superGokuN May 22 '12 at 09:43
  • in my viewdidload method for(int i=0; i<[arrLat count];i++) {CLLocationCoordinate2D theCoordinate1; konumpin* myAnnotation1=[[konumpin alloc] init]; theCoordinate1.latitude = [[arrLong objectAtIndex:i] doubleValue]; theCoordinate1.longitude = [[arrLat objectAtIndex:i] doubleValue]; myAnnotation1.coordinate=theCoordinate1; myAnnotation1.title=[arrMagaza objectAtIndex:i]; [annotations addObject:myAnnotation1]; } [mtMap addAnnotations:annotations]; – ercan May 22 '12 at 10:20
  • your konumpin class is my annotation class now as you have set NSString title in konumpin class like that set some more NSString which you would like to show in your description class ex NSString *phoneNumber and make it property as title. now import your konumpin class in your description class and make an object of it and set property for it @property (nonatomic, strong) konumpin *descriptionAnnote; now in the above delegate make object of your description class and then do this descriptionClass.descriptionAnnote = view.annotation; this will pass your annotion whose button is clicked to your – superGokuN May 22 '12 at 10:29
  • description class. if you want i can send you a demo project give me your email id – superGokuN May 22 '12 at 10:30
  • it will be very nice if you can send me a demo because it is a little bit confusing for me. my email is ercaneraslan@gmail.com. thanks – ercan May 22 '12 at 10:55
  • hey bro i sent you a demo enjoy – superGokuN May 22 '12 at 11:39
  • sorry it took time cause i made it from scratch if you stuck any where happy to help you – superGokuN May 22 '12 at 11:40
0

Bellow is Delegate method of MapView......

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{

}

when user tap annotation pin above method call automaticaly if method define in .m file nut first declare MKMapViewDelegate delegate in .h file

and also you can get title or subtitle and id from google place API.... link is... https://developers.google.com/maps/documentation/places/

Paras Joshi
  • 20,427
  • 11
  • 57
  • 70
  • thanks for the answer. i am very new to ios so please could you tell me how can i set detail.address for example is there any sample app or code for this callout method – ercan May 22 '12 at 08:56
  • sure dude,http://mithin.in/2009/06/22/using-iphone-sdk-mapkit-framework-a-tutorial this link only return subtite and title on this link but you can use above delegate method insted of view title and subtitle... – Paras Joshi May 22 '12 at 09:02
0
for(int i=0; i<[arrLat count];i++) {

CLLocationCoordinate2D theCoordinate1; 
konumpin* myAnnotation1=[[konumpin alloc] init]; 

theCoordinate1.latitude = [[arrLong objectAtIndex:i] doubleValue]; 

theCoordinate1.longitude = [[arrLat objectAtIndex:i] doubleValue]; 

myAnnotation1.coordinate=theCoordinate1; 
myAnnotation1.title=[arrMagaza objectAtIndex:i]; 
[annotations addObject:myAnnotation1]; 

} 

[mtMap addAnnotations:annotations];
ProgramFOX
  • 6,131
  • 11
  • 45
  • 51
oscar castellon
  • 3,048
  • 30
  • 19