2014-09-23 4 views
0

내 간단한지도보기에 대한 맞춤 주석을 만들려고하지만 시뮬레이터를 실행할 때 annotonon 핀이지도에 나타나지 않습니다.지도보기에 MKAnnotation이 표시되지 않습니다.

MKAnnotation 프로토콜을 구현하는 MapViewAnnotation 클래스가 있고 프로그래밍 방식으로 mapView를 만들고지도를 표시하는 또 다른 MapViewController 클래스가 있습니다.

MapViewAnnotation.h :

#import <Foundation/Foundation.h> 
#import <MapKit/MapKit.h> 
@interface MapViewAnnotation : NSObject <MKAnnotation> 

@property (nonatomic, copy) NSString *title; 
@property (nonatomic, readonly) CLLocationCoordinate2D coordinate; 

-(id) initWithTitle: (NSString *) title AndCoordinate: (CLLocationCoordinate2D) coordinate; 

@end 

MapViewAnnotation.m :

#import "MapViewAnnotation.h" 

@implementation MapViewAnnotation 

@synthesize coordinate = _coordinate; 
@synthesize title = _title; 

-(id) initWithTitle:(NSString *)title AndCoordinate:(CLLocationCoordinate2D)coordinate{ 
    self = [super init]; 
    _title = title; 
    _coordinate = coordinate; 
    return self; 
} 
@end 

MapViewController.h :

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

@interface MapViewController : UIViewController 

@property (weak, nonatomic) MKMapView *mapView; 

-(void) goToLocation; 
@end 

MapViewController.m :

#import "MapViewController.h" 
#import "MapViewAnnotation.h" 

#define LATITUDE 42.3889; 
#define LONGITUDE -72.5278; 


@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]; 
    self.mapView = [[MKMapView alloc] initWithFrame:self.view.frame]; 
    [self.view addSubview:self.mapView]; 
    [self goToLocation]; 
    [self.mapView addAnnotations:[self createAnnotations]]; 

} 


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

-(void) goToLocation { 
    MKCoordinateRegion newRegion; 

    newRegion.center.latitude = LATITUDE; 
    newRegion.center.longitude = LONGITUDE; 

    newRegion.span.latitudeDelta = 0.05f; 
    newRegion.span.longitudeDelta = 0.05f; 

    self.mapView.showsPointsOfInterest = YES; 
    [self.mapView setRegion:newRegion animated:YES]; 
} 


-(NSMutableArray *) createAnnotations { 

    NSMutableArray *annotations = [[NSMutableArray alloc] init]; 

    NSString *path = [[NSBundle mainBundle] pathForResource:@"location" ofType:@"plist"]; 

    NSArray *locations = [NSArray arrayWithContentsOfFile:path]; 

    for (NSDictionary *row in locations){ 
     NSNumber *latitude = [row objectForKey:@"latitude"]; 
     NSNumber *longitude = [row objectForKey:@"longitude"]; 
     NSString *title = [row objectForKey:@"title"]; 

     CLLocationCoordinate2D coord; 
     coord.latitude = latitude.doubleValue; 
     coord.longitude = longitude.doubleValue; 

     MapViewAnnotation *annotation = [[MapViewAnnotation alloc] initWithTitle:title AndCoordinate:coord]; 
     [annotations addObject: annotation]; 
    } 

    return annotations; 


} 
@end 
,

지도에서 보여주고 자하는 하나의 테스트 위치에 대해 좌표를 갖고 있지만 핀이지도에 표시되지 않는 plist를 살펴 보겠습니다. 다시 말하지만, 프로그래밍 방식으로 모든 작업을 수행하고 있으며, xib 또는 스토리 보드는 문제가되지 않습니다.

왜 주석이 표시되지 않는지 알 수 없습니다. 나는 내 것과 같은 다른 상황을 찾기 위해 스택을 조사했지만 도움이 될만한 것을 찾기가 불행했습니다.

감사합니다. 내 PLIST에 대답은 아주 간단했다

+0

createAnnotations가 실제로 배열과 예상되는 좌표에 주석을 추가하는지 확인하십시오. 예 : '[annotations addObject ...] 행 바로 앞에 NSLog (@ title = % @, lat = % f, long = % f ", title, coord.latitude, coord.longitude)를 입력하십시오. plist에있는 모든 주석을 올바르게 기록하고 좌표가 맞습니까? 로그가 예상 한 것과 다를 경우'path'가 유효하고'locations' 어레이가 비어 있지 않은지 확인하십시오. – Anna

+0

예. 올바른 데이터를 출력하고 있습니다. 하지만 너 덕분에 알아 냈어 .. 72.32에 좌표 중 하나를 가졌을 때 -72.32 – mufc

답변

0

,

나는 좌표 실제로의 경도 -72.536되어 있어야

latitude: 42.37 
longitude: 72.536 

했다.