在mapview上点击注释时,将数据传递给detailView

时间:2012-05-22 08:41:56

标签: iphone ios annotations mkmapview

我有一个地图视图,有10个商店位置,数据来自webservice。我只想推送到我的详细视图,以显示点击商店的地址,电话和其他信息。

当用户点击或触摸mapkit上的注释时,我需要将数据传递给我的detailview。我的mapview中有10个注释,首先我想知道,我如何理解或如何获得点击哪个注释的注释ID?

这是我返回引脚的方法

 - (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];

 }

如果我可以获得点击的注释索引,我可以使用我的数组填充详细信息属性。 如果这是不可能的,还有其他办法吗?

3 个答案:

答案 0 :(得分:9)

首先在你的注释视图中,委托制作一个按钮进入详细视图,如下:

-(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;
}

现在使用以下委托,只要annotationView中的按钮被点击,将调用以下委托,您可以从中轻松获取哪个特定的注释按钮

- (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];
}

这里annotaion是一个导入MKAnnotaion.h的类,地址和phonenumber是注释类的属性,你可以做更多,而detailView类的地址和phoneNumber属性很强。这样你就可以传递价值。希望这会对你有所帮助!

答案 1 :(得分:0)

Bellow是MapView ......

的委托方法
- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{

}

当用户点击方法调用上方的注释引脚时,如果方法在.m文件中定义,则首先在.h文件中声明MKMapViewDelegate委托

并且您还可以从Google地方API获取标题或副标题和ID .... 链接是...... https://developers.google.com/maps/documentation/places/

答案 2 :(得分: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];