我一直在尝试使用MKMapView做一个简单的应用程序,并且在尝试调用<MKAnnotation>
类时遇到了SIGABRT错误。
DetailViewController.m
文件:
#import "WVTDetailViewController.h"
#import <MapKit/MapKit.h>
#import <AddressBook/AddressBook.h>
#import "Pin.h"
@interface WVTDetailViewController ()
{
CLLocationCoordinate2D location;
NSMutableDictionary *masterDict;
NSDictionary *googleDict;
NSDictionary *yahooDict;
NSDictionary *appleDict;
NSDictionary *microsoftDict;
NSDictionary *facebookDict;
}
- (void)configureView;
@end
@implementation WVTDetailViewController
@synthesize address = _address;
- (void)configureView
{
// Update the user interface for the detail item.
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self configureView];
[_mapView setShowsUserLocation:YES];
CLLocationCoordinate2D googleLocation;
googleLocation.latitude = 37.4221;
googleLocation.longitude = -122.0844;
// Yahoo:
CLLocationCoordinate2D yahooLocation;
yahooLocation.latitude = 37.417354;
yahooLocation.longitude = -122.025189;
// Apple:
CLLocationCoordinate2D appleLocation;
appleLocation.latitude = 37.332313;
appleLocation.longitude = -122.030746;
// Microsoft:
CLLocationCoordinate2D microsoftLocation;
microsoftLocation.latitude = 47.639764;
microsoftLocation.longitude = -122.128435;
// Facebook:
CLLocationCoordinate2D facebookLocation;
facebookLocation.latitude = 37.483489;
facebookLocation.longitude = -122.149542;
if ([_address isEqualToString:@"Google"])
{
location = googleLocation;
}
if ([_address isEqualToString:@"Yahoo"])
{
location = yahooLocation;
}
if ([_address isEqualToString:@"Apple"])
{
location = appleLocation;
}
if ([_address isEqualToString:@"Microsoft"])
{
location = microsoftLocation;
}
if ([_address isEqualToString:@"Facebook"])
{
location = facebookLocation;
}
googleDict = @{(NSString *)kABPersonAddressStreetKey: @"1600 Amphitheatre Pkwy",
(NSString *)kABPersonAddressCityKey: @"Mountain View",
(NSString *)kABPersonAddressStateKey: @"CA",
(NSString *)kABPersonAddressZIPKey: @"94043"
};
yahooDict = @{(NSString *)kABPersonAddressStreetKey: @"701 1st Ave",
(NSString *)kABPersonAddressCityKey: @"Sunnyvale",
(NSString *)kABPersonAddressStateKey: @"CA",
(NSString *)kABPersonAddressZIPKey: @"94089"
};
appleDict = @{(NSString *)kABPersonAddressStreetKey: @"1 Infinite Loop",
(NSString *)kABPersonAddressCityKey: @"Cupertino",
(NSString *)kABPersonAddressStateKey: @"CA",
(NSString *)kABPersonAddressZIPKey: @"95014"
};
microsoftDict = @{(NSString *)kABPersonAddressStreetKey: @"One Microsoft Way",
(NSString *)kABPersonAddressCityKey: @"Redmond",
(NSString *)kABPersonAddressStateKey: @"WA",
(NSString *)kABPersonAddressZIPKey: @"98052"
};
facebookDict = @{(NSString *)kABPersonAddressStreetKey: @"1 Hacker Way",
(NSString *)kABPersonAddressCityKey: @"Menlo Park",
(NSString *)kABPersonAddressStateKey: @"CA",
(NSString *)kABPersonAddressZIPKey: @"94025"
};
masterDict = [[NSMutableDictionary alloc] init];
[masterDict setObject: googleDict forKey: @"Google"];
[masterDict setObject: yahooDict forKey: @"Yahoo"];
[masterDict setObject: appleDict forKey: @"Apple"];
[masterDict setObject: microsoftDict forKey: @"Microsoft"];
[masterDict setObject: facebookDict forKey: @"Facebook"];
}
- (void)viewWillAppear:(BOOL)animated
{
// Establish a 1.5km "square" around the 2D coordinate "location" and display it
MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(location, 1500, 1500);
[_mapView setRegion:viewRegion animated:YES];
Pin *myLocationPin = [[Pin alloc] initWithNameAndCoords: _address coords: location];
myLocationPin.addr = [masterDict objectForKey:_address];
[_mapView addAnnotation: myLocationPin];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
和Pin.m
文件(实现<MKAnnoation>
的文件):
#import "Pin.h"
#import <AddressBook/AddressBook.h>
@implementation Pin
- (id) initWithNameAndCoords: (NSString *) name coords: (CLLocationCoordinate2D) coords
{
if (self = [super init])
{
_name = name;
_coords = coords;
}
return self;
}
- (NSString *)title
{
return _name;
}
- (MKMapItem*) returnMapItem
{
MKMapItem *mapItem = [[MKMapItem alloc] initWithPlacemark:[[MKPlacemark alloc] initWithCoordinate:_coords addressDictionary: _addr]];
mapItem.name = _name;
return mapItem;
}
@end
首先,SIGABRT错误位于[_mapView addAnnotation: myLocationPin];
行。我在Pin.m
说Auto property synthesis will not synthesize property declared in a protocol
时也会收到警告。我使用@synthesize
指令强制合成。
我已经查看了调试器输出,但这似乎不是问题 - 合成属性正在接收它们应该的值。
答案 0 :(得分:0)
你应该把它添加到Pin.m中的getter函数
-(CLLocationCoordinate2D)coordinate { return _coord; }