MapKit and annotations with button to link a new View
All the documents and examples I found on the Net show how to use mapkit with annotations, but I haven’t found a tutorial about putting a link button inside the annotation (like in the Google Maps for iPhone application).
Follow this interesting tutorial in 3 parts for start with Map Kit.
Declare an interface for the single point. Put this into the .h file of the UIViewController that controls the mapview.
So will be something like:
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#import <MapKit/MKAnnotation.h>
#import <CoreLocation/CoreLocation.h>
#import “ShowPOIViewController.h”
@interface GPSAnnotation : NSObject<MKAnnotation> {
CLLocationCoordinate2D coordinate;
NSString *mTitle;
NSString *mSubTitle;
NSNumber *currentPoint;
}
@property(nonatomic, retain) NSString *mTitle;
@property(nonatomic, retain) NSString *mSubTitle;
@property(nonatomic, retain) NSNumber *currentPoint;
@end
@interface GPSViewController : UIViewController <MKMapViewDelegate, CLLocationManagerDelegate> {
MKMapView *mapView;
MKPlacemark *mPlacemark;
CLLocationCoordinate2D location;
IBOutlet UISegmentedControl *mapType;
ShowPOIViewController *showPOIView;
}
@property (nonatomic, retain) ShowPOIViewController *showPOIView;
@end
In the .m file of the UIViewController you have to add also the implementation of the GPSAnnotation
@implementation GPSAnnotation
@synthesize coordinate;
@synthesize mTitle;
@synthesize mSubTitle;
@synthesize currentPoint;
- (NSString *)subtitle{
return mSubTitle;
}
- (NSString *)title{
return mTitle;
}
-(id)initWithCoordinate:(CLLocationCoordinate2D) c{
coordinate=c;
NSLog(@”%f,%f”,c.latitude,c.longitude);
return self;
}
@end
before the @implementation GPSViewController
For add one or more points to the map inside the viewDidLoad() put something like:
GPSAnnotation *annotation = [[GPSAnnotation alloc] initWithCoordinate:c];
annotation.currentPoint = [NSNumber numberWithInt:1]; // this is necessary to identify the point if more than one. It is a progressive number
annotation.mTitle=@”Point of interest one”;
[mapView addAnnotation:annotation];
[annotation release];
GPSAnnotation *annotation = [[GPSAnnotation alloc] initWithCoordinate:c];
annotation.currentPoint = [NSNumber numberWithInt:2]; // this is necessary to identify the point if more than one
annotation.mTitle=@”Point of interest two”;
[mapView addAnnotation:annotation];
[annotation release];
Than for create the button inside the annotation you have to:
- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(GPSAnnotation *) annotation {MKPinAnnotationView *annView=
int postag = 0;
[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@”currentloc”];
annView.pinColor = MKPinAnnotationColorGreen;
// Define a button
UIButton *myDetailButton = [UIButton buttonWithType:UIButtonTypeCustom];
myDetailButton.frame = CGRectMake(0, 0, 23, 23);
myDetailButton.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
myDetailButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
// Set the image for the button and add an action to the button
// showLinks is an IBAction that changes view
[myDetailButton setImage:[UIImage imageNamed:@"button_right.png"] forState:UIControlStateNormal];
[myDetailButton addTarget:self action:@selector(showLinks:) forControlEvents:UIControlEventTouchUpInside];
// To every button I have to add a tag. The tag identifies which button was pressed
// A button appears automatically and is the “Current Location” of GPS system, so I give it the value 99999
if ([[annotation title] isEqualToString:@”Current Location”]) {
postag = 99999;
} else {
postag = [annotation.currentPoint intValue];
}
//Assign the value to the tag
myDetailButton.tagĀ = postag;
// Set the button as the callout view
annView.rightCalloutAccessoryView = myDetailButton;
annView.animatesDrop=TRUE;
annView.canShowCallout = YES;
annView.calloutOffset = CGPointMake(-5, 5);
return annView;
}
- (IBAction)showLinks:(id)sender
{
int whichButtonPressed = ((UIButton *)sender).tag;
// PUT HERE THE CODE NECESSARY TO ACTIVATE THE VIEW TO SHOW THE DETAILS
// SOMETHING LIKE THIS IF YOU USE A NAVIGATIONCONTROLLER:
[self.navigationController pushViewController:self.showPOIView animated:YES];
self.showPOIView.title = [[examplepoints objectAtIndex:whichButtonPressed] title];
}
And now… have fun with MapKit