-1

I have a an array of latitude and longitude and using for loop am displaying the MKPointAnnotation on the map. I want to show a view as a popup with data when the specific MKPointAnnotation is tapped.

Here is my code -

- (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 is a view that contains labels. I want to pass data from MKPointAnnotation to the view

Cœur
  • 37,241
  • 25
  • 195
  • 267
TheTravloper
  • 236
  • 5
  • 16

1 Answers1

1

Why don't you use mapView(_:didSelect:) method instead of 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

Kosuke Ogawa
  • 7,383
  • 3
  • 31
  • 52