I am trying to pass a managedObject from a mapView with multiple annotation to a mapDetailView with only one annotation of the managedObject passed. This works great in a tableView to mapDetaiView. Any help would be appreciated. My code ...
- (void)viewDidLoad {
[super viewDidLoad];
if (self.managedObjectContext == nil) {
self.managedObjectContext = [(CrossroadsTreasuresAppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext];
}
// setup the mapView
[mapView removeAnnotations:mapView.annotations];
[mapView setMapType:MKMapTypeStandard];
[mapView setZoomEnabled:YES];
[mapView setScrollEnabled:YES];
[self.view insertSubview:mapView atIndex:0];
[mapView setDelegate:self];
// setup the location coordnates to Victoria, TX
double lat = [@"28.825" doubleValue];
double lng = [@"-97.009" doubleValue];
CLLocationCoordinate2D rcoord;
rcoord.latitude = lat;
rcoord.longitude = lng;
// setup the map region
//MapLocation *annotation = [[MapLocation alloc] init];
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(rcoord, 25000, 25000);
MKCoordinateRegion adjustRegion = [mapView regionThatFits:region];
[mapView setRegion:adjustRegion animated:YES];
NSError *error;
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"GarageSaleItem" inManagedObjectContext:[self managedObjectContext]];
[fetchRequest setEntity:entity];
NSArray *markerObjects = [[self managedObjectContext] executeFetchRequest:fetchRequest error:&error];
for (int i = 0; i < [markerObjects count]; i++) {
NSDictionary *marker = (NSDictionary *)[markerObjects objectAtIndex:i];
// Set the annotation coordnates
double lat = [[marker valueForKey:@"latitude"] doubleValue];
double lng = [[marker valueForKey:@"longitude"] doubleValue];
CLLocationCoordinate2D coord;
coord.longitude = lng;
coord.latitude = lat;
// Create the annotation instatnce
MapLocation *annotation = [[MapLocation alloc] init];
// Set the annotation display information
annotation.coordinate = coord;
annotation.streetAddress = [marker valueForKey:@"streetAddress"];
annotation.city = [marker valueForKey:@"city"];
annotation.state = [marker valueForKey:@"state"];
annotation.zipCode = [marker valueForKey:@"zipCode"];
// Add the annotation the the map
[self.mapView addAnnotation:annotation];
[annotation release];
}
[fetchRequest release];
}
- (MKAnnotationView *)mapView:(MKMapView *)theMapView viewForAnnotation:(id <MKAnnotation>)annotation {
// if it's the user location, just return nil.
if ([annotation isKindOfClass:[MKUserLocation class]])
return nil;
// handle our two custom annotations
//
if ([annotation isKindOfClass:[MapLocation class]]) {
// try to dequeue an existing pin view first
static NSString* AnnotationIdentifier = @"com.coastalbendmedia.pin";
MKPinAnnotationView* pinView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:AnnotationIdentifier];
if (!pinView) {
// if an existing pin view was not available, create one
MKPinAnnotationView* customPinView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationIdentifier] autorelease];
customPinView.pinColor = MKPinAnnotationColorPurple;
customPinView.animatesDrop = YES;
customPinView.canShowCallout = YES;
// add a detail disclosure button to the callout which will open a new view controller page
//
// note: you can assign a specific call out accessory view, or as MKMapViewDelegate you can implement:
// - (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control;
//
UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
customPinView.rightCalloutAccessoryView = rightButton;
return customPinView;
} else {
pinView.annotation = annotation;
}
return pinView;
}
return nil;
}
- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control; {
DetailViewController *controller = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil];
// Pass the selected object to the new view controller.
[self.navigationController pushViewController:controller animated:YES];
// Set the detail item in the detail view controller.
// THIS IS WHERE MY PROBLEM IS! indexPath Undeclared
NSManagedObject *selectedObject = [[self fetchedResultsController] objectAtIndexPath:indexPath];
controller.detailItem = selectedObject;
[controller release];
}