click on button in MKAnnotationView - iphone

I m working on an app using MKMapView
there's some annotation and when u click on the annotation image , details are shown
for that i ve build my custom #interface Annotation : NSObject and added a boolean value "bClicked"
now I want the user to be able to click on a detail button inside the detailview but the code i ve made is not working :
#pragma mark - MKMapViewDelegate
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
MKAnnotationView *annotationView = nil;
int index = 0;
BOOL bFound = FALSE;
for (int i=0;i<[shopAnnotations count];i++)
{
if (annotation == [shopAnnotations objectAtIndex:i])
{
index = i;
bFound = TRUE;
break;
}
}
if (bFound==FALSE)
{
//ADLog(#"bFound FALSE annotation %p",annotation);
return annotationView; // <-- nil
}
Annotation *shopAnnotation = [shopAnnotations objectAtIndex:index];
annotationView = [_mapView dequeueReusableAnnotationViewWithIdentifier:shopAnnotation.identifier];
if (!annotationView)
annotationView = [[[MKAnnotationView alloc] initWithAnnotation:shopAnnotation reuseIdentifier:shopAnnotation.identifier] autorelease];
for (UIView*view in annotationView.subviews)
{
[view removeFromSuperview];
}
annotationView.image = shopAnnotation.Image;
annotationView.alpha = 1.0f;
annotationView.enabled = YES;
annotationView.canShowCallout = YES;
annotationView.userInteractionEnabled = YES;
if (shopAnnotation.bClicked==TRUE)
{
UIView *detailView = [[UIView alloc ] initWithFrame:CGRectMake(0, -50, 290, 56)];
detailView.userInteractionEnabled = YES;
detailView.superview = annotationView;
[detailView release];
UIImageView *bg = [[UIImageView alloc ] initWithImage:[UIImage imageNamed:#"bulle_carte.png"]];
[bg setFrame:CGRectMake(0, 0, 290, 56)];
bg.superview = detailView;
[bg release];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setFrame:CGRectMake(250, 3, 35, 35)];
[button setImage:[UIImage imageNamed:#"fleche_bleue_d.png"] forState:UIControlStateNormal];
button.tag = [shopAnnotation.identifier intValue];
[button addTarget:self action:#selector(btnDetailPressed:) forControlEvents:UIControlEventTouchUpInside];
button.superview = detailView;
button.userInteractionEnabled = YES;
[annotationView setRightCalloutAccessoryView:detailView];
}
return annotationView;
}
- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
NSLog(#"calloutAccessoryControlTapped");
}
-(void)btnDetailPressed:(id)sender
{
NSLog(#"btnDetailPressed");
}
when I click on the button or inside the DetailView, btnDetailPressed or calloutAccessoryControlTapped are not called/triggered.
Any suggestions?

Use the MKMapViewDelegate's method mapView:didSelectAnnotationView::
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
if ([annotation isKindOfClass:[YOUR_CUSTOM_CLASS class]]) {
//customize it
}
}
- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view
{
if ([view isKindOfClass:[YOUR_CUSTOM_CLASS class]]) {
//do something
}
}
Apple's doc

Related

Detail Disclosure Indicator MKMap to Custom View Controller

EXTRA CODE ADDED
I am new to xcode. I have managed to create annotations on my map in various different colours. What I would like to do is have each annotation lead to somewhere new with a disclosure button. I have implemented a right arrow disclosure button now what I need to know is how I link that up so that each annotation will lead to a different view controller of my choice. Here is my current code on my MapViewController implementation.
MainMapViewController.m
#import "MainMapViewController.h"
#import "LocationAnnotation.h"
#interface MainMapViewController ()
#end
//Totnes Main Centre Coordinates
#define Totnes_LATITUDE 50.433741
#define Totnes_LONGITUDE -3.685797
//The Dartmouth Inn Coordinates
#define DARTMOUTH_INN_LATITUDE 50.430036;
#define DARTMOUTH_INN_LONGITUDE -3.683873;
//Pub Offers Co-Ordinates
#define TheKingBill_LATITUDE 50.431379
#define TheKingBill_LONGITUDE -3.685495
#define TheSevenStars_LATITUDE 50.431045
#define TheSevenStars_LONGITUDE -3.682945
#define TheLordNelson_LATITUDE 50.430931
#define TheLordNelson_LONGITUDE -3.683644
//Span
#define THE_SPAN 0.01f;
#implementation MainMapViewController
#synthesize mainMapView;
- (void)viewDidLoad
{
[super viewDidLoad];
//Set Delegate
mainMapView.delegate = self;
//Create the region
MKCoordinateRegion myRegion;
//Centre
CLLocationCoordinate2D centre;
centre.latitude = Totnes_LATITUDE;
centre.longitude = Totnes_LONGITUDE;
//Span
MKCoordinateSpan span;
span.latitudeDelta = THE_SPAN;
span.longitudeDelta = THE_SPAN;
myRegion.center = centre;
myRegion.span = span;
//Set The Map View
[mainMapView setRegion:myRegion animated:YES];
//Annotation
NSMutableArray * locations = [[NSMutableArray alloc] init];
LocationAnnotation * myAnn;
//The King Bill Annotation
myAnn = [[LocationAnnotation alloc] initWithTitle:#"The King Bill"
andSubtitle:#"Another Pub In Town"
andCoordinate:CLLocationCoordinate2DMake(TheKingBill_LATITUDE, TheKingBill_LONGITUDE)
andID:1];
[locations addObject:myAnn];
//The Seven Stars Annotations
myAnn = [[LocationAnnotation alloc] initWithTitle:#"The Royal Seven Stars Hotel"
andSubtitle:#"Hotel In Town"
andCoordinate:CLLocationCoordinate2DMake(TheSevenStars_LATITUDE, TheSevenStars_LONGITUDE)
andID:2];
[locations addObject:myAnn];
//The Lord Nelson Annotations
myAnn = [[LocationAnnotation alloc] initWithTitle:#"The Lord Nelson"
andSubtitle:#"Great Pub In Centre of Town"
andCoordinate:CLLocationCoordinate2DMake(TheLordNelson_LATITUDE, TheLordNelson_LONGITUDE)
andID:3];
[locations addObject:myAnn];
[self.mainMapView addAnnotations:locations];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - MKMapViewDelegate
- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation {
if ([annotation isKindOfClass:[MKUserLocation class]])
return nil;
MKPinAnnotationView *annView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:#"pin"];
UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
annView.rightCalloutAccessoryView = rightButton;
int annId = ((LocationAnnotation *)annotation).idNumber;
annView.pinColor = (annId == 1) ? MKPinAnnotationColorPurple
: (annId == 2) ? MKPinAnnotationColorGreen
: MKPinAnnotationColorRed;
annView.canShowCallout = YES;
return annView;
}
#end
set rightbutton's tag to annID.
rightButton.tag = annId;
and then assign Selector to touchup event:
[rightButton addTarget:self action:#selector(YourMethod:) forControlEvents:UIControlEventTouchUpInside];
In YourMethod, you can use senders tag for navigating to different view
-(void)YourMethod:(UIButton*)sender
{
if(sender.tag==1)
{
//push viewcontroller1
}
else
{
//push viewcontroller
}
return;
}
As you can see, your right button is of type UIButton. Because of this fact you can addTarget to it:
[rightButton addTarget:self action:#selector(YourMethod) forControlEvents:UIControlEventTouchUpInside];
After user tap on rightButton YourMethod will be launched.
So for every pin you can add different button with different method, in the same way you are setting AnnotationColor
int annId = ((LocationAnnotation *)annotation).idNumber;
if (annId == 1)
{
annView.pinColor = MKPinAnnotationColorPurple;
UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
annView.rightCalloutAccessoryView = rightButton;
[rightButton addTarget:self action:#selector(MethodWhereYouWillFireYouNewViewController) forControlEvents:UIControlEventTouchUpInside];
}
else if (annId == 2)
{
//similar as above
}
Check This:
-(void)clickOnMapAnnotation:(UIButton*)sender
{
int AnnotationClicked =sender.tag;
if(AnnotationClicked ==1)
{
//PushViewcontroller1
}
else
{
//PushViewcontroller1
}
}
(MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id ) annotation {
if ([annotation isKindOfClass:[MKUserLocation class]])
return nil;
MKPinAnnotationView *annView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:#"pin"];
int annId = ((LocationAnnotation *)annotation).idNumber;
annView.pinColor = (annId == 1) ? MKPinAnnotationColorPurple
: (annId == 2) ? MKPinAnnotationColorGreen
: MKPinAnnotationColorRed;
//The Detail Disclosure button that I want to lead to a new view controller.
UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[rightButton addTarget:self action:#selector(clickOnMapAnnotation:) forControlEvents:UIControlEventTouchUpInside];
rightButton.tag = annId;
annView.rightCalloutAccessoryView = rightButton;
annView.canShowCallout = YES;
return annView;
}

Adding multiline subtitle into MKAnotationView Xcode

I am having a problem with displaying a multiline MKAnnotaion in Xcode 4.2
here is my code that is displaying a single line subtitle for the annotation.
-(MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation:
(id <MKAnnotation>)annotation {
MKPinAnnotationView *pinView = nil;
if(annotation != mapView.userLocation)
{
static NSString* MyAnnotationIdentifier = #"MyAnnotationIdentifier";
MKPinAnnotationView* customPinView = [[[MKPinAnnotationView alloc]
initWithAnnotation:annotation reuseIdentifier:MyAnnotationIdentifier] autorelease];
//--- adding pin color and animation
customPinView.pinColor = MKPinAnnotationColorRed;
customPinView.animatesDrop = YES;
customPinView.canShowCallout = YES;
return customPinView;
}
else
{
pinView.annotation = annotation;
}
return pinView;
}
So how to add a multiline subtitle?
Thanks in advance.
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
MKAnnotationView *annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:#"loc"];
// Button
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0, 0, 23, 23);
button.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
[button setImage:[UIImage imageNamed:yourImageName] forState:UIControlStateNormal];
[advertButton addTarget:self action:#selector(buttonPress:) forControlEvents:UIControlEventTouchUpInside];
annView.rightCalloutAccessoryView = button;
// Image and two labels
UIView *leftCAV = [[UIView alloc] initWithFrame:CGRectMake(0,0,23,23)];
[leftCAV addSubview : yourImageView];
[leftCAV addSubview : yourFirstLabel];
[leftCAV addSubview : yourSecondLabel];
annotationView.leftCalloutAccessoryView = leftCAV;
annotationView.canShowCallout = YES;
return pin;
}
also see this link for detail
How to display 2 lines of text for subtitle of MKAnnotation and change the image for the button on the right?
plz use this
- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {
if ([annotation isKindOfClass:[MKUserLocation class]])
return nil;
if ([annotation isKindOfClass:[CustomAnnotation class]]) {
CustomAnnotation *customAnnotation = (CustomAnnotation *) annotation;
MKAnnotationView *annotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:#"CustomAnnotation"];
if (annotationView == nil)
annotationView = customAnnotation.annotationView;
else
annotationView.annotation = annotation;
//Adding multiline subtitle code
UILabel *subTitlelbl = [[UILabel alloc]init];
subTitlelbl.text = #"sri ganganagar this is my home twon.sri ganganagar this is my home twon.sri ganganagar this is my home twon. ";
annotationView.detailCalloutAccessoryView = subTitlelbl;
NSLayoutConstraint *width = [NSLayoutConstraint constraintWithItem:subTitlelbl attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationLessThanOrEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:150];
NSLayoutConstraint *height = [NSLayoutConstraint constraintWithItem:subTitlelbl attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationGreaterThanOrEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:0];
[subTitlelbl setNumberOfLines:0];
[subTitlelbl addConstraint:width];
[subTitlelbl addConstraint:height];
return annotationView;
} else
return nil;
}

detailButton unable to push information over. Pushed page always have empty labels

Hi can some1 plz help me....
The problem is this: Everything seems alright.. i am able to push to another page once i tap on the detailButton. However, the page which i am able to push to is always only a view without any values passed into the labels. All my labels in the view have nothing inside.. Am i doing something wrong or any wrong concept here?
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
BOOL isRed = NO;
//BOOL isPurple = YES;
if([annotation isKindOfClass:[MyAnnotation2 class]])
isRed = YES;
MKPinAnnotationView *pinView = nil;
if(isRed) {
static NSString *redPin = #"redPin";
pinView = (MKPinAnnotationView *) [mapView dequeueReusableAnnotationViewWithIdentifier:redPin];
if (!pinView) {
pinView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:redPin] autorelease];
pinView.pinColor = MKPinAnnotationColorRed;
pinView.animatesDrop = YES;
pinView.canShowCallout = YES;
UIButton *detailButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
pinView.rightCalloutAccessoryView = detailButton;
}
else{
pinView.annotation = annotation;
}
}
else {
static NSString *greenPin = #"greenPin";
pinView = (MKPinAnnotationView *) [mapView dequeueReusableAnnotationViewWithIdentifier:greenPin];
if (!pinView) {
pinView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:greenPin] autorelease];
pinView.pinColor = MKPinAnnotationColorGreen;
pinView.animatesDrop = NO;
pinView.canShowCallout = YES;
UIButton *detailButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
pinView.rightCalloutAccessoryView = detailButton;
}
}
return pinView;
}
//
- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
NSLog(#"calloutAccessoryControlTapped");
if (self.patientDetailsViewController == nil) {
self.patientDetailsViewController = [[[PatientDetailsViewController alloc] initWithNibName:#"PatientDetailsViewController" bundle:nil] autorelease];
}
MyAnnotation2 *selectedObject = (MyAnnotation2 *)view.annotation;
_patientDetailsViewController.nric = selectedObject.title;
NSLog(#"%#",_patientDetailsViewController.nric);
[self.navigationController pushViewController:_patientDetailsViewController animated:YES];
}
Don't use your own target/action and tagging for the callout accessory button.
Instead, use the MKMapViewDelegate method calloutAccessoryControlTapped:. Remove the addTarget line and implement the delegate method.
In that delegate method, you can access the annotation tapped using:
MyAnnotation2 *selectedObject = (MyAnnotation2 *)view.annotation;

Getting wrong annotation from MapView Delegate method

I have declared the following in my .h file:
Annotation *annoForMoreDetails.
However when I try to set it to the current annotation in the method
- (MKAnnotationView *)mapView:(MKMapView *)mapViews viewForAnnotation:(id <MKAnnotation> )annotation
It gets set to the wrong object.
Here is my code:
- (MKAnnotationView *)mapView:(MKMapView *)mapViews viewForAnnotation:(id <MKAnnotation> )annotation
{
NSLog(#"welcome into the map view annotation");
// if it's the user location, just return nil.
if ([annotation isKindOfClass:[MKUserLocation class]])
return nil;
for (annotation in [mapView annotations]) {
if ([[mapView annotations] containsObject:annotation]) {
annoForMoreDetails = annotation;
}
}
if (annoForMoreDetails.coordinate.latitude != mapViews.userLocation.coordinate.latitude && annoForMoreDetails.coordinate.longitude != mapViews.userLocation.coordinate.longitude) {
// try to dequeue an existing pin view first
static NSString* AnnotationIdentifier = #"AnnotationIdentifier";
MKPinAnnotationView* pinView = [[[MKPinAnnotationView alloc]
initWithAnnotation:annotation reuseIdentifier:AnnotationIdentifier] autorelease];
pinView.animatesDrop=YES;
pinView.canShowCallout=YES;
pinView.pinColor = MKPinAnnotationColorGreen;
UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[rightButton setTitle:annotation.title forState:UIControlStateNormal];
[rightButton addTarget:self
action:#selector(moreDetails)
forControlEvents:UIControlEventTouchUpInside];
pinView.rightCalloutAccessoryView = rightButton;
return pinView;
}
else if (annoForMoreDetails.coordinate.latitude == mapViews.userLocation.coordinate.latitude && annoForMoreDetails.coordinate.longitude == mapViews.userLocation.coordinate.longitude) {
return nil;
}
return nil;
}
- (void)moreDetails {
annotationCalloutView.frame = CGRectMake(70, 120, 188, 218);
annotationCalloutView.alpha = 0.0;
mapView.userInteractionEnabled = NO;
annotationCalloutView.userInteractionEnabled = YES;
titleLabel.text = annoForMoreDetails.title;
titleLabel.numberOfLines = 0;
titleLabel.userInteractionEnabled = NO;
addressLabel.text = annoForMoreDetails.subtitle;
[self.view addSubview:annotationCalloutView];
[UIView beginAnimations:#"callout" context:NULL];
[UIView setAnimationDuration:0.6];
annotationCalloutView.alpha = 1.0;
[UIView commitAnimations];
}
If you see anything wrong please point it out!
Are you trying to use annoForMoreDetails to know which annotation the user tapped on so you can show more details? If that's the case, there's an easier way. Use the map view's calloutAccessoryControlTapped delegate method. It will be called when the user taps on the accessory button and it will pass the annotation view which includes the annotation as a property.
Remove the annoForMoreDetails instance variable and in viewForAnnotation, remove the for-loop that sets annoForMoreDetails (I think it ends up setting it to the last annotation every time). Remove all other references to annoForMoreDetails.
Also in viewForAnnotation, remove the addTarget line on the rightButton since you'll replace your custom moreDetails method with an implementation of calloutAccessoryControlTapped:
- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view
calloutAccessoryControlTapped:(UIControl *)control
{
Annotation *annoForMoreDetails = (Annotation *)view.annotation;
//the code currently in moreDetails method goes here...
}

Displaying custom UIView in callout of pin(annotation) in map, using MapKit

In my application i want to display custom view(with buttons) in the case of user touches annotation on map(standard pin).
i know that i can subclass MKAnnotationView and change the view of pin. but how can i change the look of Additional information view, that appears when user touches pin(by default it displays title and subtitle of MKAnnotation object).
how can i do this?
Thanks.
- (MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation:(id )annotation
{
MKPinAnnotationView *pinView = nil;
static NSString *defaultPinID = #"ReusedPin";
pinView = (MKPinAnnotationView*)[mVdequeueReusableAnnotationViewWithIdentifier:defaultPinID];
if ( pinView == nil )
pinView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:defaultPinID] autorelease];
if (((PinAnnotationView*)annotation).tag == 0 )
{
pinView.pinColor = MKPinAnnotationColorPurple;
}
else {
pinView.pinColor = MKPinAnnotationColorRed;
}
pinView.canShowCallout = YES;
pinView.animatesDrop = YES;
UIImageView *pinImageView = [[UIImageView alloc] initWithFrame:CGRectMake(-5, 0, 34, 34)];
UIImage *pinImage = [[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"icon" ofType:#"png"]];
pinImageView.image = pinImage;
[pinImage release];
[pinView addSubview:pinImageView];
[pinImageView release];
UIButton *btn = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
btn.tag = ((PinAnnotationView*)annotation).tag;
pinView.rightCalloutAccessoryView = btn;
return pinView;
}
-(void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control {
if ( control.tag !=0) {
ShowProviderDetailVC *viewControlle = [[ShowProviderDetailVC alloc]initWithNibName:#"ShowProviderDetailVC" bundle:nil];
viewControlle.lastViewName = #"SearchView";
for (NSMutableDictionary* dict in globalLocArray) {
if ( control.tag ==[[dict valueForKey:#"ID"] intValue] )
{
viewControlle.providerInfoDict = dict;
}
}
[self.navigationController pushViewController:viewControlle animated:YES];
[viewControlle release];
}
Custom callout bubble can be displayed by making sub class of UIView Class.
You can check these links.
http://blog.asynchrony.com/2010/09/building-custom-map-annotation-callouts-part-1/
http://blog.asynchrony.com/2010/09/building-custom-map-annotation-callouts-part-2/