Adding an accessory view button to the MKMapView call out annotation? - iphone

I've recently come across this website and I've been trying to add to my call out view a button (from a image).
The code on the websites example works just fine, but when I tried to add in the same code to my project I'm not getting the same results.
There is obviously something I've missed but I cannot seem to work this one out!
Here is my code for annotating:
- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation{
MKPinAnnotationView *annView=[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:#"currentloc"];
annView.pinColor = MKPinAnnotationColorGreen;
UIButton *advertButton = [UIButton buttonWithType:UIButtonTypeCustom];
advertButton.frame = CGRectMake(0, 0, 23, 23);
advertButton.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
advertButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
[advertButton setImage:[UIImage imageNamed:#"button_right.png"] forState:UIControlStateNormal];
[advertButton addTarget:self action:#selector(showLinks:) forControlEvents:UIControlEventTouchUpInside];
annView.rightCalloutAccessoryView = advertButton;
annView.animatesDrop=TRUE;
annView.canShowCallout = YES;
annView.calloutOffset = CGPointMake(-5, 5);
return annView;
}
Any help would be appreciated! :)

That guy is using a custom image to imitate the detail disclosure. You can actually get the standard detail disclosure quite easily:
[UIButton buttonWithType:UIButtonTypeDetailDisclosure];

Instead of setting the target like you did here:
[advertButton addTarget:self action:#selector(showLinks:) forControlEvents:UIControlEventTouchUpInside];
You can use the default delegate:
- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control

Sorry to answer my own question but this was solved by setting the delegate of the MKMapView to the ViewController. Phew, took me a while to figure that out and it's so simple!!

Related

How Do I Add Custom Button/MapPin to MKAnnotation?

I'm a noob to iphone development and i'm trying to add a custom button to an annotation callout. I have no problems adding a regular button to rightCalloutAccessoryView, but it's just not working for custom style. My image size is 32 x 32. I also want to add a custom map pin following this stackoverflow question here Any help is greatly appreciated.
MY CODE
-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
// Define your reuse identifier.
static NSString *identifier = #"MapPoint";
if ([annotation isKindOfClass:[MapPoint class]]) {
MKPinAnnotationView *annotationView = (MKPinAnnotationView *) [self.mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
if (annotationView == nil) {
annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
} else {
annotationView.annotation = annotation;
}
annotationView.enabled = YES;
annotationView.canShowCallout = YES;
annotationView.animatesDrop = YES;
annotationView.image = [UIImage imageNamed:#"myimage.png"];
UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
//UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[rightButton setImage:[UIImage imageNamed:#"phony2.png"] forState:UIControlStateNormal];
[rightButton addTarget:self
action:#selector(showDetails:)
forControlEvents:UIControlEventTouchUpInside];
annotationView.rightCalloutAccessoryView = rightButton;
return annotationView;
}
return nil;
}
[rightButton addTarget:self
action:#selector(showDetails:)
Remove this line
and set frame of the button
[rightButton setFrame:CGRectMake(0,0,32,32)];
and get the tap action from the degate method
- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control;
where you get the annotation view.
Hope this will help you.

How to show custom callout with two button in mkmapview in iPhone?

I want to show a custom callout with two button , and I also want action on both buttons ,
I try that but not do this type of callout on map, this isn't really exactly what I wanted. As I am making an iPhone application, Since I am new to MKMapview its getting difficult for me to do the same. How to show custom callout with two button?
You can go for Left and right calloutaccessoryview Look at HERE
//ANNOTATION VIEW SETTING DELEGATE
-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation{
MKPinAnnotationView *myPin = [[MKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:#"reuseString"];
myPin.draggable = NO;
myPin.canShowCallout = YES;
myPin.pinColor = MKPinAnnotationColorRed;
UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
rightButton.frame = CGRectMake(0, 0, 50, 30);
[rightButton setTitle:#"Info!" forState:UIControlStateNormal];
[rightButton setBackgroundImage:[UIImage imageNamed:#"image.png"] forState:UIControlStateNormal];
rightButton.showsTouchWhenHighlighted = YES;
[rightButton addTarget:self action:#selector(rightAcccoryViewButtonCLicked) forControlEvents:UIControlEventTouchUpInside]; //rightAcccoryViewButtonCLicked is a function
myPin.rightCalloutAccessoryView = rightButton;
return myPin;
}
Similarly you can use the leftCalloutAccessoryView and add another button.

MKAnnotation random order in MKMapView

I have a table view with an ordered list of objects, ordered by distance between me and the object. I need to show annotations in a map to show to the user the position of the objects but i need to show in the same order than table´s order because then i need to get with one has been pressed to show info of each object.
The problem is that when I put the annotations in the map each time I put them the tag number of the annotation is different. Is like the map added the annotations in a random order.
I have an annotations array and then I add that array to the map:
MKPointAnnotation* tallerAnnotation = [[MKPointAnnotation alloc] init];
[tallerAnnotation setCoordinate:CLLocationCoordinate2DMake([taller getLatitudTaller], [taller getLongitudTaller])];
[tallerAnnotation setTitle:[taller getNombre]];
[tallerAnnotation setSubtitle:[taller getDomicilio]];
[annotations addObject:tallerAnnotation];
And then add the array to the map:
[mapa addAnnotations:annotations];
Here is the code where I custom the annotation:
- (MKAnnotationView *) mapView: (MKMapView *) mapa viewForAnnotation: (id <MKAnnotation>) annotation_ {
MKPinAnnotationView *pin = (MKPinAnnotationView *) [self.mapa dequeueReusableAnnotationViewWithIdentifier: #"YourPinId"];
if (pin == nil) {
pin = [[MKPinAnnotationView alloc] initWithAnnotation:annotation_ reuseIdentifier: #"YourPinId"];
}
else {
pin.annotation = annotation_;
}
if ([[annotation_ title]isEqualToString:#"Tu posición"])
pin.pinColor = MKPinAnnotationColorRed;
else
{
[pin setCanShowCallout:YES];
pin.pinColor = MKPinAnnotationColorGreen;
UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[rightButton addTarget:self action:#selector(getInfoTalleres:) forControlEvents:UIControlEventTouchUpInside];
[rightButton setTag:posicionActualTaller];
pin.rightCalloutAccessoryView = rightButton;
UILabel *l1=[[UILabel alloc] init];
l1.frame=CGRectMake(0, 5, 50, 15);
l1.backgroundColor = [UIColor clearColor];
l1.textColor = [UIColor whiteColor];
l1.font=[UIFont fontWithName:#"Arial Rounded MT Bold" size:(10.0)];
l1.text=[[[mainDelegate getTalleres]objectAtIndex: posicionActualTaller]getDistancia];
posicionActualTaller +=1;
UIView *leftCAV = [[UIView alloc] initWithFrame:CGRectMake(0,0,50,15)];
[leftCAV addSubview : l1];
pin.leftCalloutAccessoryView = leftCAV;
}
pin.animatesDrop = YES;
return pin;
}
I would like to do something like this:
- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
NSLog(#"Tag del taller = %i",[control tag]);
[mainDelegate setTallerActual:[[mainDelegate getTalleres]objectAtIndex:[control tag]]];
[self detalleTaller];
}
[control tag] has differents values each time I enter into map view so that way it´s impossible do something like [objects objectAtIndex:[control tag]];
Please I need help.
Thanks in advance.
Ok finally I have found a solution.
When I create the
UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[rightButton addTarget:self action:#selector(getInfoTalleres:) forControlEvents:UIControlEventTouchUpInside];
for the annotation I assign the annotation title to the button this way:
[rightButton setTitle:[annotation_ subtitle] forState:UIControlStateNormal];
Then:
-(void)getInfoTalleres:(UIButton* )sender
{
for (int i = 0; i < [[mainDelegate getTalleres]count]; i++)
{
if ([[sender currentTitle] isEqualToString:[[[mainDelegate getTalleres]objectAtIndex:i]getDomicilio]])
{
[mainDelegate setTallerActual:[[mainDelegate getTalleres]objectAtIndex:i]];
[self detalleTaller];
break;
}
}
}
Hope somebody can use this solution.
Thanks a lot Cocoa.
Regards.
Check the Answer here it access title property Determine which MKPinAnnotationView was selected? and the tutorial http://www.scribd.com/doc/91629556/192/The-MKAnnotationView-class
let me know if you still not find answer

Annotation pin color not changing and the detail disclosure is not coming in the Mapkit

- (MKPinAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id) annotation{
MKPinAnnotationView *annView=[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:#"MyPin"];
annView.enabled=YES;
[annView setCanShowCallout:NO];
annView.pinColor=MKPinAnnotationColorGreen;
UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
//[rightButton addTarget:self action:#selector(annotationViewClick:) forControlEvents:UIControlEventTouchUpInside];
annView.rightCalloutAccessoryView = rightButton;
return annView;
}
I m using this code only but ts not woking.....Help me!!!
Thanks in advance......
By setting [annView setCanShowCallout:NO]; the call-out for the annotation cannot be displayed. And the detail disclosure will be visible in the callout.

Setting action for custom UIButton subclass

I have been trying for a couple of days to find a way how to call action:selector on a custom button, derived from UIButton, which I want to associate with a pin annotation. The reason that I used a subclass is that I want to pass some data into this button object, in order to show another UIView displaying these data when the button is pressed.
The problem is that while executing, when I click on the pin annotation, it opens its button, and when I click on it, nothing happens. My showDetails: method is not called on receiving the touchupInside event as I set here:
[rightButton addTarget:self
action:#selector(showDetails:)
forControlEvents:UIControlEventTouchUpInside];
My question is how to inherit the methods of UIButton, like addTarget:action:forControlEvents:, buttonWithType:, etc., in order to use them in my subclass?
Here is the complete code concerning the problem:
- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation
{
MKPinAnnotationView *annView=[[[MKPinAnnotationView alloc]
initWithAnnotation:annotation reuseIdentifier:#"MyPin"] autorelease];
annView.animatesDrop=FALSE;
annView.canShowCallout = YES;
[annView setSelected:YES];
if ([[annotation title] isEqualToString:#"Current Location"]) {
annView.pinColor = MKPinAnnotationColorRed;
}
else {
annView.pinColor = MKPinAnnotationColorPurple;
}
annView.calloutOffset = CGPointMake(-5, 5);
PlaceMark *pm=(PlaceMark *)annotation;
AddressItem *ai=[pm getData];
//Another problem is that i cant set buttonWithType:UIButtonTypeDetailDisclosure cause it generate error cause it is a method of UIbutton superclass
MyDetailButton* rightButton = [MyDetailButton buttonWithType:UIButtonTypeCustom];
[rightButton setData:ai];
[rightButton setTitle:annotation.title forState:UIControlStateNormal];
[rightButton addTarget:self action:#selector(showDetails:) forControlEvents:UIControlEventTouchUpInside];
annView.rightCalloutAccessoryView = rightButton;
return annView;
}
Thank you in advance. I hope that I have given a clear explanation of my problem.
I think you're on wrong way. To send action #selector your class need inherit from UIControl and not UIButton. Check out UIControl Class Reference I think the problem is here.
Do not hesitate to tell me if I did not understand your question.