2010-06-25 89 views
9

这个文档没有太多介绍它,似乎没有init方法呢?我将如何创建一个并设置要在地图视图中显示的经度和纬度或区域?如何创建一个MKMapView?

+0

示例代码:http://developer.apple。 com/iphone/library/samplecode/MapCallouts/Introduction/Intro.html – lukya 2010-06-25 07:07:02

回答

2

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

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

+0

你应该能够使用'initWithFrame'(在UIView文档中)分配和初始化视图,然后将视图作为子视图添加到父视图。 – 2010-06-25 20:57:41

5

您可以通过两种代码或由Interface Builder包括的MKMapView。

对于界面生成器,只需将它&它拖放到厦门国际银行。(工具 - >库 - > MapView类)

通过代码

在您的.h文件中

MKMapView * mapView; 

在你.m文件

-(void)viewWillAppear:(BOOL)animated 
{ 
    self.mapView = [[[MKMapView alloc] initWithFrame:self.view.frame] autorelease]; 
    [self.view addSubview:self.mapView];    
} 
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]; 
} 
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]; 
} 
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. 
}