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
and .m file
and then add following code to viewdidload
and implement following method
Follow this tutorial:code with explanation is given: