如何创建MKMapView?

时间:2010-06-25 06:56:49

标签: iphone

文档没有多少讨论它,似乎没有init方法吗?如何创建一个并设置要在地图视图中显示的经度和纬度或区域?

5 个答案:

答案 0 :(得分:20)

首先,添加MapKit.framework 然后,在.h文件中

#import <MapKit/MapKit.h>

并添加委托<MKMapViewDelegate>

然后,在.m文件中,添加以下代码:

- (void)viewDidLoad
{
     [super viewDidLoad];

     MKMapView *mapView = [[MKMapView alloc] initWithFrame:self.view.frame];
     [self.view addSubview:mapView];
}

答案 1 :(得分:5)

您可以通过代码或“接口”构建器包含MKMapView。

对于“界面”构建器,只需拖动它即可。将它放到你的xib。(工具 - >图书馆 - > MapView)

按代码

在你的.h文件中

MKMapView * mapView;

在你的.m文件中

-(void)viewWillAppear:(BOOL)animated 
{   
    self.mapView = [[[MKMapView alloc] initWithFrame:self.view.frame] autorelease];
    [self.view addSubview:self.mapView];            
}

答案 2 :(得分:2)

接口构建器包含MKMapView(地图视图)。将元素拖到XIB中,在控制器中添加引用插座,将它们链接起来。然后,设置区域。很多很好的例子:

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

答案 3 :(得分:1)

mapview示例编码以查找位置

@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.
}

答案 4 :(得分:0)

(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];
 }