如何基于MKAnnotation引脚执行分段



我有一个包含两个事件的Map,这两个事件都有非常接近的引脚。

此代码将检索两个引脚的位置:

// 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]);
}
}];

检索工作正常。例如:

=>PFGeoPoint:0x2805XXXX,纬度:50.XXXXX,经度:19.XXXXX

=>PFGeoPoint:0x280XXXXX,纬度:50.061XXXX,经度:19.93XXXX

当我点击一个pin时,它会根据以下代码执行分段:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([[segue identifier] isEqualToString:@"toEventDetails"]){
EventDetailsTableViewController *controller = (EventDetailsTableViewController *)segue.destinationViewController;
controller.Pointfrommap = self.location;
}
} 

我的问题:

self.location来自上述查询(PFQuery(的结果,因此它将保存从查询中获得的最后一个值,而不是来自引脚敲击的值

我的猜测:

我知道我可以使用这种委托方法:

- (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
}

也许我可以在这里添加一些内容,以便通过正确的位置。

您可以尝试

- (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
}

然后在prepareForSegue内部

controller.Pointfrommap =  (PFGeoPoint*) sender;

最新更新