2013-03-13 53 views
3

我想要創建一個顯示精確位置(座標:緯度45.733333°和經度9.133333°)的mapView,並添加帶標題和副標題的簡單註釋。 ma必須是靜態的,不需要擁有當前的用戶位置或其他任何東西。mapView在IO中顯示特定位置和註釋

我在標籤式應用程序的簡單視圖控制器中添加了地圖查看器。 我有我的MapViewController文件,我有他們連接(我猜)我的ViewController,但我似乎無法找到一個教程來做我需要的東西,其他人很複雜,我認爲他們做了很多,我是什麼非常需要,而且我是初學者,所以我遇到了一些麻煩。

謝謝!

回答

2

試試這個:

CLLocationCoordinate2D coord; 
    coord.latitude = 45.733333; 
    coord.longitude = 9.133333; 

    MKAnnotation *ann = [[MKAnnotation alloc] initWithLocation:coord]; 
    [ann setTitle:@"Title"]; 
    [ann setSubtitle:@"Subtitle"]; 
    [map addAnnotation:ann]; 

不要忘記變量更改爲您的變量名。

編輯

解決方案:

創建一個類AddressAnnotation並粘貼此代碼:

AddressAnnotation.h

#import <Foundation/Foundation.h> 
#import <MapKit/MapKit.h> 

@interface AddressAnnotation : NSObject <MKAnnotation> { 
    CLLocationCoordinate2D _coordinate; 
    NSString *_title; 
} 

@property (nonatomic, retain) NSString *title; 

-(id)initWithCoordinate:(CLLocationCoordinate2D)c; 

@end 

AddressAnnotation.m

#import "AddressAnnotation.h" 

@implementation AddressAnnotation 

- (NSString *)subtitle{ 
    return nil; 
} 

- (NSString *)title{ 
    return _title; 
} 

- (CLLocationCoordinate2D)coordinate{ 
    return _coordinate; 
} 

-(id)initWithCoordinate:(CLLocationCoordinate2D)c{ 
    _coordinate = c; 
    return self; 
} 

@end 

現在,這裏是處理地圖和創建註釋類:

MapViewController.h

#import <UIKit/UIKit.h> 
#import <MapKit/MapKit.h> 

@interface MapViewController : UIViewController 

@property (nonatomic, retain) IBOutlet MKMapView *mapView; 

- (IBAction)changeMapType:(UISegmentedControl *)control; 

@end 

MapViewController.m

#import "MapViewController.h" 
#import "AddressAnnotation.h" 

@interface MapViewController() 

@end 

@implementation MapViewController 

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{ 
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
    if (self) { 
     // Custom initialization 
    } 
    return self; 
} 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view from its nib. 

    CLLocationCoordinate2D location; 
    location.latitude = -15.830731; // change to your coordinate latitude 
    location.longitude = -47.8753; // change to your coordinate longitude 

    AddressAnnotation *addAnnotation = [[AddressAnnotation alloc] initWithCoordinate:location]; 
    addAnnotation.title = [NSString stringWithFormat:@"Title for you annotation"]; 

    MKCoordinateRegion region; 
    MKCoordinateSpan span; 
    span.latitudeDelta = 0.002;  // 0.0 is min value u van provide for zooming 
    span.longitudeDelta= 0.002; 

    region.span=span; 
    region.center =location;  // to locate to the center 
    [_mapView addAnnotation:addAnnotation]; 
    [_mapView setRegion:region animated:TRUE]; 
    [_mapView regionThatFits:region]; 

} 

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

@end 
+0

謝謝,但我怕我需要更具體的解釋。我可以將代碼放在.h和.m文件中嗎?我想確保我完成了所有其他代碼(我使用了不同的教程,恐怕我搞砸了)。謝謝! – Ivy 2013-03-13 21:31:58

+0

這是我的空代碼,就像我創建我的控制器一樣。 [鏈接](http://dl.dropbox.com/u/1074257/code_initial.txt) – Ivy 2013-03-13 21:42:12

+0

你必須添加一個MKMapView到你的視圖。你做到了嗎? – CainaSouza 2013-03-13 21:44:10