How can i recognize the callout which is been selected in MKMapView?
I have a map view on which i am displaying a number of Pin annotation. I have also used the callout on these pins so now when somebody clicks on those pins a callout appears with the detail about that place and now i want to recognize the callout and push another view specific to that. Here's the code:
-(void)configureView:(NSDictionary *)serverResult {
// Get the set of projects from the project list
[spinner stopAnimating];
NSSet * projects = [[serverResult valueForKey:@"Map"] valueForKey:@"projects"];
NSLog(@"Map got %d projects", projects.count);
mapView.showsUserLocation = YES;
mapView.delegate = self;
MKCoordinateRegion newRegion;
MKCoordinateSpan span;
span.latitudeDelta=130.0;
span.longitudeDelta=130.0;
collection = [[NSMutableArray alloc]init];
// Iterate over the set of projects
for (NSManagedObject *project in projects) {
// Get the set of locations for the current project
NSSet *locations = [project valueForKey:@"locations"];
int i =0;
for (NSManagedObject *location in locations) {
NSString * projectId = [projects valueForKey:@"id"];
NSString * lat = [location valueForKey:@"latitude"];
double lati = [lat doubleValue];
NSString * lang = [location valueForKey:@"longitude"];
double longi = [lang doubleValue];
CLLocationCoordinate2D annotation = mapView.userLocation.coordinate;
annotation.latitude= lati;
annotation.longitude= longi;
newRegion.span=span;
newRegion.center=annotation;
geoCoder=[[MKReverseGeocoder alloc] initWithCoordinate:annotation];
geoCoder.delegate=self;
[geoCoder start];
[self.mapView setRegion:newRegion animated:YES];
i= i +1;
}
}
[self.view addSubview:mapView];
[self.view addSubview:segments];
}
- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFailWithError:(NSError *)error{
NSLog(@"Reverse Geocoder Errored");
}
- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFindPlacemark:(MKPlacemark *)placemark{
NSLog(@"Reverse Geocoder completed");
mPlacemark=placemark;
[mapView addAnnotation:placemark];
}
- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation{
MKPinAnnotationView *annView=[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"currentloc"];
annView.animatesDrop=TRUE;
annView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
annView.canShowCallout = YES;
annView.enabled = YES;
return annView;
}
- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
AllDetailDashboard *summary = [[AllDetailDashboard alloc] initWithNibName:@"View Controller" bundle:nil];
//Want to write some code here to recognize which project is clicked, in AllDetailDashBoard class we have a variable called project id which is used to recognize the project but i am not able to fetch the project id for specific project which is clicked here
[self.navigationController pushViewController:summary animated:YES];
[summary release];
}
Is there any other delegate 开发者_如何学编程method which i need to use to recognize the callout. Now on click of callout i want to send the projectId to the AllDetailDashBoard.
Thanks,
Yes it is pretty easy..
This may help a little...
-(void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control{
MyObject *objectTemp = (MyObject *) view.annotation;
//Do stuff here with that object such as
AllDetailDashboard *summary = [[AllDetailDashboard alloc] initWithNibName:@"View Controller" bundle:nil];
summary.projectID = objectTemp.projectID;
}
The MKAnnotationView * is your callout.
You could make your data object implement the MKAnnotation protocol (return a coordinate, a title and a subtitle), and then in calloutAccessoryControlTapped, cast the annotation as your data object (which contains your project ID).
Or, you could tag each annotation view with a number that relates back to the project ID.
A callout is displayed when an annotation is selected. Implement these delegate methods to get the annotationView that is tapped:
– mapView:didSelectAnnotationView:
– mapView:didDeselectAnnotationView:
btw, i dont see any code that is adding the annotation. In your annotation class add a property 'projectID' and initialize it when you initialize ur annotation. This way you can access the property like this:
if([annotationView.annotation isKindOfClass:[<your annotation classname> class]]) {
<your annotation classname> *ann = (<your annotation classname>*)annotationView.annotation;
NSString *projectID = [NSString stringWithString:ann.projectID];
}
精彩评论