2010-06-25 73 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. 
}