将位置特定点击功能添加到didSelectAnnotation MapKit

时间:2012-08-17 13:17:46

标签: iphone parameters annotations mkmapview

我在mapView上添加了许多注释。这适用于下面的代码

float userLatitude = [[userDict objectForKey:@"lat"]floatValue];
float userLongitude = [[userDict objectForKey:@"long"]floatValue];
NSString *someString = [someArray objectAtIndex:i];
NSString *anotherString = [anotherArray objectAtIndex:i];

    CLLocationCoordinate2D loopCoord = {.latitude =  userLatitude, .longitude =  userLongitude};

    MapAnnotationViewController *addAnnotation = [[MapAnnotationViewController alloc] initWithCoordinate:loopCoord];
        self.localImage = [UIImage imageNamed:@"first.png"];
        [addAnnotation setTitle:someString];
        [addAnnotation setSubTitle:anotherString];
        [mainMapView addAnnotation:addAnnotation];

我基本上想要添加自定义触摸,即使在人触摸地图上的每个注释时触发也会触发。该方法需要从地图上的特定位置触发(不是标准的didSelect)如何将每个注释的参数标记或传递给委托方法:

-(void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view

我如何获得这样的代码:

UITapGestureRecognizer *singleFingerTap = 
  [[UITapGestureRecognizer alloc] initWithTarget:self 
                                          action:@selector(handleSingleTap:)];
[self.view addGestureRecognizer:singleFingerTap];
[singleFingerTap release];

//The event handling method
- (void)handleSingleTap:(UITapGestureRecognizer *)recognizer {
  CGPoint location = [recognizer locationInView:[recognizer.view superview]];

  //Do stuff here...
}

根据特定注释传递给didSelectAnnotationView

1 个答案:

答案 0 :(得分:1)

我认为传统的方法是创建一个实现MKAnnotation协议的类,并将自定义数据存储在那里。您需要将标题,副标题和坐标存储为标准,您可以添加touchSelector。然后当调用didSelect方法时,其中一个参数是MKAnnotationView,它具有一个名为annotation的属性。将其转换为您的自定义类,然后访问该选择器属性。

-(void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view
 {
   MyAnnotationClass *myAnno = (MyAnnotationClass *)view.annotation;
   //Do something with myAnno.touchSelector;
}

这是一个关于在您自己的MKAnnotaion兼容类中存储数据的问题: Store data in MKAnnotation?