Mapkit with multi annotation (callout), mapping the next view - iphone

Wanted some help with a problem with mapkit I am facing. Should be a silly problem or I have missed out something while going through the mapkit framework.
Here is the senario.
I am placing multiple annotation on the map when the user performs some search like pizza.
Added button for the right annotation view, on click which opens a next detail view. The problem is how to send some information to the next view, for example I add index to annotations while creating them, now I want to access this information from annotation, and pass it to the next view via the selector set on the button.
I have checked all the mapkit delicate, but don't find a one where I can map this information with the next view and annotation.
Hope I have not confused you guys in my question. Please let me know I will reframe it.
Thaking in advance.

When you create the UIButton for the annotation, set the tag property (tag is an NSInteger property of UIView) to an id or array index that identifies the relevant object. You can then retrieve that tag value from the sender parameter to your selector.
Edit: here's some sample code.
You create your annotation view and associate the button in your delegate's -mapView:viewForAnnotation: method:
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
// Boilerplate pin annotation code
MKPinAnnotationView *pin = (MKPinAnnotationView *) [self.map dequeueReusableAnnotationViewWithIdentifier: #"restMap"];
if (pin == nil) {
pin = [[[MKPinAnnotationView alloc] initWithAnnotation: annotation reuseIdentifier: #"restMap"] autorelease];
} else {
pin.annotation = annotation;
}
pin.pinColor = MKPinAnnotationColorRed
pin.canShowCallout = YES;
pin.animatesDrop = NO;
// now we'll add the right callout button
UIButton *detailButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
// customize this line to fit the structure of your code. basically
// you just need to find an integer value that matches your object in some way:
// its index in your array of MKAnnotation items, or an id of some sort, etc
//
// here I'll assume you have an annotation array that is a property of the current
// class and we just want to store the index of this annotation.
NSInteger annotationValue = [self.annotations indexOfObject:annotation];
// set the tag property of the button to the index
detailButton.tag = annotationValue;
// tell the button what to do when it gets touched
[detailButton addTarget:self action:#selector(showDetailView:) forControlEvents:UIControlEventTouchUpInside];
pin.rightCalloutAccessoryView = detailButton;
return pin;
}
Then in your action method, you'll unpack the value from tag and use it to display the right detail:
-(IBAction)showDetailView:(UIView*)sender {
// get the tag value from the sender
NSInteger selectedIndex = sender.tag;
MyAnnotationObject *selectedObject = [self.annotations objectAtIndex:selectedIndex];
// now you know which detail view you want to show; the code that follows
// depends on the structure of your app, but probably looks like:
MyDetailViewController *detailView = [[MyDetailViewController alloc] initWithNibName...];
detailView.detailObject = selectedObject;
[[self navigationController] pushViewController:detailView animated:YES];
[detailView release];
}

In the Annotation view, is it possible to grab,say, the Title or Subtitle or any other information you used while creating pins? What i am looking to do is have a certain image popup in the annotation based on one of those variables.
#import "MapPin.h"
#implementation MapPin
#synthesize coordinate;
#synthesize title;
#synthesize subtitle;
#synthesize indexnumber;
#synthesize imageFile;
-(id)initWithCoordinates:(CLLocationCoordinate2D)location
placeName: placeName
description:description
indexnum:indexnum
imageFileLoc:imageFileLoc{
self = [super init];
if (self != nil) {
imageFile=imageFileLoc;
[imageFile retain];
indexnumber=indexnum;
[indexnumber retain];
coordinate = location;
title = placeName;
[title retain];
subtitle = description;
[subtitle retain];
}
return self;
}
-(void)addAnnotations {
// Normally read the data for these from the file system or a Web service
CLLocationCoordinate2D coordinate = {35.9077803, -79.0454936};
MapPin *pin = [[MapPin alloc]initWithCoordinates:coordinate
placeName:#"Keenan Stadium"
description:#"Tar Heel Football"
indexnum:#"1"
imageFileLoc:#"owl.jpg"];
[self.map addAnnotation:pin];

Another option:
You can implement these methods:
- (void)mapView:(MKMapView *)mapView didSelectAnnotation:(MKAnnotationView *)view;
- (void)mapView:(MKMapView *)mapView didDeselectAnnotation:(MKAnnotationView *)view;

Related

iphone: How to show different image for every pin point on MapKit?

I want to put Start and End image with overlay in an iPhone/iPad application. I have start and end Lattitude and Longitude values and want to draw overlay between start and end points and put start image on Start point and End Image on End point.
I have googled but What I found is MapKit gets one image and set it on both Start and End points, could not find any help for 2nd image.
like
annotationView.image=[UIImage imageNamed:#"parkingIcon.png"];
It only set one image for both start and end points. But I want to put different images for both points.
Please help.
I got that ... thanks for all who tried to help me out. the Complete solution is
Create a class
#interface MyAnnotationClass : NSObject <MKAnnotation> {
NSString *_name;
NSString *_description;
CLLocationCoordinate2D _coordinate;
}
#property (nonatomic, retain) NSString *name;
#property (nonatomic, retain) NSString *description;
#property (nonatomic, readonly) CLLocationCoordinate2D coordinate;
-(id) initWithCoordinate:(CLLocationCoordinate2D) coordinate;
ViewDidLoad method code :
mapView.delegate = self;
//Initialize annotation
MyAnnotationClass *commuterLotAnnotation=[[MyAnnotationClass alloc] initWithCoordinate:CLLocationCoordinate2DMake( 39.047752, -76.850388)];
commuterLotAnnotation.name = #"1";
MyAnnotationClass *overflowLotAnnotation=[[MyAnnotationClass alloc] initWithCoordinate:CLLocationCoordinate2DMake( 39.047958, -76.852520)];
overflowLotAnnotation.name = #"2";
//Add them to array
self.myAnnotations=[NSArray arrayWithObjects:commuterLotAnnotation, overflowLotAnnotation, nil];
//Release the annotations now that they've been added to the array
[commuterLotAnnotation release];
[overflowLotAnnotation release];
//add array of annotations to map
[mapView addAnnotations:_myAnnotations];
viewForAnnotation code :
-(MKAnnotationView *)mapView:(MKMapView *)MapView viewForAnnotation:(id<MKAnnotation>)annotation{
static NSString *parkingAnnotationIdentifier=#"ParkingAnnotationIdentifier";
if([annotation isKindOfClass:[MyAnnotationClass class]]){
//Try to get an unused annotation, similar to uitableviewcells
MKAnnotationView *annotationView=[MapView dequeueReusableAnnotationViewWithIdentifier:parkingAnnotationIdentifier];
//If one isn't available, create a new one
if(!annotationView){
annotationView=[[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:parkingAnnotationIdentifier];
if([((MyAnnotationClass *)annotation).name isEqualToString: #"1"]){
annotationView.image=[UIImage imageNamed:#"passenger.png"];
}
else if([((MyAnnotationClass *)annotation).name isEqualToString: #"2"]){
annotationView.image=[UIImage imageNamed:#"place.png"];
}
}
return annotationView;
}
return nil;
}
This is how you can add separate image for every point on MapKit.
you have to use the -(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation; , a MKAnnotation delegate method
wich allow you to set a given annotation on the map.
This method is returned for every annotations, you just have to find the end and start annotations and set image to it
First, you have to implement the MKMapViewDelegate protocol and set the delegate class to your map.
In the MKMapView Delegate, declare the method:
- (MKAnnotationView *)mapView:(id)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
if (annotation == [mapView userLocation]) // The user's blue point : no customisation.
return nil;
MKAnnotationView * annView = [mapView dequeueReusableAnnotationViewWithIdentifier:/* your specific identifier */];
if (annView == nil) {
annView = [[[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:/* your specific identifier */] autorelease];
}
annView.image = /* your image */;
return annView
}
But you have to detect if the annotation is the starting one or the ending one. You can set a specific title to easily retrieve it (like #"start") or subclass your MKAnnotations by adding a discriminating property.

How to keep data associated with MKAnnotation from being lost after a callout pops up and user taps disclosure button?

How do I keep data associated with an MKAnnotation object after the user taps the pin, sees a callout, and taps the disclosure button which opens a detailed view controller? I want to display all data associated with the pin in the detail view controller.
I have a simple MKAnnotation class that looks like:
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
#interface VoiceMemoryAnnotation : NSObject <MKAnnotation> {
NSString * blobkey;
}
#property (nonatomic, retain) NSString * blobkey;
-(id)initWithBlobkey:(NSString *) key andCoordinate:(CLLocationCoordinate2D) c;
#end
I implemented the call back "viewForAnnotation"
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
MKPinAnnotationView*singleAnnotationView = [[MKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:nil];
// PM: this pin will have a callout (i.e. dont' forget to override title function! Else exception thrown)
singleAnnotationView.canShowCallout = YES;
// PM: add disclosure button
UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
// PM: when user taps disclosure button, bring them to another page with details about the selected voice memory
[rightButton addTarget:self action:#selector(showPinDetails:) forControlEvents:UIControlEventTouchUpInside];
singleAnnotationView.rightCalloutAccessoryView = rightButton;
return singleAnnotationView;
}
If I understand correctly, the above method is called when you add a VoiceMemoryAnnotation to a map object. When this viewForAnnotation is called, I simply allocate a MKPinAnnotationView object and return it. When the user taps this retuned pin, they see the callout. As soon as they click the disclosure button it calls "showPinDetails":
- (void)showPinDetails:(id)sender
{
detailViewController = [[MemoryDetailViewController alloc]initWithNibName:#"MemoryDetailViewController" bundle:nil];
[self presentModalViewController:detailViewController animated:YES];
}
The problem is the "sender" object does not contain any information about which pin was selected. Is there some way I can pass in the selected annotation to the showPinDetails method?
In the showPinDetails: method, you can get the currently selected annotation from the map view's selectedAnnotations property.
That property is an NSArray but since the map view only allows one annotation to be selected at a time, you would just use the object at index 0. For example:
- (void)showPinDetails:(id)sender
{
if (mapView.selectedAnnotations.count == 0)
{
//no annotation is currently selected
return;
}
id<MKAnnotation> selectedAnn = [mapView.selectedAnnotations objectAtIndex:0];
if ([selectedAnn isKindOfClass[VoiceMemoryAnnotation class]])
{
VoiceMemoryAnnotation *vma = (VoiceMemoryAnnotation *)selectedAnn;
NSLog(#"selected VMA = %#, blobkey=%#", vma, vma.blobkey);
}
else
{
NSLog(#"selected annotation (not a VMA) = %#", selectedAnn);
}
detailViewController = [[MemoryDetailViewController alloc]initWithNibName:#"MemoryDetailViewController" bundle:nil];
[self presentModalViewController:detailViewController animated:YES];
}
Instead of using a custom button action method, it can be easier to use the map view's calloutAccessoryControlTapped delegate method which lets you get access to the selected annotation more directly. In viewForAnnotation, remove the addTarget and just implement the delegate method:
- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view
calloutAccessoryControlTapped:(UIControl *)control
{
id<MKAnnotation> selectedAnn = view.annotation;
if ([selectedAnn isKindOfClass[VoiceMemoryAnnotation class]])
{
VoiceMemoryAnnotation *vma = (VoiceMemoryAnnotation *)selectedAnn;
NSLog(#"selected VMA = %#, blobkey=%#", vma, vma.blobkey);
}
else
{
NSLog(#"selected annotation (not a VMA) = %#", selectedAnn);
}
//do something with the selected annotation...
}

Conditional Custom Annotation View in iphone MapView

In my MapView, i read data from SQLite and display pins on it (up to 5000 record).
the database has the structure of ID| Longitude| Latitude | Title | subtitle
i used this code to make the pin clickable:
pin.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
i need to add a new column (Clickable) in the database, and make the pin clickable just if the "Clickable" value is ON.
any detailed suggestion about the best idea to do that?
From my experience if you don't set any property of the annotation (title,subTitle,image,accessory-button) and tap on the pin, the callout is not displayed.
Instead, if you want show the callout but not call an action when the accessory button is tapped, you could use a thing like this:
(After downloading the data from the db, you could store each item as a NSDictionary and then all items in a NSArray)
- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control{
NSString *clickable=[[yourArray objectAtIndex:yourIndex] objectForKey:#"clickable"];
if(![clickable isEqualToString:#"YES"]){
return;
}
}
Of course I used a string as example, you may also use NSNumbers or BOOLs.
Hope i understood your question.
To control whether the callout has the accessory button based on the "clickable" flag in the table, I suggest you add a clickable property to your annotation class and set it when adding the annotation. Then you can check this property when creating the annotation view in the viewForAnnotation method.
To add the annotations to the map view (ie. when you call addAnnotation), if you are currently using a pre-defined class like MKPointAnnotation, you'll need to instead define your own custom class that implements the MKAnnotation protocol. If you already have a custom class, add a clickable property to it.
Here's an example of a custom annotation class:
//CustomAnnotation.h...
#interface CustomAnnotation : NSObject<MKAnnotation>
#property (nonatomic, assign) CLLocationCoordinate2D coordinate;
#property (nonatomic, copy) NSString *title;
#property (nonatomic, copy) NSString *subtitle;
#property (nonatomic, assign) int annotationId;
#property (nonatomic, assign) BOOL clickable;
#end
//CustomAnnotation.m...
#implementation CustomAnnotation
#synthesize coordinate;
#synthesize title;
#synthesize subtitle;
#synthesize annotationId;
#synthesize clickable;
- (void)dealloc {
[title release];
[subtitle release];
[super dealloc];
}
#end
Here's an example of how you would create and add the annotations (the actual values for the annotation properties will come from your sql rows):
CustomAnnotation *ca1 = [[CustomAnnotation alloc] init];
ca1.annotationId = 1;
ca1.coordinate = CLLocationCoordinate2DMake(lat1,long1);
ca1.title = #"clickable";
ca1.subtitle = #"clickable subtitle";
ca1.clickable = YES;
[myMapView addAnnotation:ca1];
[ca1 release];
CustomAnnotation *ca2 = [[CustomAnnotation alloc] init];
ca2.annotationId = 2;
ca2.coordinate = CLLocationCoordinate2DMake(lat2,long2);
ca2.title = #"not clickable";
ca2.subtitle = #"not clickable subtitle";
ca2.clickable = NO;
[myMapView addAnnotation:ca2];
[ca2 release];
Then the viewForAnnotation will look like this:
-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
static NSString *reuseId = #"CustomAnnotation";
MKPinAnnotationView* customPinView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:reuseId];
if (customPinView == nil) {
customPinView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:reuseId] autorelease];
customPinView.pinColor = MKPinAnnotationColorPurple;
customPinView.animatesDrop = YES;
customPinView.canShowCallout = YES;
}
else {
customPinView.annotation = annotation;
}
CustomAnnotation *ca = (CustomAnnotation *)annotation;
if (ca.clickable)
customPinView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
else
customPinView.rightCalloutAccessoryView = nil;
return customPinView;
}
Finally, you can handle the button press in the calloutAccessoryControlTapped delegate method and access your custom annotation's properties:
-(void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
CustomAnnotation *ca = (CustomAnnotation *)view.annotation;
NSLog(#"annotation tapped, id=%d, title=%#", ca.annotationId, ca.title);
}

selector adding parameters

What I want to do is basically send some form of id with my button so I can calculate which one has been clicked. My buttons get dynamically created and all of them execute the same function that loads a new view, I want to know which button was clicked so I can display the correct data on the new view.
I have an NSMutableArray of annotations that I add a button to the details of the pin. The button works and it loads the next view, but I want to figure out which button was pressed.
I use a singleton with an NSMutableArray called array. The singleton name is Helper
What I did was:
Helper* array = [Helper sharedManager];
int clickedNum = [array.array indexOfObject:annotation];
[myDetailButton setTag:clickedNum];
[myDetailButton addTarget:self action:#selector(moreInfo:event:) forControlEvents:UIControlEventTouchUpInside];
That is where i create the annotations that go unto the map. The next line is my function ID
- (IBAction)moreInfo:(UIButton*)sender
And this is where I want to retrieve the tag
Helper *array = [Helper sharedManager];
array.clicked = sender.tag;
When I run this and click on the button in one of my annotations I get an exception saying NSInvalidArgumentException, reason: -(MapViewController moreInfo:event:]
Any help would be appriciated
Edit:
As requested the interface for helper:
#interface Helper : NSObject {
NSMutableArray *array;
int clicked;
}
#property (nonatomic, retain) NSMutableArray *array;
#property (nonatomic) int clicked;
+(id)sharedManager;
#end
Also I thought it wise to add:
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
static NSString *identifier = #"Events";
MKPinAnnotationView *retval = nil;
if ([annotation isKindOfClass:[Events class]])
{
(MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
if (retval == nil) {
retval = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier] autorelease];
Helper *array = [Helper sharedManager];
int clickedNum = [array.array indexOfObject:annotation];
// Set up the Left callout
UIButton *myDetailButton = [UIButton buttonWithType:UIButtonTypeCustom];
myDetailButton.frame = CGRectMake(0, 0, 23, 23);
myDetailButton.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
myDetailButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
[myDetailButton setTag:clickedNum];
NSLog([NSString stringWithFormat:#"Testing tag: %#", clickedNum]);
[myDetailButton addTarget:self action:#selector(moreInfo:) forControlEvents:UIControlEventTouchUpInside];
// Set the image for the button
[myDetailButton setImage:[UIImage imageNamed:#"icon72.png"] forState:UIControlStateNormal];
// Set the button as the callout view
retval.leftCalloutAccessoryView = myDetailButton;
}
if (retval) {
[retval setPinColor:MKPinAnnotationColorGreen];
retval.animatesDrop = YES;
retval.canShowCallout = YES;
}
}
return retval;
}
Your method moreInfo: doesn't match what you have set as the selector in
[myDetailButton addTarget:self action:#selector(moreInfo:event:) forControlEvents:UIControlEventTouchUpInside];
So change that to
[myDetailButton addTarget:self action:#selector(moreInfo:) forControlEvents:UIControlEventTouchUpInside];
Or change the method definition.
Although Deepak's answer is correct and will work, since your buttons are annotation callout accessory views, there is an easier way to tell which annotation's button was selected.
Instead of adding your own target+action, use the map view's mapView:annotationView:calloutAccessoryControlTapped: delegate method which will get called when a callout view is tapped. There, the annotation is available as view.annotation. You don't need to set the button tag or use the annotation's index.
For an example, see this other answer.
A separate issue is that in your viewForAnnotation method, you are calling dequeueReusableAnnotationViewWithIdentifier but not doing anything with the result (so not getting any re-use). Instead, it should be assigned to retval and inside the last if, set retval.annotation.

MKAnnotationView and passing data to another function

Actually I'm developing something in my app which displays annotations within my MKMapView and those annotations should be clickable and pass a param to a method.
The problem is for now how to pass the parameter to the next function when tapping the disclosure button.
For the formatting of the annotations I use the - (MKAnnotationView *)mapView:(MKMapView *)aMapView viewForAnnotation:(id <MKAnnotation>)annotation method to display the disclosure button. But the problem is, how to get values out of the Annotation object which is displayed at the moment?
My code for the Annotation (only the relevant part):
#synthesize coordinate, id, title, subtitle;
+ (id)annotationWithCoordinate:(CLLocationCoordinate2D)coordinate {
return [[[[self class] alloc] initWithCoordinate:coordinate] autorelease];
}
- (id)initWithCoordinate:(CLLocationCoordinate2D)coordinate {
self = [super init];
if(nil != self) {
self.coordinate = coordinate;
}
return self;
}
And when touching the disclosure button the method - (void)displayInfo:(NSNumber *) anId; should be called with handing over Annotation.id as the anId param.
The disclosure button is generated with that code:
UIButton *disclosureButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
dropPin.rightCalloutAccessoryView = disclosureButton;
dropPin.animatesDrop = YES;
dropPin.canShowCallout = YES;
So - the question:
How can I pass that param to the function OR read the id of the clicked Annotation?
I thought I could do the handling of the tap on disclosureButton either with a selector for UIButton (but - how to pass the id?) or with - (void)mapView:(MKMapView *)mapView
annotationView:(MKAnnotationView *)view
calloutAccessoryControlTapped:(UIControl *)control (again - how to pass the id?).
I hope you know what I mean and very big thanks in advance for your answers! :)
In calloutAccessoryControlTapped the annotation is available via the view:
MKAnnotation *annotation = view.annotation;