Im trying to use the MKMap API and integrate a database table id so I can click a button detail disclosure to send the user to another page with further information. Ive been all over teh MKMapKit on the Apple site to find some property or method to help me with this and went over a few tutorials with no answers.
Ive tried to attach the id into the subtitle context so I can retrieve it in the MKAnnotationView where I make my MKPinAnnotationView and add teh button to the rightCalloutAccessoryView. It errors out and doesnt want to work.
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation{
MKPinAnnotationView *annView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:#"mkpin"];
annView.animatesDrop = YES;
annView.canShowCallout = YES;
UIButton *disclosureButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
annView.rightCalloutAccessoryView = disclosureButton;
return annView;
}
I used this technique for an individual pin on a map and it seemed to work but I didnt require an id...
When I try to get the subtitle of the annotation it kills the app. I know the (id)annotation is an integer but I dont know how to manipulate this information. This function above I think is called after my code:
[mapView addAnnotations:markers]; //where markers is an array of title, subtitle aka id
Any help would be greatly appreciated.
The subtitle property is for a subtitle, not a database id. No good can come from abusing it in that way. Even if you could get it to work, it's a horrible approach.
All you have to do is give your annotation class an appropriate property to store your id. When you are passed the annotation, you can access it from there by casting to your annotation class. When you are passed the annotation view in other delegate methods, you can access the annotation through its annotation property.
Related
I have a custom class for my MKAnnotations, I want to override the default mapView:viewForAnnotatation method so that I can add extra information in the callout. When I set my delegate in code (as per the code below) the annotations are dropped on the map and are selectable but my mapView:viewForAnnoation is never called.
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation{
NSLog(#"viewForAnnotation: called");
MKAnnotationView *aView = [mapView dequeueReusableAnnotationViewWithIdentifier:#"mapPin"];
if(!aView){
aView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:#"mapPin"];
}
aView.annotation = annotation;
return aView;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.mapView.delegate = self;
}
I know the delegate is being set as I can override the method -(void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view and I see an NSLog when I select an annotation.
When I change from setting the delegate in code to setting it in the Storyboard the method is called (NSLog(#"viewForAnnotation: called"); statements appear) but the annotations don't appear on the map and sometimes this error appears:
<Error>: ImageIO: CGImageReadSessionGetCachedImageBlockData *** CGImageReadSessionGetCachedImageBlockData: bad readSession [0x8618480]
This seems like two separate issues:
Regarding the setting delegate in code v storyboard, it's hard to reconcile your various observations (delegate method didSelectAnnotationView is getting called in both scenarios, but viewForAnnotation is not). The only difference between setting it in code v in the storyboard is the timing of when the delegate is getting set. You're not showing us the process of adding the annotations, so it's hard to diagnose on the basis of what you've described. If none of your delegate methods were getting called, I'd suspect the mapView IBOutlet, but if some are working and others aren't, I can only suspect a timing issue.
Regarding the setting of the MKAnnotationView, the default implementation does nothing. You either need to write your own subclass of MKAnnotationView, set its image if you're using your own image, or, much easier, just use MKPinAnnotationView. But just creating a MKAnnotationView won't do anything. You really want:
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation{
// If it's the user location, just return nil.
if ([annotation isKindOfClass:[MKUserLocation class]])
return nil;
// Handle any custom annotations.
MKAnnotationView *aView = [mapView dequeueReusableAnnotationViewWithIdentifier:#"mapPin"];
if(aView){
aView.annotation = annotation;
} else {
aView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:#"mapPin"];
}
aView.canShowCallout = NO;
return aView;
}
(Note, I'm not only creating a MKPinAnnotationView, but I'm also making sure that it's not a MKUserLocation, in case you ever choose to show user location on the map. I'm also going to explicitly set the canShowCallout, as that's presumably the reason you're writing this method at all.)
Bottom line, if you want to show a simple pin annotation view, use MKPinAnnotationView. Using MKAnnotationView alone will result in no annotations from appearing.
In case someone else is searching for a reason why mapView:viewForAnnotatation is not being called when the delegate is set in code, there is a bug in iOS 6 - http://openradar.appspot.com/12346693
I came across the same issue and I wanted to share my solution:
I also override
(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation
But I realised that these Annotations work similar to TableView and iOS will reuse the annotations like the cells in TVC (table view controller)
Since I was only using one Identifier mapView dequeueReusableAnnotationViewWithIdentifier:#"mapPin" it would not need to call ViewForAnnotation again if it had enough 'annotation' in memory.
So my solution was to create multiple Identifier the first time the map load based on my conditions.
This solved my problem.
i just dropped to this question accidentally when i was searching for the same problem.
I solved my problem, in my case im resizing the mapview.
I added delegate after i resize the mapview. it works now perfectly.!
I'm having alot of trouble trying to identify which callout bubble has been clicked.
Is there a way of identifying the callout bubbles some how as I've tried a number of examples on here but because I'm quite new to Objective C I can't seem to work out how to use them.
The following answers I've been trying to use but I'm confused with them
How to track which annotation callout clicked
I'm looking at tagging the annotations but thats even confusing me. :(
Taken from the second answer in the above link:
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
if ([annotation isKindOfClass:[MKUserLocation class]]){
return nil;
if(annotation.tag == 111)
//Do something
else
//Do some other thing
}
How does the first part of this snippet work, perhaps if I understood it better I'd know what I'm working with.
Also how would I go about retrieving the tags when the callout is clicked so I can make the app respond respectively.
You can subclass MKPinAnnotationView so you can identify it when
- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKPinAnnotationView *)view
is called.
You have to create class which extends MKAnnotation and create some property like tag and assign unique value or tag and add in mapview like.
Place* home = [[Place alloc] init] ;
home.name = [dForMap valueForKey:#"shortaddress"];
home.latitude = [[dForMap valueForKey:#"latitude"]floatValue];
home.longitude = [[dForMap valueForKey:#"longitude"]floatValue];
home.description = [dForMap valueForKey:#"shortaddress"];
home.flierid = count;
PlaceMark* from = [[PlaceMark alloc] initWithPlace:home] ;
from.flierid = count;
from.mapAnnotationType = MapAnnotationTypeProperty;
[self addAnnotation:from];
you can check the property in didSelectAnnotationView method like this way when callout bubble is clicked.
- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view
{
PlaceMark *a = (PlaceMark*)view.annotation;
Place *test = a.place;
int tag = test.flierid;
}
Thanks.
In my application I have a MKMapView where several annotations are shown. The map rotates based on the heading of the device. To rotate the map the following statement is performed (called by the method locationManager: didUpdateHeading:)
self.navigationMapView.mapView.transform = CGAffineTransformMakeRotation(-heading);
where the heading (magnetic) is expressed in radians. What I noticed it's that even the annotations in the map rotate and I don't want it. I tried to fix it in the following method:
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation{
static NSString *identifier = #"AnnotationViewIdentifier";
MKAnnotationView *av = [mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
if (av == nil) {
av = [[[MKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:identifier] autorelease];
}
else{
av.annotation = annotation;
}
av.transform = CGAffineTransformMakeRotation(degreesToRadians(self.arController.currentHeading.magneticHeading));
av.canShowCallout = YES;
return av;
}
and I want to call this method from "didUpdateHeading:" but I really don't know how to do it. The TableView class has the reloadData function that calls the delegate method but here the things seem different. Any suggestions?!
Another question, my annotations on the map show the distance from the user, I would like to update them (distance label) as soon as the user change location. Any suggestions?!
So with a MKMapView having that be called properly is a little bit annoying. Essentially you have one of two options. Option 1: Create an array of the annotation on the screen and remove that from the map_view and then re-add them to the map_view. Essentially creating your own reload data function. Option 2: Do something simple such as
CGLocationCoordinate2D coordinate = map_view.center;
map_view.center = coordinate;
-- Essentially the point is to reset a property of the map causing it to redraw. However this option is not always going to work. Option 1 has a higher chance of working however that one can also fail, so if simply taking the annotations off and re-adding them causes nothing to happen then simply decreate the map and then recreate the map at the end of your map refresh function something like.
[my_map_view removeFromSuperView];
[my_map_view release];
my_map_view = nil;
my_map_view = [[MKMapView alloc] initWithFrame:CGRectMake(0,0,320,480)];
one of these options should work. I had to do option one for my solution however I know some people are lucky and option 2 works just as well.
I am writing an application that displays a thumbnail from a server in the leftCalloutAccessoryView. Before I actually get the image, I display a placemark image. Once the photo is downloaded, a method is called and I want to update that image without removing the original annotation and placing another. I just wan the image to switch from the placeholder to the downloaded thumbnail. Below is the code that does not work, but if someone could let me know if I'm on the right track, that would be great.
MyAnnotationClass *annotation=[[MyAnnotationClass alloc] initWithCoordinate:item.location];
[annotation setItem:item];
if(item.title){
annotation.name=item.title;
}
else{
annotation.name=#"no title";
}
[annotation setDescription:[[NSString alloc] initWithFormat:#"Views:%d Likes:%d Comments:%d",item.views,item.likes,item.comments]];
[annotation setImage:[[photo.thumb copyWithDimensions: CGSizeMake(32.0, 32.0)] autorelease]];
MKAnnotationView *av=[__mapView viewForAnnotation:annotation];
UIImageView *imageView=[[UIImageView alloc] initWithImage:annotation.image];
imageView.frame=CGRectMake(0.0, 0.0, 32.0, 32.0);
av.leftCalloutAccessoryView=imageView;
[annotation release];
I have been looking around here for quite sometime, but nothing that I can find will do what I need. Thanks
If the annotation is already on the map and you want to update it, don't create a new one.
Instead, find the annotation you want to update in the map view's annotations array and update its properties. Then call viewForAnnotation: on that existing annotation and update the leftCalloutAccessoryView.
Also, make sure the viewForAnnotation delegate method has logic that sets the leftCalloutAccessoryView to either the placeholder image or the actual image by checking the annotation's properties (and not always the placeholder image).
This other question has some sample code that may help.
I have set a custom pinImage for my annotations and when I change the type toMKMapTypeHybrid it reverts the pinImage setting to the standard pins.
I'm setting the mapType in my viewWillAppear method of the map view controller. I'm setting my pinImage for the annotations like so (shortened for clarity):
- (MKAnnotationView *) mapView:(MKMapView *) mapView viewForAnnotation:(id ) annotation {
MKPinAnnotationView *customAnnotationView=[[[MKPinAnnotationView alloc]
initWithAnnotation:annotation
reuseIdentifier:#"markerAnnotationView"] autorelease];
UIImage *pinImage = [UIImage imageNamed:#"/pin-image"];
[customAnnotationView setImage:pinImage];
return customAnnotationView;
}
Is there any way to use setImage and set the mapType via code?
The solution to this problem was to use an MKAnnotationView instead of the MKPinAnnotationView. I had previously been using the MKPinAnnotationView for good reason but later added a custom image to the pin without changing the instantiating class to MKAnnotationView.
Now, I think it is probably buggy framework behavior. If you're going to let an MKPinAnnotationView accept a setImage call then make sure that it handles it correctly when you change mapTypes. If you only want them to do pins then don't do anything with the call. Ah well.