我正在尝试为我的简单地图视图创建自定义注释,但是当我运行模拟器时,annotaiton引脚没有显示在地图上。
我有一个实现MKAnnotation协议的MapViewAnnotation类,我有另一个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 :(得分:0)
答案很简单,
在我的plist中,我有坐标
latitude: 42.37
longitude: 72.536
实际上它应该是-72.536
经度。