Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create an MKMapView?

Tags:

iphone

The documentation doesn't talk much about it, and there seems to be no init method for this? How would I create one and set the longitude and latitude or region to display in the map view?

like image 642
dontWatchMyProfile Avatar asked Jun 25 '10 06:06

dontWatchMyProfile


4 Answers

First, add MapKit.framework.
Then, in .h file

#import <MapKit/MapKit.h>

and add delegate <MKMapViewDelegate>.

Then, in .m File, add the following code:

- (void)viewDidLoad
{
     [super viewDidLoad];

     MKMapView *mapView = [[MKMapView alloc] initWithFrame:self.view.frame];
     [self.view addSubview:mapView];
}
like image 91
Ankit Goyal Avatar answered Sep 27 '22 22:09

Ankit Goyal


You can include MKMapView both by code or by Interface builder.

For Interface builder just drag it & drop it to your xib.(Tools->Library->MapView)

By code

In your .h file

MKMapView * mapView;

In your .m file

-(void)viewWillAppear:(BOOL)animated 
{   
    self.mapView = [[[MKMapView alloc] initWithFrame:self.view.frame] autorelease];
    [self.view addSubview:self.mapView];            
}
like image 32
raaz Avatar answered Sep 27 '22 22:09

raaz


Interface builder includes the MKMapView (Map View). Drag the element into your XIB, add a referencing outlet in your controller, link them up. Then, set the region. Lots of good examples:

http://developer.apple.com/iphone/library/samplecode/WorldCities/Introduction/Intro.html#//apple_ref/doc/uid/DTS40009466

like image 39
Kevin Sylvestre Avatar answered Sep 27 '22 21:09

Kevin Sylvestre


mapview sample coding to find an location

@interface mapViewController ()

@end

@implementation mapViewController

- (void)viewDidLoad {
[super viewDidLoad];


self.title=self.name;

 CLLocationCoordinate2D myCoordinate = 
 _mapView.userLocation.coordinate;
  myCoordinate.latitude =[self.lat doubleValue];
 myCoordinate.longitude =[self.lng doubleValue];


 //    NSLog(@"--->%@",self.lat);
 //     NSLog(@"--->%@",self.lng);
 //set location and zoom level
 MKCoordinateRegion viewRegion = 
  MKCoordinateRegionMakeWithDistance(myCoordinate, 1000, 1000);
  MKCoordinateRegion adjustedRegion = [self.mapView   
  regionThatFits:viewRegion];
 [self.mapView setRegion:adjustedRegion animated:YES];

 MKPointAnnotation *point = [[MKPointAnnotation alloc] init];
 // Set your annotation to point at your coordinate
 point.coordinate = myCoordinate;
 point.title = self.address;

//Drop pin on map
[self.mapView addAnnotation:point];

self.mapView.delegate = self;
// Do any additional setup after loading the view.
}
like image 27
sasidharan.M Avatar answered Sep 27 '22 22:09

sasidharan.M