2014-09-23 96 views
0

我想为我的一个简单地图视图创建一个自定义注释,但是当我运行模拟器时,annotaiton图钉没有显示在地图上。MKAnnotation在地图视图上不显示

我有一个MapViewAnnotation类,它实现了MKAnnotation协议,我有另一个MapViewController类,在那里我以编程方式创建一个mapView并显示地图。

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或故事板(尽管这不应该是问题)。

我不知道为什么注释没有显示出来。我试图在堆栈上寻找类似我的其他情况,但很不幸找到任何可能有所帮助的东西。

谢谢。

+0

确认createAnnotations实际上是将注释添加到数组和预期坐标。例如。在[[annotations addObject ...]行之前,放上'NSLog(@“title =%@,lat =%f,long =%f”,title,coord.latitude,coord.longitude);'。它是否正确记录了plist中的所有注释,并且坐标是否正确?如果日志不符合您的预期,请检查'path'是否有效,'locations'数组是否为空,等等。 – Anna 2014-09-23 02:12:41

+0

是的。它输出正确的数据。但我明白了,多亏了你..我有一个72.32的坐标,当它应该是-72.32 – mufc 2014-09-23 02:38:55

回答

0

答案是非常简单的,

在我的plist我有坐标

latitude: 42.37 
longitude: 72.536 

当它应该实际上已经-72.536经度。