添加位置具体点击功能didSelectAnnotation MapKit

问题描述:

我有一些注释我放在mapView。这工作得很好下面添加位置具体点击功能didSelectAnnotation MapKit

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

+0

你就不能访问该didSelectAnnotationView使用view.annotation注释? – Anna 2012-08-17 13:27:56

+0

是的,但你会添加轻敲手势识别器到annotationClass呢?你会怎么做,所以它是特定于每个注释 – Eric 2012-08-17 13:30:11

+0

为什么你需要手势识别器?委托方法将在没有它的情况下被调用。 – Anna 2012-08-17 13:34:24

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

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

下面是关于你自己的MKAnnotaion兼容类存储数据的SO问题: Store data in MKAnnotation?