Iphone – Displaying Pin with Title (annotation) once map loads

annotationsiphonemapkitxcode

I am working on my first app and within it I'm just trying to have on button click show a map with a pin (and title on this pin of location). I was able to load the mapview and to have it show the coordinates I want. But when trying to display the pin and annotation I am having issues. Not sure where to code this and how to make annotation to display pin. I've searched and seen many tutorials, but most show a different mapview style and are showing pin on user selection, I want to show pin on load of map.

Here is the code I have to show the map which is working, but has no pin display or annotation:

FirstLocateViewController.m code:

#import "FirstLocateViewController.h"

@implementation FirstLocateViewController

@synthesize dismissViewButton;

-(IBAction)dismissView:(id)sender {
[self dismissModalViewControllerAnimated:YES];
}

- (void)viewDidLoad {
[super viewDidLoad];
CGRect frame = CGRectMake(0,0, 320,420);
mapView = [[MKMapView alloc] initWithFrame:frame];
mapView.mapType = MKMapTypeStandard;
CLLocationCoordinate2D coord = {latitude: 12.3456, longitude: -7.890};
MKCoordinateSpan span = {latitudeDelta: 0.05, longitudeDelta: 0.05};
MKCoordinateRegion region = {coord, span};
[mapView setRegion:region];
[self.view addSubview:mapView];

}


FirstLocateViewController.h code:

#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#import <MapKit/MKAnnotation.h>


@interface FirstLocateViewController : UIViewController <MKMapViewDelegate> {
UIButton *dismissViewButton;
MKMapView *mapView;
}

@property (nonatomic, retain) IBOutlet UIButton *dismissViewButton;

- (IBAction)dismissViewButton:(id)sender;

@end

Thank you in advanced for any significant help.

Best Answer

For that you need to create annotation create one class which has CLLocationCoordinate2D,title,subtitle like this .h file

#import <Foundation/Foundation.h>
#import <MapKit/MKAnnotation.h>


@interface DisplayMap : NSObject <MKAnnotation> {

    CLLocationCoordinate2D coordinate; 
    NSString *title; 
    NSString *subtitle;
}
@property (nonatomic, assign) CLLocationCoordinate2D coordinate; 
@property (nonatomic, copy) NSString *title; 
@property (nonatomic, copy) NSString *subtitle;

@end

and .m file

#import "DisplayMap.h"


@implementation DisplayMap

@synthesize coordinate,title,subtitle;


-(void)dealloc{
    [title release];
    [super dealloc];
}

@end

and then add following code to viewdidload

DisplayMap *ann = [[DisplayMap alloc] init]; 
ann.title=@"put title here";
ann.coordinate = region.center; 
[mapView addAnnotation:ann];

and implement following method

-(MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation:
(id <MKAnnotation>)annotation {
    MKPinAnnotationView *pinView = nil; 
    if(annotation != mapView.userLocation) 
    {
        static NSString *defaultPinID = @"com.invasivecode.pin";
        pinView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
        if ( pinView == nil ) pinView = [[[MKPinAnnotationView alloc]
                                          initWithAnnotation:annotation reuseIdentifier:defaultPinID] autorelease];

        pinView.pinColor = MKPinAnnotationColorPurple; 
        pinView.canShowCallout = YES;
        pinView.animatesDrop = YES;
    } 
    else {
        [mapView.userLocation setTitle:@"I am here"];
    }
    return pinView;
}

Follow this tutorial:code with explanation is given: