Search code examples
iosobjective-cmkmapviewseguemkannotation

How can I perform a segue based on a MKAnnotation pin?


I have a Map with 2 events which both of them have pins pretty close to each other.

This code will retrieve the location of both pins:

// This query is used to get the location of all events in the parse cloud database
PFQuery *locationQuery = [PFQuery queryWithClassName:@"Event"];
[locationQuery whereKeyExists:@"EventLocation"];
[locationQuery whereKey:@"Organizator" notEqualTo:PFUser.currentUser.username];
[locationQuery findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
    if (!error) {
        // All the locations are stored in the objects NSArray

        for (PFObject *gp in objects) {

            // We store all the found locations in a PFGeoPoint object and then we have to transform this PFGeoPoints in real latitude/longitude coordinates via the CLLocationCoordinate2DMake method
            self.location = [gp objectForKey:@"EventLocation"];
            CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake(self.location.latitude, self.location.longitude);

            // Subclass of MKAnnotation
            AnnotationMap *AllEventsAround = [[AnnotationMap alloc]init];

            AllEventsAround.coordinate = coordinate; // We assign the coordinates got in CLLocationCoordinate2DMake to the coordinates of the AnnotationMap class
            AllEventsAround.title = [gp valueForKey:@"SportName"];
            AllEventsAround.subtitle = [NSString stringWithFormat:@"%@",[gp valueForKey:@"DateAndTime"]];

            // method call to point the annotations on the map
            [self.EventsmapMapView addAnnotation:AllEventsAround];
        }
    } else {
        // Log details of the failure
        NSLog(@"Error: %@ %@", error, [error userInfo]);
    }
}];

The retrieval works fine. An example can be :

=> PFGeoPoint: 0x2805XXXX, latitude: 50.XXXXX, longitude: 19.XXXXX

=> PFGeoPoint: 0x280XXXXX, latitude: 50.061XXXX, longitude: 19.93XXXX

When I click on a pin, it performs a segue according to this code:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    if ([[segue identifier] isEqualToString:@"toEventDetails"]){

    EventDetailsTableViewController *controller = (EventDetailsTableViewController *)segue.destinationViewController;
    controller.Pointfrommap = self.location;
  }
} 

My problem:

self.location comes from the result of the above query (PFQuery), therefore it will save the last value obtained from the query, but not the value that comes from a pin tapped

My guess:

I am aware that I can use this Delegate method:

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

    //In case a pin is selected, we perform a segue to the event's detail view. On the segue, the actual event coordinates are stored in tappedCoord and send to the details view controller

    [self performSegueWithIdentifier:@"toEventDetails" sender:self];   // Use your appropriate segue identifier
}

Maybe there's something I can add here in order to pass the correct location.


Solution

  • You can try

    - (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view{
    
         AnnotationMap *allEventsAround = (AnnotationMap*)view.annotation;
         PFGeoPoint *loc  = [PFGeoPoint geoPointWithLatitude:allEventsAround.coordinate.latitude longitude:allEventsAround.coordinate.longitude];
        [self performSegueWithIdentifier:@"toEventDetails" sender:loc];   // Use your appropriate segue identifier
    }
    

    Then inside prepareForSegue

    controller.Pointfrommap =  (PFGeoPoint*) sender;