开发者

How to create an MKMapView?

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 l开发者_开发知识库ongitude 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.
}


(void)viewDidLoad { 
    [super viewDidLoad];
    MKMapView *myMapView = [[MKMapView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    [self.view addSubview:myMapView];
 }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜