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?
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];
}
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];
}
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
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.
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With