如何在MKPointAnnotation上添加Tap手势以显示包含数据的另一个视图?

时间:2018-06-12 12:37:21

标签: ios objective-c mapkit mkmapview mkpointannotation

我有一个纬度和经度数组,并使用for循环在地图上显示MKPointAnnotation。我想在点击特定的MKPointAnnotation时将视图显示为带有数据的弹出窗口。

这是我的代码 -

- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation {

    MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(userLocation.coordinate, 10000, 10000);
    [self.mapView setRegion:[self.mapView regionThatFits:region] animated:YES];
    [_locationManager stopUpdatingLocation];
    NSArray *set = [[NSArray alloc] init];
    for (int i = 0; i < _json.count; i++) {
        _name = [_json[i] valueForKey:@"name"];
        _class = [_json[i] valueForKey:@"class"];
        set = [_json[i] valueForKey:@"set"];
        if (setFreeHour.count != 0) {
            for (int j=0;j<set.count;j++) {
                NSDictionary *dict = [[NSDictionary alloc] init];
                dict = set[j];
                _lat = dict[@"latitude"];
                _longi = dict[@"longitude"];
                CLLocationCoordinate2D coordinate;
                coordinate.latitude = [_lat doubleValue];
                coordinate.longitude = [_longi doubleValue];
                // Add an annotation
                MKPointAnnotation *point1 = [[MKPointAnnotation alloc] init];
                point1.coordinate = CLLocationCoordinate2DMake(coordinate.latitude, coordinate.longitude);
                point1.title = _name;
                point1.subtitle = _class;
                [self.mapView addAnnotation:point1];
            }
        }
    }
}   


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

    if ([annotation isKindOfClass:[MKUserLocation class]])
        return nil;

    // Handle any custom annotations.
    if ([annotation isKindOfClass:[MKPointAnnotation class]]) {
        // Try to dequeue an existing pin view first.
        MKAnnotationView *pinView = (MKAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:@"CustomPinAnnotationView"];
        if (!pinView) {
            // If an existing pin view was not available, create one.
            pinView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"CustomPinAnnotationView"];

            pinView.canShowCallout = YES;
            pinView.image = [UIImage imageNamed:@"annotation"];
            pinView.calloutOffset = CGPointMake(0, 0);
            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(showDetails)];
            [pinView addGestureRecognizer:tap];
        } else {
            pinView.annotation = annotation;
        }
        return pinView;
    } 
    return nil;
}

- (void)showDetails{
   self.popup.hidden = NO;
}

// PS:popup是一个包含标签的视图。我想将数据从MKPointAnnotation传递到视图

1 个答案:

答案 0 :(得分:1)

为什么不使用mapView(_:didSelect:)方法代替UITapGestureRecognizer

(void)mapView:(MKMapView *)mapview didSelectAnnotationView:(MKAnnotationView *)view {
    // 1. get data from view(MKAnnotationView)
    // 2. pass data to another view
}

https://developer.apple.com/documentation/mapkit/mkmapviewdelegate/1452393-mapview