I have annotations on a mapview and a callout with a button on each. What I need to do is grab properties from this callout, ie. the title, but logging this line:
NSLog(#"%#", mapView.selectedAnnotations);
returns <AddressAnnotation: 0x1bdc60> which obviously gives me no useful info...
My question is, how can I access the properties of a selected annotation callout?
This is how
for (id annotation in mapView.annotations) {NSLog([annotation title]);}
Here is what I did in the annotationviewClick function:
Hope this helps
-(IBAction) annotationViewClick:(id) sender{
[self.view addSubview:LoadingView];
Annotation *ann = [myMap.selectedAnnotations objectAtIndex:([myMap.selectedAnnotations count]-1)];
NSLog(#"Selected:%#", [ann tag]);
}
mapView.selectedAnnotations returns an array of anotations. You should access its items to get info.
Related
I have a map view with annotations, and these annotations display a callout. When the callout's disclosure detail button is clicked, it segues into a new view.
My MKAnnotations are a custom class that implements <MKAnnotation>. Let's call that class MyClass. They are stored in an NSMutableArray. During viewdidload of this view, I add each object of MyClass in this array to the map view's annotations. Using the debugger, I can see that once all of this adding is done, the [self.MapView annotations] order is the same as the NSMutableArray.
Now I set another breakpoint within mapView:viewForAnnotation: and check out the order of 1) my NSMutableArray and 2) [self.MapView annotations]. The array is of course in the same order. However, the order of the annotations has been scrambled.
This was a big problem for me, because I needed to use the specific instance of MyClass that the user selected in the next view. AKA, I wanted to look at the annotation, find its index, and then use that to get the same index within the array.
I've now realized that I can just save the annotation directly (coming from an Android background, this was very cool to me). However, I am still conceptually at a loss as to why the order became scrambled. Can someone help me? Code below:
- (void)viewDidLoad
{
if([fromString isEqualToString:#"FromList"])
self.navigationItem.hidesBackButton = TRUE;
else {
self.navigationItem.rightBarButtonItem = nil;
}
self.array = [MySingleton getArray];
//set up map
//declare latitude and longitude of map center
CLLocationCoordinate2D center;
center.latitude = 45;
center.longitude = 45;
//declare span of map (height and width in degrees)
MKCoordinateSpan span;
span.latitudeDelta = .4;
span.longitudeDelta = .4;
//add center and span to a region,
//adjust the region to fit in the mapview
//and assign to mapview region
MKCoordinateRegion region;
region.center = center;
region.span = span;
MapView.region = [MapView regionThatFits:region];
for(MyClass *t in self.array){
[MapView addAnnotation:t];
}
[super viewDidLoad];
}
//this is the required method implementation for MKMapView annotations
- (MKAnnotationView *) mapView:(MKMapView *)thisMapView
viewForAnnotation:(MyClass *)annotation
{
static NSString *identifier = #"MyIdentifier";
//the result of the call is being cast (MKPinAnnotationView *) to the correct
//view class or else the compiler complains
MKPinAnnotationView *annotationView = (MKPinAnnotationView *)[thisMapView
dequeueReusableAnnotationViewWithIdentifier:identifier];
if(annotationView == nil)
{
annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
}
annotationView.pinColor = MKPinAnnotationColorGreen;
//pin drops when it first appears
annotationView.animatesDrop=TRUE;
//tapping the pin produces a gray box which shows title and subtitle
annotationView.canShowCallout = YES;
UIButton *infoButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
annotationView.rightCalloutAccessoryView = infoButton;
return annotationView;
}
When you call addAnnotation or addAnnotations, the map view adds the reference(s) to its internal list of annotations.
The annotations property of MKMapView simply returns this internal list (whatever type it might be) as an NSArray.
I don't know of any place in the documentation where it states that the annotations property returns the array in the same order that you added the annotations in. If you have showsUserLocation turned on, the array will include that annotation even though you didn't explicitly add it.
You do not need to be concerned about nor should you depend on the order of the objects in the annotations property.
Just a few suggestions regarding the code:
Since your array contains objects that implement <MKAnnotation>, instead of looping through it, you can add all the annotations in one shot by calling addAnnotations (plural) and pass it the array
In viewForAnnotation, none of the properties you are setting depend on any specific annotation so you can set them all inside the if (av == nil) block. This way you get maximum reuse.
Also in viewForAnnotation, after and outside the if, you should set the annotation property of the view to the current annotation. This is in case the view is being reused from another annotation.
Finally, in viewForAnnotation, don't assume the annotation will be of type MyClass. If you turn on showsUserLocation, that won't be the case. It's safer to declare the parameter as id<MKAnnotation> and then if necessary check what its class is and then cast it.
#Anna, you state you should not be concerned for the order of the annotations. That's not true in my case. Some annotationviews might overlap, and I always need a specific one to be on the top of the two overlapping views. So the order DO makes sense for the annotations, as I hope the - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation gets called in the same order as i added the annotations.
EDIT:
and the solution is here :-)
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 tried several things but am unable to solve it out.
I have 10 custom annotations on the map depending upon the area visible.
Now I have 2 buttons next and previous. Clicking on which the callout of annotation must get displayed.
i.e if i click on next buton then callout of annotation 1 will appear and when i click next again then the callout of first will hide and callout of second will appear.
I have tried out
[self.mapView selectAnnotation:self.nextSelectedAnnotationView.annotation animated:YES]
and
[self.mapView deselectAnnotation:self.selectedAnnotationView.annotation animated:YES];
But the main problem is how to get the annotation here??
I have tried NSArray* selectedAnnotations=self.mapview.annotations to get the annotations array
id annotationView =[selectedAnnotations objectAtIndex:i];
[self.mapView selectAnnotation:annotationView animated:YES];
But no luck :(
Any other way to solve my issue.??
it may help you.
NSArray *selectedAnnotations = mapView.selectedAnnotations;
for(id annotationView in selectedAnnotations) {
[mapView deselectAnnotation:[annotationView annotation] animated:NO];
}
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.
I've subclassed MKAnnotation so that i can assign objects to each annotation, this is so that when the rightCalloutAccessoryView is clicked i can push a navigation controller with the object passed to it and display the objects data in another view.
This all works great apart from one thing, i've extended upon initWithCoordinate like so:
-(id)initWithCoordinate:(CLLocationCoordinate2D)coord andObject:(NSManagedObject *)object {
[self setPlace:object];
coordinate = coord;
title = [place valueForKey:#"name"];
subtitle = [place valueForKey:#"address"];
return self;
}
Although everything is working great i'm recieving the warning:
NO '-initWithCoordinate:andObject:' method found
Which means i'm doing something wrong somewhere, what is the correct way to go about adding upon initWithCoorinate?
Put the prototype -(id)initWithCoordinate:(CLLocationCoordinate2D)coord andObject:(NSManagedObject *)object in .h file.