MKAnnotationView Callout Accessory - ios

I am trying to implement a custom annotation with a custom callout view. I have successfully integrated both within my app, as you can see below:
I have created my custom callout view in a xib file:
However, when the user zooms/drags the map, my callout view stays in the same spot on the screen while the map and annotation is in a new position:
I am wondering how I can make the callout view stay above the annotation even when the map is moved, rotated or dragged. The code for my callout view can be found below:
- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation
{
if ([annotation isKindOfClass:[MKUserLocation class]])
{
return nil; //return nil to use default white dot view
}
if ([annotation isKindOfClass:[FishAnnotation class]])
{
static NSString *reuseId = #"seafoodRestaurant";
MKAnnotationView *annotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:reuseId];
if (annotationView == nil)
{
annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:reuseId];
}
else
{
annotationView.annotation = annotation;
}
FishAnnotation *ann = (FishAnnotation *)annotation;
annotationView.image = [UIImage imageNamed:ann.imageName];
annotationView.draggable = YES;
annotationView.canShowCallout = NO;
return annotationView;
}
//return nil (default view) if annotation is not our custom type
return nil;
}
- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view
{
CalloutView *calloutView = (CalloutView *)[[[NSBundle mainBundle] loadNibNamed:#"calloutView" owner:self options:nil] objectAtIndex:0];
CGRect calloutViewFrame = calloutView.frame;
calloutViewFrame.origin = CGPointMake(view.frame.origin.x - calloutView.frame.size.width/2 + 18, view.frame.origin.y-calloutView.frame.size.height);
calloutView.frame = calloutViewFrame;
FishAnnotation *annotation = view.annotation;
[calloutView setTitle:#"Seafood Restaurant!" subTitle:#"647-123-4567"];
calloutView.annotation = view.annotation;
[view.superview addSubview:calloutView];
[view.superview bringSubviewToFront:calloutView];
}
-(void)mapView:(MKMapView *)mapView didDeselectAnnotationView:(MKAnnotationView *)view
{
for (UIView *subview in view.superview.subviews)
{
if ([subview isKindOfClass:[CalloutView class]])
{
[subview removeFromSuperview];
}
}
}
Please let me know if there is a way to have the callout view fixed above the annotation. I have tried to use
[view addSubview:calloutView];
[view bringSubviewToFront:calloutView];
instead of using view.superview, however the callout view does not display (it's hidden) if I try this.

I found a temporary solution below. The problem with this is that the buttons on my callout view no longer work. The callout disappears when I select a button.
- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view
{
FishAnnotation *annotation = view.annotation;
// [appleMapView setCenterCoordinate:annotation.coordinate animated:YES];
if(![view.annotation isKindOfClass:[MKUserLocation class]])
{
CalloutView *calloutView = (CalloutView *)[[[NSBundle mainBundle] loadNibNamed:#"calloutView" owner:self options:nil] objectAtIndex:0];
CGRect calloutViewFrame = calloutView.frame;
calloutViewFrame.origin = CGPointMake(-calloutView.frame.size.width/2 + 18, -calloutView.frame.size.height);
calloutView.frame = calloutViewFrame;
[calloutView setTitle:annotation.title subTitle:annotation.subTitle];
calloutView.annotation = view.annotation;
[view addSubview:calloutView];
[view bringSubviewToFront:calloutView];
}
}
-(void)mapView:(MKMapView *)mapView didDeselectAnnotationView:(MKAnnotationView *)view
{
for (UIView *subview in view.subviews)
{
if ([subview isKindOfClass:[CalloutView class]])
{
[subview removeFromSuperview];
}
}
}

I suggest you to use this library because I've used it for my project recently.
SMCalloutView
https://github.com/nfarina/calloutview
It will help you deal with the mapView scrolling & panning.
Then I believe you can load and place your customView and addSubView inside the SMCalloutView.

Related

How to change UILabel value in CustomAnnotation by clicking button in iOS

I have added a custom annotation and a percentage label on it.
By pressing the button in red circle, I want to change value of label from percentage to business name.
My Code:
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
static NSString *identifier = #"MyLocation";
if ([annotation isKindOfClass:[BusinessCustomAnnotation class]]) {
MKAnnotationView *annotationView = (MKAnnotationView *) [mapViewOffers dequeueReusableAnnotationViewWithIdentifier:identifier];
if (annotationView == nil) {
annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
UILabel* category = [[UILabel alloc] initWithFrame:CGRectMake(annotationView.frame.size.width / 2, 15, 55, 20)];
BusinessCustomAnnotation *myAnnotationView = (BusinessCustomAnnotation *)annotation;
NSLog(#"Type One Offer! = %i", mapTypes);
[category setAdjustsFontSizeToFitWidth:YES];
if (mapTypes == 1) {
category.text = [NSString stringWithFormat:#"%#%#", myAnnotationView.offerPercentage, #"%"];
}else if (mapTypes == 2){
category.text = myAnnotationView.businessName;
}else if (mapTypes == 3){
category.text = myAnnotationView.businessName;
}
[category setMinimumScaleFactor:1.0];
category.font = [UIFont systemFontOfSize:15.0 weight:5.0];
category.textAlignment = NSTextAlignmentCenter;
[annotationView addSubview:category];
annotationView.enabled = YES;
annotationView.canShowCallout = NO;
annotationView.image = [UIImage imageNamed:#"iconMapMarker"];//here we use a nice image instead of the default pins
} else {
annotationView.annotation = annotation;
}
return annotationView; }return nil; }
Above mapview delegate is calling for one time only.
Waiting for the solution.
Thanks in advance for helping me.
There are two ways of detecting user interaction with your annotation view. The common technique is to define a callout (that standard little popover bubble that you see when you tap on a pin in a typical maps app) for your MKAnnotationView. And you create the annotation view for your annotation in the standard viewForAnnotation method:
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
if ([annotation isKindOfClass:[MKUserLocation class]])
return nil;
MKAnnotationView *annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:#"loc"];
annotationView.canShowCallout = YES;
annotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
return annotationView;
}
By doing this, you get a callout, but you're adding an right accessory, which is, in my example above, a disclosure indicator. That way, they tap on your annotation view (in my example above, a pin on the map), they see the callout, and when they tap on that callout's right accessory (the little disclosure indicator in this example), your calloutAccessoryControlTapped is called.
- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
//first check your view class here
// here your code for change text on view
}
- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view
{
//first check your view class here
// here your code for change text on view
}
You need to refresh the annotations. In Action of button try this :
mapTypes = 2
for (id<MKAnnotation> annotation in mapView.annotations)
{
[mapView removeAnnotation:annotation];
[mapView addAnnotation:annotation];
}
You can't make any changes to already added annotation pin.To make any changes to the annotation pin you need to remove all the pin and add it back.
Annotations don't refresh.
You have to remove all existing annotations with
[self.mapView removeAnnotations:self.mapView.annotations];
and update your "mapTypes" variable value to "2" or "3" in order to show business name.
Then you can can add your annotations again with [MKMapView addAnnotation:].

(iOS) Callout is not clickable in all area

I have strange problem with marker and callout view. My marker has 36x48 points but callout view 132x61points. If I add below code to didSelectAnnotationView my callout runs action only in marker's area (36x48). Rest callout's area does not react. Do you have any ideas how to improve my code that the whole area will be clickable ?
Thanks in advance
- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view
{
if(![view.annotation isKindOfClass:[MKUserLocation class]])
{
CalloutView *calloutView = (CalloutView *)[[[NSBundle mainBundle] loadNibNamed:#"calloutView" owner:self options:nil] objectAtIndex:0];
CGRect calloutViewFrame = calloutView.frame;
calloutViewFrame.origin = CGPointMake(- calloutViewFrame.size.width/2+35, -calloutViewFrame.size.height + 57);
calloutView.frame = calloutViewFrame;
calloutView.userInteractionEnabled = YES;
view.userInteractionEnabled = YES;
[view addSubview:calloutView];
}
}

Detect tap on MK ANNOTATION view callout bubble

I have an annotation on the map. When I select a annotation I will display callout bubble with custom view. Now when I click on the callout bubble I want to go to new view controller but callout view disappears when I tap on the view.
-(void)mapView:(MKMapView *)mapView1 didSelectAnnotationView:(MKAnnotationView *)view
{
NSLog(#"selected");
if(![view.annotation isKindOfClass:[MKUserLocation class]])
{
CustomInfoWindow *calloutView = [[[NSBundle mainBundle]
loadNibNamed:#"infoWindow"owner:self options:nil] objectAtIndex:0];
CGRect calloutViewFrame = calloutView.frame;
calloutViewFrame.origin = CGPointMake(-calloutViewFrame.size.width/2 + 15, -calloutViewFrame.size.height);
calloutView.frame = calloutViewFrame;
[calloutView.imagePlace.layer setBorderColor: [[UIColor orangeColor] CGColor]];
[calloutView.imagePlace.layer setBorderWidth: 3.0];
NSData* imageData = [[NSData alloc] initWithContentsOfURL:
[NSURL URLWithString:#"http://farm3.staticflickr.com/2926/14605349699_67a1d51b80.jpg"]];
UIImage* image = [[UIImage alloc] initWithData:imageData];
[calloutView.imagePlace setImage:image];
[view addSubview:calloutView];
}
}
-(void)mapView:(MKMapView *)mapView didDeselectAnnotationView:(MKAnnotationView *)view
{
for (UIView *subview in view.subviews ){
[subview removeFromSuperview];
}
}
I'm no expert but you may want to add a UITapGestureRecognizer to your view.
Use the following code:
UITapGestureRecognizer *tgr = [[UITabGestureRecognizer alloc] initWithTarget:self action:#selector(showNextView)];
calloutView.addGestureRecognizer(tgr);
and
- (void)showNextView {
...
}
The bubble does not have logic, you need use callout accessory. So, use MapView delegate method:
- (void)mapView:(MKMapView *)mv annotationView:(MKAnnotationView *)pin calloutAccessoryControlTapped:(UIControl *)control;
And add callout at viewForAnnotation delegate method. For example:
UIButton *myDetailAccessoryButton = [UIButton buttonWithType:UIButtonTypeInfoLight];
myDetailAccessoryButton.frame = CGRectMake(0, 0, 23, 23);
myDetailAccessoryButton.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
myDetailAccessoryButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
pinView.rightCalloutAccessoryView = myDetailAccessoryButton;
I hope my answer helps you.

Dragging the map after dragging my MKAnnotationView does not move the MKAnnotationView with it

MKPinAnnotationView does not allow you to use a custom image as 'pin' and enable dragging at the same time, because the image will change back to the default pin as soon as you start dragging. Therefore I use an MKAnnotationView instead of an MKPinAnnotationView.
While using MKAnnotationView instead of MKPinAnnotationView does keep your custom image shown as your 'pin' it doesn't support the drag & drop animation that you get with the default pin.
Anyway, my issue is that after I drag my custom MKAnnotationView to a new point on the map and then move the map itself the MKAnnotationView does not move with the map anymore.
-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {
static NSString *defaultID = #"myLocation";
if([self.annotation isKindOfClass:[PinAnnotation class]])
{
//Try to get an unused annotation, similar to uitableviewcells
MKAnnotationView *annotationView = [self.mapView dequeueReusableAnnotationViewWithIdentifier:defaultID];
//If one isn't available, create a new one
if(!annotationView)
{
annotationView = [[MKAnnotationView alloc] initWithAnnotation:self.annotation reuseIdentifier:defaultID];
annotationView.canShowCallout = YES;
annotationView.draggable = YES;
annotationView.enabled = YES;
}
UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0, 0.0, 32, 32)];
imgView.image = self.passableTag.image;
annotationView.leftCalloutAccessoryView = imgView;
annotationView.image = [UIImage imageNamed:[Constants tagIconImageNameForTagType:self.passableTag.type]];
return annotationView;
}
return nil;
}
Just set the annotationView.dragState to MKAnnotationViewDragStateNone after ending the drag:
- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view didChangeDragState:(MKAnnotationViewDragState)newState fromOldState:(MKAnnotationViewDragState)oldState {
if ([view.annotation isKindOfClass:[PinAnnotation class]]) {
if (newState == MKAnnotationViewDragStateEnding) {
view.dragState = MKAnnotationViewDragStateNone;
}
}
}
I had the same problem and I solved it adding "setDragState" to my MKAnnotationView class.
This is an old solution but it worked to me (iOS8).
Don't reuse MKAnnotationView and it will work.

iOS Mapkit Custom Callout

Here's a sample custom callout that I'd want to get a similar style of. I was thinking of inheriting from MKAnnotation but I'm lost how to start it and I don't know if the callout's design could be overridden.
Any ideas how to implement this custom callout with ui controls inside?
EDIT: Here's my code from following the similar StackOverflow answer:
- (MKAnnotationView *)mapView:(MKMapView *)mapview viewForAnnotation:(id <MKAnnotation>)annotation
{
if ([annotation isKindOfClass:[MKUserLocation class]])
return nil;
static NSString* AnnotationIdentifier = #"AnnotationIdentifier";
MKAnnotationView *annotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:AnnotationIdentifier];
if(annotationView)
return annotationView;
else
{
UIImage *img = [UIImage imageNamed:#"default_thumb.png"];
MKAnnotationView *annotationView =
[[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationIdentifier];
annotationView.canShowCallout = YES;
annotationView.image = img;
annotationView.draggable = YES;
/*
// change left accessory view button with image
UIImageView *leftAccView = [[UIImageView alloc] initWithImage:img];
annotationView.leftCalloutAccessoryView = leftAccView;
//include a right button to show more info
UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[rightButton addTarget:self action:#selector(calloutSubMenu:) forControlEvents:UIControlEventTouchUpInside];
[rightButton setTitle:annotation.title forState:UIControlStateNormal];
annotationView.rightCalloutAccessoryView = rightButton;
*/
return annotationView;
}
return nil;
}
// Customize accessory view
- (void)mapView:(MKMapView *)mapview annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
[mapview deselectAnnotation:view.annotation animated:YES];
PopOverCallout *popOverCallout = [[PopOverCallout alloc]init];
UIPopoverController *popOver = [[UIPopoverController alloc] initWithContentViewController:popOverCallout];
self.annotationPopoverController = popOver;
popOver.popoverContentSize = CGSizeMake(500, 500);
[popOver presentPopoverFromRect:view.bounds inView:view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}
The
(void)mapView:(MKMapView *)mapview annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
should be called whenever the pin is touched to show the callout right? but what happens is that a blank regular callout is shown. What might I be doing wrong?
BTW: the UIViewController subclass only has a label on it's XIB and is pretty much blank.
Found out the answer after a few mins of nap.
using this as the guide, I just needed to override
-(void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view
which shows the annotation callout. Thanks again for the answer, Ms. Anna.

Resources