单击两次注释并在第二次单击时执行操作

时间:2014-05-13 01:18:10

标签: ios xcode5 mkmapview

我有一个地图视图,我已经从JSON响应中绘制了坐标。它首次单击时显示引脚/注释名称。现在我需要以某种方式允许用户再次单击相同的pin / annotation并执行seque和一些动作/代码。我怎么能这样做?

1 个答案:

答案 0 :(得分:3)

以下是我在许多应用中实现此功能的方法......首先,我创建了自己的自定义注释,其中包含了一些我可能要存储的值,这些值对于与该引脚关联的每个对象都是唯一的。在这种情况下,它是一个URL。这是一个通用" CustomAnnotation"对象的.h和.m:

#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>

@interface CustomAnnotation : NSObject <MKAnnotation>

@property (nonatomic, assign) CLLocationCoordinate2D coordinate;
@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *subtitle;
@property (nonatomic, retain) NSString *URL;


@end

// the .m file doesn't need anything else in it

#import "CustomAnnotation.h"
#import <MapKit/MapKit.h>

@implementation CustomAnnotation

@end

然后在视图控制器中我添加了mapview,你需要添加注释,自定义点击它时显示的视图,并处理动作:

- (void)createMapAndAddAnnotation {
    // create the mapview and add it to the view
    MKMapView *mapView = [[MKMapView alloc] initWithFrame:self.view.frame];
    mapView.delegate = self;
    [self.view addSubview:mapView];

    // create the annotation however you want, this is just for demo purposes
    CustomAnnotation *annotation = [[CustomAnnotation alloc] init];
    annotation.coordinate = CLLocationCoordinate2DMake(50, 50);
    annotation.title = @"Test Title";
    annotation.subtitle = @"Subtitle";
    annotation.URL = @"www.google.com";
    [mapView addAnnotation:annotation];
}

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {
    // if the annotation is a custom annotation
    if ([annotation isKindOfClass:[CustomAnnotation class]]) {
        // create the reuse identifier so we can recycle annotatinons
        NSString *pinAnnotationViewReuseID = @"PinAnnotationView";

        // attempt to reuse one
        MKPinAnnotationView *pinView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:pinAnnotationViewReuseID];

        // if we were unable to dequeue one for reuse
        if (!pinView) {
            // create and format it accordingly
            pinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:pinAnnotationViewReuseID];
            pinView.pinColor = MKPinAnnotationColorRed;
            pinView.animatesDrop = YES;
            pinView.canShowCallout = YES;
            pinView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
        }

        // otherwise all we want to do is set the annotation on the pin
        else {
            pinView.annotation = annotation;
        }

        // and return it
        return pinView;
    }

    // otherwise we don't want to return one at all
    else {
        return nil;
    }
}

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control {
    // get the annotation object from the view that was tapped
    CustomAnnotation *tappedAnnotation = view.annotation;

    // get the info from the annotation itself
    NSString *urlFromAnnotation = tappedAnnotation.URL;

    // do something with the info - in this case I'm just popping up an alert for demo purposes
    [[[UIAlertView alloc] initWithTitle:@"The URL was:" message:urlFromAnnotation delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] show];
}

以下是显示结果的视频:

https://www.dropbox.com/s/rbcpvo3cuspgly9/annotation.mov

编辑:根据您的编辑/问题

你说你添加了自定义注释文件,但在你展示的代码中你没有使用自定义注释对象,你只使用了常规的MKPointAnnotation ...下面我编辑了你的代码以使其更多可读且清晰 - 您可能想尝试使您的变量名称和字典键更有意义。这是假设您有一个名为employees的数组,其中填充了字典,每个字典都有一个名为&#34; GymName&#34;并且该键的值是带有lat和lon值的%分隔字符串?

for (int index = 0; index < employees.count; index++) {
        NSLog(@"Inside Loop!");

        // get the employee dictionary based on the array index
        NSDictionary *employee = employees[index];

        // get the coordinate string - I'm guessing based on your code it's something like "45.254%72.156"
        NSString *coordinateString = employee[@"GymName"];

        // break up the lat and lon into components in an array
        NSArray *coordinateComponents = [coordinateString componentsSeparatedByString:@"%"];

        // get the latitude and longitude values from the components array and conver them to doubles
        double latitude = [coordinateComponents[0] doubleValue];
        double longitude = [coordinateComponents[1] doubleValue];

        // create the coordinate object from the lat and long
        CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake(latitude, longitude);

        // create the custom annotation and add it to the map
        CustomAnnotation *annotation = [[CustomAnnotation alloc] init];
        annotation.coordinate = coordinate;
        [_mapView addAnnotation:annotation];
    }